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

↑ 关注“少安事务所”公众号,欢迎⭐收藏,不错过精彩内容~

本文写于 2023-09-29

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 ^9 维护,提供了 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 类似 。

  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 已在 CentOS 7 上安装完成。

总结

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

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

🌻 往期精彩 ▼


-- / END / --

alt

如果这篇文章为你带来了灵感或启发,就请帮忙点『赞』or『在看』or『转发』吧,感谢!(๑˃̵ᴗ˂̵)

本文由 mdnice 多平台发布

  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值