1. 下载源码
从 https://www.postgresql.org/ftp/source/ 下载源码postgresql-15.2.tar.gz
到 /root/soft
2. 准备编译环境
首先把自动创建的postgres用户删除, 命令如下:
userdel -r postgres
然后把用户postgres的HOME目录建在“/home”目录下:
groupadd -g 701 postgres
useradd -g 701 -u 701 -s /bin/bash -m postgres
cd /root/soft
tar -xvf postgresql-15.2.tar.gz
如果需要更新 CentOS,需要将镜像从 mirror.centos.org 更改为 vault.centos.org
cd /etc/yum.repos.d/
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
安装依赖 参考
yum -y install ncurses-devel readline-devel zlib zlib-devel perl-ExtUtils-Embed
yum install lz4-devel
yum install openssl openssl-devel
yum install libxml2 libxml2-devel
yum install pam pam-devel
yum install systemd-devel
[root@localhost soft]# mkdir build_dir
[root@localhost soft]# ls
build_dir postgresql-15.2 postgresql-15.2.tar.gz
# 编译选项参考 https://www.postgresql.org/docs/15/install-procedure.html
# systemd 配置文件参考 https://www.postgresql.org/docs/15/server-start.html
# When using systemd, you can use the following service unit file (e.g., at /etc/systemd/system/postgresql.service)
# 编译配置
../postgresql-15.2/configure --with-lz4 --with-openssl --with-pam --with-libxml --with-systemd CFLAGS="-O0 -DOPTIMIZER_DEBUG -g3 -gdwarf-2" --prefix=/home/postgres/soft
# 编译
make & make install
#在编译安装的时候,很多是直接执行 make && make install
#但是如果想,编译所有可以编译的内容,比如文档、其它模块(contrib),可以执行 make world
#但是如果想,编译所有可以编译的内容,包含其它模块(contrib,但是不包含文档,可以执行
make install-world-bin
3. 配置数据环境
vi /home/postgres/.bashrc
mkdir /home/postgres/data
chown -R postgres:postgres /home/postgres/data
vi /home/postgres/.bashrc
export PATH=/home/postgres/soft/bin:$PATH
export LD_LIBRARY_PATH=/home/postgres/soft/lib:$LD_LIBRARY_PATH
export PGDATA=/home/postgres/data
export PGHOST=/tmp
4. 创建数据库
su - postgres
initdb -E UTF-8 -D /home/postgres/data --locale=en_US.UTF-8 --wal-segsize 64 -W
# 启动数据库
pg_ctl -D /home/postgres/data start
5. 添加服务
保存以下内容为 postgresql-15.service 并放到 /usr/lib/systemd/system/ 目录下
# 文件路径 /usr/lib/systemd/system/postgresql-15.service
[Unit]
Description=PostgreSQL 15 database server
Documentation=https://www.postgresql.org/docs/15/static/
After=syslog.target
After=network-online.target
[Service]
Type=notify
User=postgres
Group=postgres
# 数据目录 Location of database directory
Environment=PGDATA=/home/postgres/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
Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
Environment=PG_OOM_ADJUST_VALUE=0
# 执行文件目录
#ExecStartPre=/home/postgres/soft/bin/postgresql-15-check-db-dir ${PGDATA}
ExecStart=/home/postgres/soft/bin/postmaster -D ${PGDATA}
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
# Do not set any timeout value, so that systemd will not kill postmaster
# during crash recovery.
TimeoutSec=0
# 0 is the same as infinity, but "infinity" needs systemd 229
TimeoutStartSec=0
TimeoutStopSec=1h
[Install]
WantedBy=multi-user.target
重新加载服务
systemctl daemon-reload
启用服务
systemctl enable postgresql-15
查看服务状态
systemctl status postgresql-15
启动服务
systemctl start postgresql-15
停止服务
systemctl stop postgresql-15