PG如何实现跨大版本升级

数据库进行升级,是一个再正常不过的功能,比如功能的需要,遇到BUG,安全漏洞等等,具体升级包含子版本升级,主版本升级。如果用过ORACLE的朋友,一定知道,在ORACLE中,如果要实现大版本的升级,比如从11g,升级到19c,一般使用的dbua来完成,并且标准的升级,一定会有数据库停机时间,小版本的升级,如19.3,升级到19.24 ,使用OPATCH 工具来完成,一样需要停数据库。

那对于PG,一样也存在升级,如果是小版本升级,比如12.0 升级到12.19 ,直接使用二进制文件替换即可,如果是大版本升级,一般使用 pg_upgrade来完成,当然,如果时间允许,也可以使用pg_dumpall也行。只是时间要长一些。

关于PG_UPGRADE的使用,可以参见官方文档:PostgreSQL: Documentation: 16: pg_upgrade

部分截图如下:

PG_UPGRADE 的用法:

pg_upgrade -b oldbindir [-B newbindir ] -d oldconfigdir -D newconfigdir [option...]

目前pg_upgrade 支持从 9.2.x 到目前最新的版本,甚至BETA版本。

下面我们以一个升级案例来说明:

目录

1.升级环境

2.源码安装新版本16.3

3.关闭源和目标数据库

4.执行升级检查

5.正式升级

6.修改环境变量

7.修改初始参数

8.启动PG并调整PG_UPGRADE要求的脚本

9.结果验证

10、联系我们


1.升级环境

源版本:pg 12.0

PGHOME=/u01/app/postgres/product/12.0/dbhome_1

PGDATA=/u01/app/postgres/product/pgdata_12

PORT=5432

目标版本:pg 16.3

PGHOME=/u01/app/postgres/product/16.3/dbhome_1

PGDATA=/u01/app/postgres/product/pgdata

PORT=5432

操作系统:OpenEuler 24.03

sztechdb=# select * from version();

version


PostgreSQL 12.0 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 12.3.1 (openEuler 12.3.1-30.oe2403), 64-bit

(1 row)

2.源码安装新版本16.3

这个步骤,可以参见前面的文档:https://blog.csdn.net/cqsztech/article/details/140748640

如果源端有额外的插件,需要安装

这里安装,只需要到 initdb即可,不需要打开

postgres@openeuler2403 bin]$ ./initdb --pgdata=/u01/app/postgres/pgdata -U postgres

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 "zh_CN.UTF-8".

The default database encoding has accordingly been set to "UTF8".

initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"

The default text search configuration will be set to "simple".

Data page checksums are disabled.

fixing permissions on existing directory /u01/app/postgres/pgdata ... 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/Shanghai

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 /u01/app/postgres/pgdata -l logfile start

3.关闭源和目标数据库

把源和目标数据库都关闭。

