跳转到内容
搜索 AlmaLinux 文档

    浏览当前文档目录

    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 与模块流”
    Terminal window
    dnf module list postgresql
    dnf -y install --downloadonly --destdir=/tmp/a postgresql-server
    ls /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 打包方式决定的。

    Terminal window
    sudo dnf module enable postgresql:16
    sudo dnf -y install --downloadonly --destdir=/tmp/b postgresql-server
    ls /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 确认,这三步不要省。

    Terminal window
    sudo mkdir -p /var/lib/pgsql/data /var/run/postgresql
    sudo chown -R postgres:postgres /var/lib/pgsql /var/run/postgresql
    sudo -u postgres /usr/bin/initdb -D /var/lib/pgsql/data \
    --auth-local=trust --auth-host=scram-sha-256
    cat /var/lib/pgsql/data/PG_VERSION
    syncing data to disk ... ok
    13

    PG_VERSION 文件记录大版本,用它确认“装的是不是想要的那个版本”比看包名更直接。

    Terminal window
    sudo -u postgres /usr/bin/pg_ctl -D /var/lib/pgsql/data \
    -l /var/lib/pgsql/data/startup.log -o '-k /var/run/postgresql' start
    sudo -u postgres psql -h /var/run/postgresql -tAc 'select version();'
    grep -vE '^#|^$' /var/lib/pgsql/data/pg_hba.conf
    PostgreSQL 13.23 on x86_64-redhat-linux-gnu, ...
    local all all trust
    host all all 127.0.0.1/32 scram-sha-256
    host all all ::1/128 scram-sha-256
    Terminal window
    sudo dnf module enable postgresql:16
    sudo dnf -y install postgresql-server
    sudo postgresql-setup --initdb
    sudo systemctl enable --now postgresql
    psql --version

    postgresql-setup 负责初始化并注册 systemd 单元;容器里执行会报 System has not been booted with systemd,所以容器用 initdb。

    • 跳过 module enable:装到 13,应用却按 16 的语法写 SQL;
    • /var/run/postgresql 未创建:本地 socket 连接失败;
    • 混用模块流与 PGDG:同一个大版本出现两个来源,升级时互相覆盖。
    Terminal window
    dnf module list postgresql
    sudo dnf module enable postgresql:16
    sudo dnf -y install postgresql-server
    rpm -q postgresql-server | grep module
    sudo postgresql-setup --initdb
    sudo systemctl enable --now postgresql
    sudo -u postgres psql -tAc 'select version();'

    检查:

    1. rpm -q 输出含 module
    2. systemctl is-active postgresql 为 active
    3. select version() 显示 16.x
    4. cat 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 单元或容器编排,并由它们负责重启与开机自启。