暂无图片
暂无图片
3
暂无图片
暂无图片
暂无图片

【PG16】 后 RHEL 7 时代, PG 16 如何在 CentOS 7 上运行

原创 严少安 2023-10-02
4202

pgbanner.png

PostgreSQL 16 Released

9/14, PostgreSQL 16 正式发布。从发布公告[1] 和 Release Notes[2] 可以看到 PG16 包含了诸多新特性和增强改进。

  1. 性能提升,查询计划支持并行 FULLRIGHT 关联。[3]
  2. 逻辑复制,支持从 standby 服务器进行复制,支持订阅者并行应用大事务,新增预定义角色 pg_create_subscription[4]
  3. 开发体验,增加了 SQL/JSON 构造函数 (JSON_ARRAY(), JSON_ARRAYAGG())和恒等函数 (IS JSON)。[5]
  4. 监控增强,增加 pg_stat_io 视图,以支持监控 I/O 统计数据。[6]

CentOS 7 上 RPM 安装 PG16 (EOL)

一般情况下,在 CentOS 7 系统上安装 PostgreSQL 只需要两条 yum/dnf 命令即可。

sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm sudo yum install -y postgresql16-server

但是从 PostgreSQL 16 开始,这种安装方式在 CentOS 7 上已经被停用,所以执行上面两条命令会看到下面的输出。

No package postgresql16-server available. Error: Nothing to do

从 yum 仓库[7] 也只能找到 rhel 8/9 的 postgresql16 rpm 包,并没有 rhel 7。

其实相关信息 9 月初已在官方网站上发布了公告[8],或许只是注意到的人并不多。

EOL announcement for RHEL 7

PostgreSQL RPM repo stopped adding new packages to the PostgreSQL RPM repo as of Aug 2023, including PostgreSQL 16.

We will maintain older major releases until each major release is EOLed by PostgreSQL project. Please visit here for latest release dates for each major release.

If you have any questions, please either email to pgsql-pkg-yum@lists.postgresql.org, or create a ticket at our redmine.

那么,仍在使用 CentOS 7 的环境如何安装、升级 PostgreSQL 16 呢?

Docker 运行 PG16(容易)

PostgreSQL 的官方 Docker 镜像由 the PostgreSQL Docker Community [8:1] 维护,提供了 4 个 PG16 的镜像,分别基于 Debian bullseye / Debian Bookworm / alpine3.17 / alpine3.18 。

演示步骤如下:

  1. 主机的操作系统为 CentOS 7。
$ hostnamectl Static hostname: centos7.shawnyan.cn ... Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-1160.92.1.el7.x86_64 Architecture: x86-64
  1. 拉取 PG16 的 Docker 镜像。
[shawnyan@centos7 ~]$ docker pull postgres:16 16: Pulling from library/postgres a803e7c4b030: Pull complete 5cf7cbd17f32: Pull complete ddc24c6f1e18: Pull complete 2b0f4d94850a: Pull complete fccb5b7554d1: Pull complete 1dd940c0e742: Pull complete f641e2497276: Pull complete 9c05395a8e66: Pull complete 285e24d225ac: Pull complete 3faa43a5d9fc: Pull complete 482fc7a6b0f4: Pull complete 29ca5fe1b2a4: Pull complete d3012096b6ce: Pull complete Digest: sha256:379b7a1223b394106cc20d18a5177ed77738003416057e8898cde10e6b7a082a Status: Downloaded newer image for postgres:16 docker.io/library/postgres:16 [shawnyan@centos7 ~]$ docker images postgres REPOSITORY TAG IMAGE ID CREATED SIZE postgres 16 ec7f99c50d3c 8 days ago 418MB
  1. 启动 PG16 容器。
