
下面给出一份在 CentOS 7.9 源码编译安装 PostgreSQL 18.4 的可直接复制执行流程,包含依赖、configure 参数、initdb、systemd 自启、远程访问,以及 CentOS7 特有的坑(GCC 4.8.5 是否够用、ICU、libxml 等)。
结论先行:PostgreSQL 18.x 在 CentOS 7.9 默认 GCC 4.8.5 下可以编译通过(PG 官方要求 C99,4.8.5 满足),但建议装 devtoolset 用 GCC 7/9 更稳;ICU 必须用系统自带 libicu-devel-50,不要禁用 ICU,否则排序规则会受限。
1. 安装编译依赖:
修改yum源为aliyun源:
cd /etc/yum.repos.d/
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
curl -O http://mirrors.aliyun.com/repo/Centos-7.repo
mv Centos-7.repo CentOS-Base.repo
yum install -y wget gcc gcc-c++ make readline-devel zlib-devel openssl-devel libicu-devel bison flex perl-devel perl-ExtUtils-Embed libxml2-devel libxslt-devel systemd-devel python3-devel
如果后面要 --with-systemd(CentOS7 用 systemd 管理 PG),systemd-devel 必装。
可选升级 GCC(推荐但非必须):
yum install -y centos-release-scl devtoolset-9-gcc devtoolset-9-gcc-c++ scl-utils
cd /etc/yum.repos.d/
for f in CentOS-SCLo-scl.repo CentOS-SCLo-scl-rh.repo; do
if [ -f "$f" ]; then
sed -i 's/^mirrorlist=/#mirrorlist=/g' "$f"
sed -i 's|^#baseurl=http://mirror.centos.org/centos/7/sclo|baseurl=https://mirrors.aliyun.com/centos/7/sclo|g' "$f"
sed -i 's|^baseurl=http://mirror.centos.org/centos/7/sclo|baseurl=https://mirrors.aliyun.com/centos/7/sclo|g' "$f"
fi
done
sed -i 's/^mirrorlist=/#mirrorlist=/g' /etc/yum.repos.d/CentOS-Base.repo
sed -i 's|^#baseurl=http://mirror.centos.org/centos|baseurl=http://vault.centos.org/centos|g' /etc/yum.repos.d/CentOS-Base.repo
sed -i 's|centos-sclo-sclo|centos-sclo|g' /etc/yum.repos.d/CentOS-SCLo-scl.repo
rm -rf /etc/yum.repos.d/CentOS-SCLo-scl.repo
清缓存:
yum clean all
rm -rf /var/cache/yum
yum makecache
yum install -y centos-release-scl devtoolset-9-gcc devtoolset-9-gcc-c++ scl-utils
scl enable devtoolset-9 bash # 当前 shell 切到 gcc 9
gcc --version
2. 下载 PostgreSQL 18.4 源码
官方 FTP:https://ftp.postgresql.org/pub/source/v18.4/postgresql-18.4.tar.gz
cd /usr/local/src
wget https://ftp.postgresql.org/pub/source/v18.4/postgresql-18.4.tar.gz
tar xf postgresql-18.4.tar.gz
3. configure(关键步骤)
独立安装openssl:
cd /usr/local/src
yum install -y gcc make perl-core zlib-devel
wget https://github.com/openssl/openssl/releases/download/OpenSSL_1_1_1w/openssl-1.1.1w.tar.gz
tar xf openssl-1.1.1w.tar.gz
cd openssl-1.1.1w
./config --prefix=/usr/local/openssl11 --openssldir=/usr/local/openssl11 shared zlib
make -j$(nproc)
make install_sw # 只装库和二进制,不装文档
3)注册动态库(让 PG 运行时能找到 libssl.so.1.1)
echo "/usr/local/openssl11/lib" > /etc/ld.so.conf.d/openssl11.conf
ldconfig
/usr/local/openssl11/bin/openssl version # 应显示 1.1.1w
推荐生产级参数:
cd /usr/local/src/postgresql-18.4
./configure --prefix=/opt/pgsql-18.4 --with-pgport=5432 --with-openssl --with-includes=/usr/local/openssl11/include --with-libraries=/usr/local/openssl11/lib --with-icu --with-readline --with-zlib --with-libxml --with-libxslt --with-perl --with-python --with-systemd
常见报错对照:
ICU library not found → 没装 libicu-devel,CentOS7 自带的是 ICU 50,能用,不要 --without-icu
bison/flex not found → 装 bison flex
readline library not found → 装 readline-devel
configure 结尾确认这些行是 yes:OpenSSL / ICU / readline / zlib / systemd
4. 编译安装
make -j$(nproc) && make install
5. 建用户、目录、环境变量
groupadd -g 1001 postgres
useradd -u 1001 -g postgres -m -s /bin/bash postgres
mkdir -p /data/pg18/data
chown -R postgres:postgres /opt/pgsql-18.4 /data/pg18
chmod 700 /data/pg18/data
vim /etc/profile.d/pgsql.sh
export PGHOME=/opt/pgsql-18.4
export PGDATA=/data/pg18/data
export PATH=$PGHOME/bin:$PATH
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
source /etc/profile.d/pgsql.sh
6. 初始化数据库集群
切到 postgres 用户:
su - postgres
initdb -D $PGDATA -E UTF8 --locale=C -W
PG 18 的 initdb 默认开启 data checksums,如要关掉加 --no-data-checksums。
7. 基础配置(监听 + 远程)
在文件的最后面添加:
vim $PGDATA/postgresql.conf
listen_addresses = '*'
port = 5432
max_connections = 200
logging_collector = on
log_directory = 'log'
vim $PGDATA/pg_hba.conf
末尾加:
host all all 0.0.0.0/0 scram-sha-256
PG 18 已弃用 md5,用 scram-sha-256;远程连前先 ALTER USER postgres PASSWORD 'xxx';
8. 启动测试
mkdir -p /data/pg18/data/log
touch/data/pg18/data/log/startup.log
pg_ctl -D $PGDATA -l $PGDATA/log/startup.log start
psql -c "SELECT version();"
pg_ctl stop
9. systemd 自启(CentOS7)
su -
vim /usr/lib/systemd/system/postgresql-18.service
[Unit]
Description=PostgreSQL 18.4
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
Environment=PGDATA=/data/pg18/data
ExecStart=/opt/pgsql-18.4/bin/pg_ctl start -D ${PGDATA} -l ${PGDATA}/log/startup.log
ExecStop=/opt/pgsql-18.4/bin/pg_ctl stop -D ${PGDATA}
ExecReload=/opt/pgsql-18.4/bin/pg_ctl reload -D ${PGDATA}
TimeoutSec=60
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable postgresql-18
systemctl start postgresql-18
systemctl status postgresql-18
10. CentOS 7.9 专属注意事项
GCC 4.8.5:PG18 能编过,但生产建议 devtoolset-9/11,避免个别警告变错误。
ICU 50 很老:够用,但部分新 collation 特性受限;别用 --without-icu。
Python:CentOS7 自带 python3 是 3.6,要 --with-python 需 python3-devel,否则 configure 会跳过。
SELinux:如果远程连不上且日志无报错,先 setenforce 0 验证,再写 policy 或改 postgresql_port_t。
防火墙:firewall-cmd --add-port=5432/tcp --permanent && firewall-cmd --reload



CentOS7.9 编译安装配置-PG-18.4