PostgreSQL进行大版本升级

概述

Postgresql从9.6 版本以后,版本命名规则都是以 大版本.小版本 的形式,如 10.5、11.2 等,针对相同大版本,升级小版本的场景,通常情况直接替换安装包即可。 对于大版本不同的情况,因为元数据结构可能变化,需要进行数据升级,本文就介绍一下具体的升级步骤

主要步骤:
1、关闭低版本数据库
2、上传高版本软件包,并配置好数据目录,初始化完成
3、进行版本间的升级check
4、进行版本数据升级
5、基础配置文件替换,启动新库
6、配置好环境,使其重新适配ambari平台

命令pg_upgrade

用于不同大版本之间的数据库升级,以下是一些描述

Before running pg_upgrade you must:
  create a new database cluster (using the new version of initdb)
  shutdown the postmaster servicing the old cluster
  shutdown the postmaster servicing the new cluster

When you run pg_upgrade, you must provide the following information:
  the data directory for the old cluster  (-d DATADIR)
  the data directory for the new cluster  (-D DATADIR)
  the "bin" directory for the old version (-b BINDIR)
  the "bin" directory for the new version (-B BINDIR)

可以看到,命令有几个前提:
1、新库必须要先初始化
2、新旧库均需处于关闭状态
3、需要配置新旧库之前的配置文件目录

下面就测试由11.6升级到15.4 , 具体演示如下

升级步骤

1、升级规划

版本 软件安装目录 数据目录
11.6 /home/pgsql/postgresql /data01/pgsql/data/
15.4 /home/pgsql/postgresql-15.4 /data01/pgsql/data_15.4/

新的数据目录先建好,赋予权限

2、新库初始化

需要将postgresql-15.4.tar.gz 上传至服务器,解压,进入bin目录

[pgsql@gwtest01 bin]$ initdb -D /data01/pgsql/data_15.4/
The files belonging to this database system will be owned by user "pgsql".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
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 /data01/pgsql/data_15.4 ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... Asia/Shanghai
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
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 /data01/pgsql/data_15.4/ -l logfile start

不要启动新库

3、关闭旧库,进行升级检查

关闭旧库:

$ pg_ctl stop
waiting for server to shut down.... done
server stopped

设置环境变量:

export PGDATAOLD=/data01/pgsql/data
export PGDATANEW=/data01/pgsql/data_15.4
export PGBINOLD=/home/pgsql/postgresql/bin
export PGBINNEW=/home/pgsql/postgresql-15.4/bin

进行升级检查:

$ /home/pgsql/postgresql-15.4/bin/pg_upgrade -d $PGDATAOLD -D $PGDATANEW -b $PGBINOLD -B $PGBINNEW -c
Performing Consistency Checks
-----------------------------
Checking cluster versions                                   ok
Checking database user is the install user                  ok
Checking database connection settings                       ok
Checking for prepared transactions                          ok
Checking for system-defined composite types in user tables  ok
Checking for reg* data types in user tables                 ok
Checking for contrib/isn with bigint-passing mismatch       ok
Checking for user-defined encoding conversions              ok
Checking for user-defined postfix operators                 ok
Checking for incompatible polymorphic functions             ok
Checking for tables WITH OIDS                               ok
Checking for invalid "sql_identifier" user columns          ok
Checking for presence of required libraries                 ok
Checking database user is the install user                  ok
Checking for prepared transactions                          ok
Checking for new cluster tablespace directories             ok

*Clusters are compatible*

像上面一样输出,就说明没有问题,可以继续
4、报错处理
检查时会遇到一些报错,常见以下问题

$/home/pgsql/postgresql-15.4/bin/pg_upgrade -d $PGDATAOLD -D $PGDATANEW -b $PGBINOLD -B $PGBINNEW -c
/home/pgsql/postgresql-15.4/bin/postgres: error while loading shared libraries: libzstd.so.1: cannot open shared object file: No such file or directory
no data was returned by command ""/home/pgsql/postgresql-15.4/bin/postgres" -V"

check for "/home/pgsql/postgresql-15.4/bin/postgres" failed: cannot execute
Failure, exiting

高版本pg需要依赖libzstd 去Root下安装即可 yum install libzstd -y

