当前位置: 首页 > news >正文

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

查看丢失的库是什么

[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
 

再次检查,OK。

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.结果验证

到此,PG_UPGRADE 升级就完成了。

但感觉PG_UPGRADE还是有一些遗憾,

1.整个升级,都是命令行完成,没有图形界面,不太直观

2.升级过程中,数据文件路径要变化,不能就数据文件原地升级,也许是强迫症吧。

10、联系我们

如果你还想和我交流,可以按如下方式找到我们。

----------------------------------------------------------------------
微信群:水煮数据库

主要交流日常运维中用到的数据库相关问题,包含但不限于:ORACLE,PG,MYSQL,SQLSERVER,OB,TIDB,达梦,TDSQL,OPENGAUSS,人大金仓,GBASE等等,

加我微信:zq24803366,备注:水煮数据库, 我拉你入群。
----------------------------------------------------------------------

   

http://www.lryc.cn/news/411871.html

相关文章:

  • JDK 8 升级 17 及 springboot 2.x 升级 3.x 指南
  • 基于java的人居环境整治管理系统(源码+lw+部署文档+讲解等)
  • 深入了解Pip:Python包管理器的详细指南
  • Corsearch 用 ClickHouse 替换 MySQL 进行内容和品牌保护
  • 常见的应急救援设备有哪些_鼎跃安全
  • Vue 项目部署后首页白屏问题排查与解决
  • STM32 定时器移相任意角度和占空比,频率可调
  • C++ 与其他编程语言区别_C++11/14/17新特性总结
  • 玩转云服务:Google Cloud谷歌云永久免费云服务器「白嫖」 指南
  • 用18讲必看:宇哥亲划重点内容+核心题总结
  • 什么是安全生产痕迹化管理?如何做到生产过程中全程痕迹化管理?
  • VIsual Studio:为同一解决方案下多个项目分别指定不同的编译器
  • Flat Ads资讯:Meta、Google、TikTok 7月产品政策速递
  • 嵌入式C++、ROS 、OpenCV、SLAM 算法和路径规划算法:自主导航的移动机器人流程设计(代码示例)
  • 数据安全堡垒:SQL Server数据库备份验证与测试恢复全攻略
  • 嵌入式人工智能(40-基于树莓派4B的水滴传感器和火焰传感器)
  • EF访问PostgreSql,如何判断jsonb类型的数组是否包含某个数值
  • Qt 实战(8)控件 | 8.1、QComboBox
  • 模拟算法概览
  • uniapp手写滚动选择器
  • 智慧医院临床检验管理系统源码(LIS),全套LIS系统源码交付,商业源码,自主版权,支持二次开发
  • 超市是怎样高效完成客流统计与客流分析
  • 进程地址空间,零基础最最最详解
  • 全面解锁:通过JSP和Ajax实现钉钉签到数据展示及部门筛选功能
  • LLM应用-prompt提示:让大模型总结生成PPT
  • 安全防护软件的必要性:从微软蓝屏事件谈起
  • 解开基于大模型的Text2SQL的神秘面纱
  • 对象转化成base64-再转回对象
  • vue运行或打包报错 “‘node --max-old-space-size=10240“‘ 不是内部或外部命令
  • 反爬虫限制:有哪些方法可以保护网络爬虫不被限制?