[shawnyan@centos7 ~]$ docker run --name pg16 -e POSTGRES_HOST_AUTH_METHOD=trust -d postgres:16 e3bb74e44107d349f8a2c4a0f9ac9cb3aa4ac26e66bb930069b37c563cc815dd [shawnyan@centos7 ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e3bb74e44107 postgres:16 "docker-entrypoint.s…" 2 seconds ago Up 1 second 5432/tcp pg16

这里需要注意的是,需要指定超管用户的密码,或者允许所有连接没有密码登陆,否则容器会启动失败。

[shawnyan@centos7 ~]$ docker logs pg16 Error: Database is uninitialized and superuser password is not specified. You must specify POSTGRES_PASSWORD to a non-empty value for the superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run". You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all connections without a password. This is *not* recommended. See PostgreSQL documentation about "trust": https://www.postgresql.org/docs/current/auth-trust.html
  1. 连接 PG16。

通过 psql 连接 PG16,并查看版本信息。

[shawnyan@centos7 ~]$ docker exec -it pg16 psql -U postgres psql (16.0 (Debian 16.0-1.pgdg120+1)) Type "help" for help. postgres=# select version(); version --------------------------------------------------------------------------------------------------------------------- PostgreSQL 16.0 (Debian 16.0-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit (1 row) postgres=# select 'Hi, PG16~'; ?column? ----------- Hi, PG16~ (1 row)

在 CentOS 7 上编译 PG16 源码(进阶)

PG16 在 CentOS 7 上的源码编译步骤与 PG15 类似,具体可参考文章 《【PG15】【番外篇】自定义 Dockerfile 构建 PostgreSQL 15 编译版 Docker 镜像》

  1. 下载 PG16 的源码,并进行编译、安装。
wget https://mirrors.neusoft.edu.cn/postgresql/source/v16.0/postgresql-16.0.tar.gz tar zxf postgresql-16.0.tar.gz cd postgresql-16.0/ ./configure --prefix=/opt/postgresql --with-extra-version="-ShawnYan" make -j $(nproc) world make install-world
  1. 初始化 PG16。
$ initdb --pgdata="$PGDATA" The files belonging to this database system will be owned by user "postgres". This user must also own the server process. The database cluster will be initialized with locale "en_US.UTF-8". The default database encoding has accordingly been set to "UTF8". The default text search configuration will be set to "english". Data page checksums are disabled. fixing permissions on existing directory /data/pg16/data ... ok creating subdirectories ... ok selecting dynamic shared memory implementation ... posix selecting default max_connections ... 100 selecting default shared_buffers ... 128MB selecting default time zone ... Asia/Tokyo creating configuration files ... ok running bootstrap script ... ok performing post-bootstrap initialization ... ok syncing data to disk ... ok initdb: warning: enabling "trust" authentication for local connections initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb. Success. You can now start the database server using: pg_ctl -D /data/pg16/data -l logfile start
  1. 启动 PG16。
$ pg_ctl -D /data/pg16/data -l logfile start waiting for server to start.... done server started
  1. 查看版本信息。
$ psql psql (16.0-ShawnYan) Type "help" for help. postgres=# select version(); version ------------------------------------------------------------------------------------------------------------------ PostgreSQL 16.0-ShawnYan on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit (1 row)

pg16.png

到此,PG16 已在 CentOS 7 上安装完成。

总结

CentOS 7 的时代即将结束,将来如何选择操作系统,是选用 Redhat、Rocky Linux、Ubuntu,还是云厂商的 Linux ?

或许,在考虑升级 PG 版本的同时,也是时候该考虑一下 OS 的版本了。


  1. https://www.postgresql.org/about/news/postgresql-16-released-2715/ ↩︎

  2. https://www.postgresql.org/docs/16/release-16.html ↩︎

  3. https://www.postgresql.org/docs/16/parallel-query.html ↩︎

  4. https://www.postgresql.org/docs/16/predefined-roles.html ↩︎

  5. https://www.postgresql.org/docs/16/functions-json.html ↩︎

  6. https://www.postgresql.org/docs/16/monitoring-stats.html#MONITORING-PG-STAT-IO-VIEW ↩︎

  7. https://download.postgresql.org/pub/repos/yum/16/redhat/ ↩︎

  8. https://github.com/docker-library/postgres ↩︎ ↩︎

最后修改时间:2023-10-12 15:40:39
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论