$ pg_upgrade -d $PGDATAOLD -D $PGDATANEW -b $PGBINOLD -B $PGBINNEW -c
Performing Consistency Checks
-----------------------------
Checking cluster versions
New cluster data and binary directories are from different major versions.
Failure, exiting

此报错原因是pg_upgrade 的命令使用的是环境变量中的,也就是旧版本的命令,需要使用高版本的pg_upgrade 命令

5、升级数据库

$ /home/pgsql/postgresql-15.4/bin/pg_upgrade -d $PGDATAOLD -D $PGDATANEW -b $PGBINOLD -B $PGBINNEW
Performing Consistency Checks
-----------------------------
Checking cluster versions                                   ok
Checking database user is the install user                  ok
Checking database connection settings                       ok
Checking for prepared transactions                          ok
Checking for system-defined composite types in user tables  ok
Checking for reg* data types in user tables                 ok
Checking for contrib/isn with bigint-passing mismatch       ok
Checking for user-defined encoding conversions              ok
Checking for user-defined postfix operators                 ok
Checking for incompatible polymorphic functions             ok
Checking for tables WITH OIDS                               ok
Checking for invalid "sql_identifier" user columns          ok
Creating dump of global objects                             ok
Creating dump of database schemas
                                                            ok
Checking for presence of required libraries                 ok
Checking database user is the install user                  ok
Checking for prepared transactions                          ok
Checking for new cluster tablespace directories             ok

If pg_upgrade fails after this point, you must re-initdb the
new cluster before continuing.

Performing Upgrade
------------------
Analyzing all rows in the new cluster                       ok
Freezing all rows in the new cluster                        ok
Deleting files from new pg_xact                             ok
Copying old pg_xact to new server                           ok
Setting oldest XID for new cluster                          ok
Setting next transaction ID and epoch for new cluster       ok
Deleting files from new pg_multixact/offsets                ok
Copying old pg_multixact/offsets to new server              ok
Deleting files from new pg_multixact/members                ok
Copying old pg_multixact/members to new server              ok
Setting next multixact ID and offset for new cluster        ok
Resetting WAL archives                                      ok
Setting frozenxid and minmxid counters in new cluster       ok
Restoring global objects in the new cluster                 ok
Restoring database schemas in the new cluster
                                                            ok
Copying user relation files
                                                            ok
Setting next OID for new cluster                            ok
Sync data directory to disk                                 ok
Creating script to delete old cluster                       ok
Checking for extension updates                              ok

Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade.
Once you start the new server, consider running:
    /home/pgsql/postgresql-15.4/bin/vacuumdb --all --analyze-in-stages

Running this script will delete the old cluster's data files:
    ./delete_old_cluster.sh

启动新库:

$ pwd
/home/pgsql/postgresql-15.4/bin

$ ./pg_ctl start -D  /data01/pgsql/data_15.4
waiting for server to start....2023-09-04 15:32:44.100 CST [5963] LOG:  starting PostgreSQL 15.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
2023-09-04 15:32:44.100 CST [5963] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2023-09-04 15:32:44.101 CST [5963] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2023-09-04 15:32:44.104 CST [5966] LOG:  database system was shut down at 2023-09-04 15:29:43 CST
2023-09-04 15:32:44.107 CST [5963] LOG:  database system is ready to accept connections
 done
server started

复制旧库配置文件

$ cp /data01/pgsql/data/postgresql.conf /data01/pgsql/data_15.4/postgresql.base.conf
$ cp /data01/pgsql/data/pg_hba.conf /data01/pgsql/data_15.4/pg_hba.conf

$ vi /data01/pgsql/data_15.4/postgresql.conf
最下面添加内容:
include postgresql.base.conf

设置环境变量,检查新库数据

$ export LD_LIBRARY_PATH=/home/pgsql/postgresql-15.4/lib/
$ ./psql
psql (15.4)
Type "help" for help.

postgres=# \d
         List of relations
 Schema |   Name   | Type  | Owner
--------+----------+-------+-------
 public | gwtest01 | table | pgsql
 public | gwtest02 | table | pgsql
 public | gwtest03 | table | pgsql
(3 rows)

postgres=# select * from gwtest01 ;
 id | name
