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)
# 编译配置
# 编译配置 DOPTIMIZER_DEBUG 导致日志打印特别多
# ../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
../postgresql-15.2/configure --with-lz4 --with-openssl --with-pam --with-libxml --with-systemd CFLAGS="-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong -grecord-gcc-switches -m64 -mtune=generic " LDFLAGS="-Wl,--as-needed" --prefix=/home/postgres/soft1
# 编译
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
6. 编译配置pgbouncer
#配置路径 /root/soft
#下载 https://github.com/libevent/libevent
cd /root/soft/libevent-2.1.12-stable
./configure --prefix=/root/soft/libevent
make
make install
cd pgbouncer-1.18.0/
export PKG_CONFIG_PATH=/root/soft/libevent/lib/pkgconfig
./configure --with-systemd --prefix=/home/postgres/pgbouncer
make
make install
mkdir -p /home/postgres/pgbouncer/share/lib
[root@localhost ld.so.conf.d]# cp -r /root/soft/libevent/lib/* /home/postgres/pgbouncer/share/lib
为了防止运行时动态库链接问题
cd /etc/ld.so.conf.d
在 /etc/ld.so.conf.d 添加文件 libevent.conf 内容如下
/home/postgres/pgbouncer/share/lib
立即生效
/sbin/ldconfig
mkdir /var/log/pgbouncer/
mkdir /var/run/pgbouncer/
chown -R postgres:postgres /var/log/pgbouncer/
chown -R postgres:postgres /var/run/pgbouncer/
重新加载服务
systemctl daemon-reload
启用服务
systemctl enable pgbouncer
查看服务状态
systemctl status pgbouncer
启动服务
systemctl start pgbouncer && tail -f /var/log/pgbouncer/pgbouncer.log
cat /etc/pgbouncer/pgbouncer.ini
停止服务
systemctl stop pgbouncer
7. 相关文章
准备好一个数据库用户及其组别:
groupdel postgres
userdel postgres
-- 查下id 999, 也可以换一个
getent group | grep 999
groupadd -g 999 postgres
useradd -g 999 -u 999 postgres
passwd postgres
下载源码:
wget https://ftp.postgresql.org/pub/source/v15.3/postgresql-15.3.tar.gz
安装编译PostgreSQL所需要的依赖包:
-- root用户
yum install -y readline readline-devel flex bison openssl openssl-devel git
yum install -y gcc gcc-c++ epel-release llvm5.0 llvm5.0-devel clang libicu-devel perl-ExtUtils-Embed zlib-devel openssl openssl-devel pam-devel libxml2-devel libxslt-devel openldap-devel systemd-devel tcl-devel python-devel python3-devel lz4 lz4-devel uuid libuuid-devel
注意:这里头的systemd-devel是后边制作服务时需要的。至于里边一些tcl, python, perl之类,是可选的,不用则不需要。
配置编译项:
为了简化,配置项可以弄得尽量简单点儿。
[06:20:10-postgres@sean-rh1:/iihero/source]$ tar zxf postgresql-15.3.tar.gz
[06:20:42-postgres@sean-rh1:/iihero/source]$ cd postgresql-15.3
./configure --prefix=/usr/pgsql-15.3 --with-openssl --with-lz4 --with-libxml --with-libxslt --with-systemd -with-extra-version=" [By Sean]"
......
当然,您也可以加很多选项,基于前边的依赖包。
比如下例:
./configure LLVM_CONFIG=/usr/lib64/llvm5.0/bin/llvm-config CLANG=/opt/rh/llvm-toolset-7/root/usr/bin/clang --with-openssl --prefix=/usr/pgsql-15.3 --with-pgport=6666 --with-blocksize=8 --with-wal-blocksize=8 --with-segsize=1 --enable-profiling --enable-cassert --enable-dtrace --enable-debug --with-icu --with-uuid=e2fs --with-lz4 --with-llvm --with-python --with-libxml --with-libxslt --with-systemd -with-extra-version=" [By Sean]"
实际试验时,使用上边的简单的配置项即可,我们的主要目的是为了配置后边的服务。
在生产环境当中,其实,你可以从pg_config --configure命令直接提取已安装或编译系统中的configure选项。比如:
[21:41:06-postgres@centos2:/var/lib/pgsql]$ /usr/pgsql-14/bin/pg_config --configure
'--enable-rpath' '--prefix=/usr/pgsql-14' '--includedir=/usr/pgsql-14/include' '--mandir=/usr/pgsql-14/share/man' '--datadir=/usr/pgsql-14/share' '--libdir=/usr/pgsql-14/lib' '--with-lz4' '--with-icu' '--with-llvm' '--with-perl' '--with-python' '--with-tcl' '--with-tclconfig=/usr/lib64' '--with-openssl' '--with-pam' '--with-gssapi' '--with-includes=/usr/include' '--with-libraries=/usr/lib64' '--enable-nls' '--enable-dtrace' '--with-uuid=e2fs' '--with-libxml' '--with-libxslt' '--with-ldap' '--with-selinux' '--with-systemd' '--with-system-tzdata=/usr/share/zoneinfo' '--sysconfdir=/etc/sysconfig/pgsql' '--docdir=/usr/pgsql-14/doc' '--htmldir=/usr/pgsql-14/doc/html' 'CFLAGS=-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' 'LDFLAGS=-Wl,--as-needed' 'LLVM_CONFIG=/usr/lib64/llvm5.0/bin/llvm-config' 'CLANG=/opt/rh/llvm-toolset-7/root/usr/bin/clang' 'PKG_CONFIG_PATH=:/usr/lib64/pkgconfig:/usr/share/pkgconfig' 'PYTHON=/usr/bin/python3'
这样,你也可以根据实际需要进行裁减。
编译安装:
[06:30:47-postgres@sean-rh1:/iihero/source/postgresql-15.3]$ make -j4 world-bin && sudo make install-world-bin
... ...
这里采用了-j4进行并行编译。
如果想清除安装,随时可以:
sudo make distclean