在RockyLinux 9.x上安装(从源代码中构建)PostgreSQL 15.x

简而言之

在企业级数据库中,大家熟知的是PostgreSQL。截至2023年4月,最新版本是15.2。我们将以从源代码开始构建的方式,在RockyLinux上进行安装。如果是RHEL兼容的操作系统(如AlmaLinux或OracleLinux),可以进行类似的操作。
注意,需要预先安装zlib。

准备工作

如果安装了操作系统标准的PostgreSQL,则需要卸载。

rpm -e qt-postgresql-4.8.5-13.el7.x86_64
rpm -e qt5-qtbase-postgresql-5.6.1-10.el7.x86_64
rpm -e postgresql-devel-9.2.18-1.el7.x86_64
rpm -e qt3-PostgreSQL-3.3.8b-51.el7.x86_64
rpm -e postgresql-libs-9.2.18-1.el7.x86_64

提前创建用户和工作目录。

mkdir /usr/local/pgsql 
#useradd postgres
useradd postgres -m -s /bin/bash
chown postgres:postgres /usr/local/pgsql 

验证环境版本

ソフトウェアバージョンPostgreSQL15.2OSRockeyLiux 9.1

安装任务

cd /usr/local/src
wget https://ftp.postgresql.org/pub/source/v15.2/postgresql-15.2.tar.gz
tar xvzf postgresql-15.2.tar.gz
chown -R postgres:postgres postgresql-15.2
su postgres
cd postgresql-15.2
./configure --without-readline
make -j 8
make install
su postgres
vi ~/.bashrc
#以下の内容を最下部に書いて保存
export PATH="$PATH":/usr/local/pgsql/bin 
export POSTGRES_HOME=/usr/local/pgsql 
export PGLIB=$POSTGRES_HOME/lib 
export PGDATA=$POSTGRES_HOME/data 
export MANPATH="$MANPATH":$POSTGRES_HOME/man 
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIB"  
source ~/.bashrc

初始设定

将数据库进行初始化

initdb --no-locale 

听取地址的设置

一般情况下,配置仅限于本地主机,但如果以主机服务器形式从其他AP服务器或客户端访问数据库,则需要适当扩大可访问范围。
本次设置为从任意地址接收通信(需注意安全性)。

vi /usr/local/pgsql/data/postgresql.conf
#listen_addresses = 'localhost'
↓
listen_addresses = '*'
vi $PGDATA/pg_hba.conf
host    all             all              0.0.0.0/0               trust

确认启动

pg_ctl -w start

添加管理用户

createuser admin -s -P

停止一次PostgreSQL

pg_ctl -w stop

启动脚本

起动脚本是由CentOS7.2提供的rpm构建而成。

su root

cat > /usr/lib/systemd/system/postgresql.service << EOF
# It's not recommended to modify this file in-place, because it will be
# overwritten during package upgrades.  If you want to customize, the
# best way is to create a file "/etc/systemd/system/postgresql.service",
# containing
#   .include /lib/systemd/system/postgresql.service
#   ...make your changes here...
# For more info about custom unit files, see
# http://fedoraproject.org/wiki/Systemd#How_do_I_customize_a_unit_file.2F_add_a_custom_unit_file.3F

# For example, if you want to change the server's port number to 5433,
# create a file named "/etc/systemd/system/postgresql.service" containing:
#   .include /lib/systemd/system/postgresql.service
#   [Service]
#   Environment=PGPORT=5433
# This will override the setting appearing below.

# Note: changing PGPORT or PGDATA will typically require adjusting SELinux
# configuration as well; see /usr/share/doc/postgresql-*/README.rpm-dist.

# Note: do not use a PGDATA pathname containing spaces, or you will
# break postgresql-setup.

# Note: in F-17 and beyond, /usr/lib/... is recommended in the .include line
# though /lib/... will still work.

[Unit]
Description=PostgreSQL database server
After=network.target

[Service]
Type=forking

User=postgres
Group=postgres

# Port number for server to listen on
Environment=PGPORT=5432

# Location of database directory
Environment=PGDATA=/usr/local/pgsql/data

# Where to send early-startup messages from the server (before the logging
# options of postgresql.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog

# Disable OOM kill on the postmaster
OOMScoreAdjust=-1000

#ExecStartPre=/usr/local/pgsql/bin/postgresql-check-db-dir \${PGDATA} <=このモジュールは存在しないのでコメント
ExecStart=/usr/local/pgsql/bin/pg_ctl start -D \${PGDATA} -s -o "-p \${PGPORT}" -w -t 300
ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D \${PGDATA} -s -m fast
ExecReload=/usr/local/pgsql/bin/pg_ctl reload -D \${PGDATA} -s

# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300

[Install]
WantedBy=multi-user.target

EOF

在服务中的启动和停止

systemctl start postgresql
systemctl stop postgresql
systemctl enable postgresql
广告
将在 10 秒后关闭
bannerAds