----+------
  1 | aaaa
  1 | aaaa
  1 | aaaa
(3 rows)

更新新库的统计信息

$ /home/pgsql/postgresql-15.4/bin/vacuumdb --all --analyze-in-stages
vacuumdb: processing database "gwtest": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "postgres": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "template1": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "gwtest": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "postgres": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "template1": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "gwtest": Generating default (full) optimizer statistics
vacuumdb: processing database "postgres": Generating default (full) optimizer statistics
vacuumdb: processing database "template1": Generating default (full) optimizer statistics

此时,新库数据库已经升级完成

重新适配ambari平台

1、将旧库备份或删除

数据盘盘够推荐先改名,不要删除

$ mv postgresql postgresql_bak
$ mv /data01/pgsql/data /data01/pgsql/data_bak

2、关闭新库,创建软链

$ ./postgresql-15.4/bin/pg_ctl stop  -D  /data01/pgsql/data_15.4
waiting for server to shut down....2023-09-04 17:35:13.470 CST [5963] LOG:  received fast shutdown request
2023-09-04 17:35:13.471 CST [5963] LOG:  aborting any active transactions
2023-09-04 17:35:13.473 CST [5963] LOG:  background worker "logical replication launcher" (PID 5969) exited with exit code 1
2023-09-04 17:35:13.473 CST [5964] LOG:  shutting down
2023-09-04 17:35:13.474 CST [5964] LOG:  checkpoint starting: shutdown immediate
2023-09-04 17:35:13.476 CST [5964] LOG:  checkpoint complete: wrote 0 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.001 s, sync=0.001 s, total=0.004 s; sync files=0, longest=0.000 s, average=0.000 s; distance=0 kB, estimate=1931 kB
2023-09-04 17:35:13.484 CST [5963] LOG:  database system is shut down
 done
server stopped

设置软链:

[pgsql@gwtest01 ~]$ ln -s postgresql-15.4/ postgresql
[pgsql@gwtest01 ~]$ cd /data01/pgsql/
[pgsql@gwtest01 pgsql]$ pwd
/data01/pgsql
[pgsql@gwtest01 pgsql]$ ls
data_15.4  data_bak
[pgsql@gwtest01 pgsql]$ ln -s data_15.4/ data

后面就可以原环境变量直接启动使用

[pgsql@gwtest01 pgsql]$ pg_ctl  start
waiting for server to start....2023-09-04 17:37:37.972 CST [14380] LOG:  starting PostgreSQL 15.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
2023-09-04 17:37:37.973 CST [14380] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2023-09-04 17:37:37.974 CST [14380] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2023-09-04 17:37:37.976 CST [14383] LOG:  database system was shut down at 2023-09-04 17:35:13 CST
2023-09-04 17:37:37.980 CST [14380] LOG:  database system is ready to accept connections
 done
server started

[pgsql@gwtest01 pgsql]$ psql
psql (15.4)
Type "help" for help.

postgres=#

相关推荐

  1. PostgreSQL进行版本升级

    2024-05-16 13:46:08       13 阅读
  2. Django、Python版本升级问题汇总

    2024-05-16 13:46:08       41 阅读
  3. OpenSSL升级版本

    2024-05-16 13:46:08       35 阅读
  4. nginx升级版本

    2024-05-16 13:46:08       22 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-16 13:46:08       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-16 13:46:08       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-16 13:46:08       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-16 13:46:08       20 阅读

热门阅读

  1. 什么是Docker容器的基础镜像

    2024-05-16 13:46:08       11 阅读
  2. 2024年第四届长三角高校数学建模竞赛C题思路

    2024-05-16 13:46:08       11 阅读
  3. 常见递归问题

    2024-05-16 13:46:08       10 阅读
  4. css基础之定位、元素的显示与隐藏

    2024-05-16 13:46:08       12 阅读
  5. C++虚函数的使用

    2024-05-16 13:46:08       11 阅读
  6. 如何在MATALB中调用libMR

    2024-05-16 13:46:08       10 阅读
  7. 俄罗斯方块【python,小游戏】

    2024-05-16 13:46:08       10 阅读
  8. Prometheus 服务发现 添加标签

    2024-05-16 13:46:08       13 阅读