postgres@openeuler2403 pgdata_12\]$ pg_ctl stop waiting for server to shut down.... done server stopped ### 4.执行升级检查 是否能升级,正式升级之前,可以先检查一下,有点试升级的味道。 注意,执行PG_UPGRADE命令,需要在高版本的环境下运行 先看看pg_upgrade 的用法: \[postgres@openeuler2403 bin\]$ pg_upgrade -c --old-datadir=/u01/app/postgres/pgdata_12 \\ \> --new-datadir=/u01/app/postgres/pgdata \\ \> --old-bindir=/u01/app/postgres/product/12.0/dbhome_1/bin \\ \> --new-bindir=/u01/app/postgres/product/16.3/dbhome_1/bin ![](https://i-blog.csdnimg.cn/direct/c8cdcb232bd6481cbf0fe51ede4be8dd.png) 查看丢失的库是什么 \[postgres@openeuler2403 pgdata_12\]$ cat /u01/app/postgres/pgdata/pg_upgrade_output.d/20240801T164259.299/loadable_libraries.txt could not load library "$libdir/pgcrypto": ERROR: could not access file "$libdir/pgcrypto": ûĿ¼ In database: postgres --暂时源端去掉这个插件 postgres=# drop extension pgcrypto; DROP EXTENSION ![](https://i-blog.csdnimg.cn/direct/4e0bb3ea7a0f45d3b3541aeb95df59f7.png) 再次检查,OK。 ![](https://i-blog.csdnimg.cn/direct/818941f11caf4a0f9743c321f2fc1528.png) ### 5.正式升级 \[postgres@openeuler2403 bin\]$ **pg_upgrade --old-datadir=/u01/app/postgres/pgdata_12 \\ \> --new-datadir=/u01/app/postgres/pgdata \\ \> --old-bindir=/u01/app/postgres/product/12.0/dbhome_1/bin \\ \> --new-bindir=/u01/app/postgres/product/16.3/dbhome_1/bin** 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 incompatible "aclitem" data type in user tables ok Checking for user-defined encoding conversions ok Checking for user-defined postfix operators ok Checking for incompatible polymorphic functions 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 ------------------ Setting locale and encoding for new cluster ok 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 notice Your installation contains extensions that should be updated with the ALTER EXTENSION command. The file update_extensions.sql when executed by psql by the database superuser will update these extensions. Upgrade Complete ---------------- Optimizer statistics are not transferred by pg_upgrade. Once you start the new server, consider running: /u01/app/postgres/product/16.3/dbhome_1/bin/vacuumdb --all --analyze-in-stages Running this script will delete the old cluster's data files: **./delete_old_cluster.sh** \[postgres@openeuler2403 bin\]$ ### 6.修改环境变量 \[postgres@openeuler2403 bin\]$ cat \~/.bash_profile # Source /root/.bashrc if user has one \[ -f \~/.bashrc \] \&\& . \~/.bashrc export PGPORT=5432 export PGDATA=/u01/app/postgres/pgdata export PGHOME=/u01/app/postgres/product/16.3/dbhome_1 export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH export PATH=$PGHOME/bin:$PATH:. export PGHOST=$PGDATA export PGUSER=postgres export PGDATABASE=postgres export PGCLIENTENCODING=GBK ### 7.修改初始参数 cat \>\> $PGDATA/postgresql.conf \<\< "EOF" listen_addresses = '\*' port=5432 unix_socket_directories='/u01/app/postgres/pgdata' logging_collector = on log_directory = 'pg_log' log_filename = 'postgresql-%a.log' log_truncate_on_rotation = on max_connections = 500 shared_buffers = 2GB EOF cat \>\> $PGDATA/pg_hba.conf \<\< "EOF" # TYPE DATABASE USER ADDRESS METHOD host all all 0.0.0.0/0 md5 EOF ### 8.启动PG并调整PG_UPGRADE要求的脚本 --插件更新 \[postgres@openeuler2403 pgdata\]$ psql -f /u01/app/postgres/product/16.3/dbhome_1/bin/update_extensions.sql You are now connected to database "postgres" as user "postgres". ALTER EXTENSION ALTER EXTENSION ALTER EXTENSION --收集统计信息 \[postgres@openeuler2403 pgdata\]$ /u01/app/postgres/product/16.3/dbhome_1/bin/vacuumdb --all --analyze-in-stages vacuumdb: processing database "postgres": Generating minimal optimizer statistics (1 target) vacuumdb: processing database "sztechdb": Generating minimal optimizer statistics (1 target) vacuumdb: processing database "template1": Generating minimal optimizer statistics (1 target) vacuumdb: processing database "postgres": Generating medium optimizer statistics (10 targets) vacuumdb: processing database "sztechdb": Generating medium optimizer statistics (10 targets) vacuumdb: processing database "template1": Generating medium optimizer statistics (10 targets) vacuumdb: processing database "postgres": Generating default (full) optimizer statistics vacuumdb: processing database "sztechdb": Generating default (full) optimizer statistics vacuumdb: processing database "template1": Generating default (full) optimizer statistics --删除老版本的文件 \[postgres@openeuler2403 pgdata\]$ cat /u01/app/postgres/product/16.3/dbhome_1/bin/delete_old_cluster.sh #!/bin/sh rm -rf '/u01/app/postgres/pgdata_12' rm -rf '/data/pgdata/PG_12_201909212' \[postgres@openeuler2403 pgdata\]$ /u01/app/postgres/product/16.3/dbhome_1/bin/delete_old_cluster.sh \[postgres@openeuler2403 pgdata\]$ ls -ltr /u01/app/postgres/ 总计 8 drwxrwxr-x. 4 postgres postgres 30 7月31日 17:53 product drwxr-xr-x. 2 postgres postgres 4096 7月31日 19:17 archive_log drwx------. 21 postgres postgres 4096 8月 1日 17:36 pgdata \[postgres@openeuler2403 pgdata\]$ ls -ltr /data/pgdata/ 总计 8 drwx------ 3 postgres postgres 4096 8月 1日 09:23 PG_16_202307071_bak drwx------ 3 postgres postgres 4096 8月 1日 17:27 PG_16_202307071 ### 9.结果验证 ![](https://i-blog.csdnimg.cn/direct/b675b9d7ccc84521a37c93e15f944167.png) ![](https://i-blog.csdnimg.cn/direct/c1d38473311245bab86d24f3da62dbad.png) ![](https://i-blog.csdnimg.cn/direct/092f0bd9339c440c9b1b80763844d4cf.png) ![](https://i-blog.csdnimg.cn/direct/b726ad4abcb44884b23a6fa249f96286.png) ![](https://i-blog.csdnimg.cn/direct/adaf610e796e498d8a0a5b6dbec2eeab.png) ![](https://i-blog.csdnimg.cn/direct/a05fd2f6f1434389a850a26f9cab73b4.png) ![](https://i-blog.csdnimg.cn/direct/6d8567832208487b9a44116b5f2b077c.png) 到此,PG_UPGRADE 升级就完成了。 但感觉PG_UPGRADE还是有一些遗憾, 1.整个升级,都是命令行完成,没有图形界面,不太直观 2.升级过程中,数据文件路径要变化,不能就数据文件原地升级,也许是强迫症吧。 ### 10、联系我们 如果你还想和我交流,可以按如下方式找到我们。 ---------------------------------------------------------------------- 微信群:水煮数据库 主要交流日常运维中用到的数据库相关问题,包含但不限于:ORACLE,PG,MYSQL,SQLSERVER,OB,TIDB,达梦,TDSQL,OPENGAUSS,人大金仓,GBASE等等, 加我微信:zq24803366,备注:水煮数据库, 我拉你入群。 ----------------------------------------------------------------------

