AlmaLinux 9:PostgreSQL 版本选择与初始化
在 AlmaLinux 9.8 特权容器中实测:dnf module list postgresql、--downloadonly 对比版本、initdb、pg_ctl、psql、pg_hba.conf。日志:plans/content-expansion/evidence/logs/alma-postgresql-install.log。
初始化在 13.23 上跑通;16.x 流程相同未单独执行。postgresql-setup/systemctl 路径依赖 systemd,未在容器验证。
两套来源:非模块化 13 与模块流
Section titled “两套来源:非模块化 13 与模块流”dnf module list postgresqldnf -y install --downloadonly --destdir=/tmp/a postgresql-serverls /tmp/a | grep '^postgresql-server'postgresql 15 client, server [d]postgresql 16 client, server [d]postgresql 18 client, server [d]
postgresql-server-13.23-6.el9_8.x86_64.rpm模块流列的是 15/16/18,但直接装得到 13.23:appstream 里有非模块化的 13 包,未 enable 流时 dnf 用它。这不是 bug,是 EL9 打包方式决定的。
显式选择版本
Section titled “显式选择版本”sudo dnf module enable postgresql:16sudo dnf -y install --downloadonly --destdir=/tmp/b postgresql-serverls /tmp/b | grep '^postgresql-server'postgresql-server-16.15-1.module+el9.8.0+40320+f6f9ed3c.x86_64.rpm带 module+el9.8.0 才是模块流版本。先 enable、再装、再 rpm -q 确认,这三步不要省。
初始化:容器里用 initdb
Section titled “初始化:容器里用 initdb”sudo mkdir -p /var/lib/pgsql/data /var/run/postgresqlsudo chown -R postgres:postgres /var/lib/pgsql /var/run/postgresqlsudo -u postgres /usr/bin/initdb -D /var/lib/pgsql/data \ --auth-local=trust --auth-host=scram-sha-256cat /var/lib/pgsql/data/PG_VERSIONsyncing data to disk ... ok13PG_VERSION 文件记录大版本,用它确认“装的是不是想要的那个版本”比看包名更直接。
启动、连接与认证
Section titled “启动、连接与认证”sudo -u postgres /usr/bin/pg_ctl -D /var/lib/pgsql/data \ -l /var/lib/pgsql/data/startup.log -o '-k /var/run/postgresql' startsudo -u postgres psql -h /var/run/postgresql -tAc 'select version();'grep -vE '^#|^$' /var/lib/pgsql/data/pg_hba.confPostgreSQL 13.23 on x86_64-redhat-linux-gnu, ...local all all trusthost all all 127.0.0.1/32 scram-sha-256host all all ::1/128 scram-sha-256真实主机上的标准路径
Section titled “真实主机上的标准路径”sudo dnf module enable postgresql:16sudo dnf -y install postgresql-serversudo postgresql-setup --initdbsudo systemctl enable --now postgresqlpsql --versionpostgresql-setup 负责初始化并注册 systemd 单元;容器里执行会报 System has not been booted with systemd,所以容器用 initdb。
- 跳过 module enable:装到 13,应用却按 16 的语法写 SQL;
/var/run/postgresql未创建:本地 socket 连接失败;- 混用模块流与 PGDG:同一个大版本出现两个来源,升级时互相覆盖。
练习:从选版本到能连上
Section titled “练习:从选版本到能连上”dnf module list postgresqlsudo dnf module enable postgresql:16sudo dnf -y install postgresql-serverrpm -q postgresql-server | grep modulesudo postgresql-setup --initdbsudo systemctl enable --now postgresqlsudo -u postgres psql -tAc 'select version();'检查:
rpm -q输出含modulesystemctl is-active postgresql为 activeselect version()显示 16.xcat PG_VERSION为 16
常见问题
为什么模块流列了 15/16/18,装出来却是 13?
appstream 同时存在非模块化的 postgresql 13;未 enable 模块流时 dnf 解析到它。先 dnf module enable postgresql:16 才会装 16。
怎么确认装到了想要的版本?
三步:rpm -q postgresql-server 看 release 里有没有 module、cat /var/lib/pgsql/data/PG_VERSION 看实际初始化的版本、psql -c "select version();" 看运行版本。
容器里能跑 PostgreSQL 吗?
能,用 initdb + pg_ctl,不需要 systemd;但生产建议用 systemd 单元或容器编排,并由它们负责重启与开机自启。
- 容器服务:Quadlet 管理容器
- 安装后核对:新装系统 7 项核对
- 日志轮转:logrotate 实战