Rocky 9 装 PostgreSQL:先选流,再初始化
在 Rocky Linux 9.8 特权容器中实测:dnf module list postgresql、--downloadonly 对比装到的包版本、initdb、pg_ctl start、psql 连接、pg_hba.conf。日志:plans/content-expansion/evidence/logs/rocky-postgresql-install.log。
初始化流程在 13.23 上跑通;16.x 的流程相同、版本号不同,未单独执行。systemd 的 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 server and client modulepostgresql 16 client, server [d] PostgreSQL server and client modulepostgresql 18 client, server [d] PostgreSQL server and client module
postgresql-server-13.23-6.el9_8.x86_64.rpm模块流列出 15/16/18,但直接装解析到 13.23——因为 appstream 里同时存在非模块化的 13 包,没启用模块流时 dnf 走它。
要高版本:先 enable 流
Section titled “要高版本:先 enable 流”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.rpmmodule+el9.8.0 说明这条来自模块流。装之前先确认版本,不要装完才发现是 13。
初始化数据目录
Section titled “初始化数据目录”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-256syncing data to disk ... okSuccess. You can now start the database server using: /usr/bin/pg_ctl -D /var/lib/pgsql/data -l logfile start--auth-local=trust 只适合本地测试;生产应保持 scram-sha-256,并给 postgres 用户设密码。
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();'waiting for server to start.... doneserver startedPostgreSQL 13.23 on x86_64-redhat-linux-gnu, ...容器里没有 systemd,所以用 pg_ctl 直接起;-k 指定 socket 目录,否则客户端找不到本地 socket。
pg_hba 长什么样
Section titled “pg_hba 长什么样”grep -vE '^#|^$' /var/lib/pgsql/data/pg_hba.conflocal all all trusthost all all 127.0.0.1/32 scram-sha-256host all all ::1/128 scram-sha-256local 是 Unix socket,host 是 TCP。改完必须 pg_ctl reload 或 restart 才生效。
有 systemd 的环境
Section titled “有 systemd 的环境”sudo postgresql-setup --initdbsudo systemctl enable --now postgresqlsudo -u postgres psql -c 'select version();'真实主机上这两条是标准路径;它们依赖 systemd,容器里会报 System has not been booted with systemd(本次实测如此)。
- 直接
dnf install postgresql-server装到 13,以为得到 15/16; - socket 目录不存在:
/var/run/postgresql没建,psql连不上; - 改了 pg_hba 不 reload:认证规则不生效。
练习:确认版本再装
Section titled “练习:确认版本再装”dnf module list postgresqlsudo dnf module enable postgresql:16sudo dnf -y install postgresql-serverrpm -q postgresql-serversudo postgresql-setup --initdbsudo systemctl enable --now postgresqlsudo -u postgres psql -c 'select version();'检查:
rpm -q postgresql-server版本是 16.x(含module字样)systemctl is-active postgresql为 activepsql -c 'select version();'输出 16.x
常见问题
为什么 dnf 装到 13 而不是模块列表里的默认 15?
appstream 同时提供非模块化的 postgresql 13 与模块流 15/16/18;未启用模块流时 dnf 解析到非模块化包。要 15/16/18 必须先 dnf module enable。
模块流和 PGDG 仓库怎么选?
模块流是发行版自带、生命周期跟随系统,适合稳定版本;需要更新的小版本或扩展时用 PGDG。两者不要混装同一个大版本。
postgresql-setup --initdb 报 systemd 错误?
该命令依赖 systemd;容器/救援环境用 initdb 直接初始化,再 pg_ctl 启动。
- 容器服务:Quadlet 管理容器
- 日志轮转:logrotate 实战
- 安装后核对:安装后 7 项核对