相关推荐
计算机毕设定制辅导-无忧学长6 小时前
西门子 PLC 与 Modbus 集成:S7-1500 RTU/TCP 配置指南(一)
服务器·数据库·tcp/ip
程序员柳7 小时前
基于微信小程序的校园二手交易平台、微信小程序校园二手商城源代码+数据库+使用说明,layui+微信小程序+Spring Boot
数据库·微信小程序·layui
梦在深巷、7 小时前
MySQL/MariaDB数据库主从复制之基于二进制日志的方式
linux·数据库·mysql·mariadb
IT乌鸦坐飞机7 小时前
ansible部署数据库服务随机启动并创建用户和设置用户有完全权限
数据库·ansible·centos7
IT_10247 小时前
Spring Boot项目开发实战销售管理系统——数据库设计!
java·开发语言·数据库·spring boot·后端·oracle
祁思妙想9 小时前
八股学习(三)---MySQL
数据库·学习·mysql
惊骇世俗王某人9 小时前
1.MySQL之如何定位慢查询
数据库·mysql
秦歌66610 小时前
向量数据库-Milvus快速入门
数据库·milvus
Edingbrugh.南空11 小时前
Flink SQLServer CDC 环境配置与验证
数据库·sqlserver·flink
码不停蹄的玄黓11 小时前
MySQL分布式ID冲突详解:场景、原因与解决方案
数据库·分布式·mysql·id冲突