PG如何实现跨大版本升级

news2024/9/20 17:01:21

数据库进行升级,是一个再正常不过的功能,比如功能的需要,遇到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.coloradmin.cn/o/1967658.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

dp:221. 最大正方形

221. 最大正方形 看到这个题目真能立马想到dp吗&#xff1f;貌似很难&#xff0c;即使知道是一个dp题也很难想到解法。 直观来看&#xff0c;使用bfs以一个点为中点进行遍历&#xff0c;需要的时间复杂度为 O ( n 2 m 2 ) O(n^2m^2) O(n2m2) 但是可以很容易发现&#xff0c;…

昂科烧录器支持MindMotion灵动微电子的32位微控制器MM32SPIN360C

芯片烧录行业领导者-昂科技术近日发布最新的烧录软件更新及新增支持的芯片型号列表&#xff0c;其中MindMotion灵动微电子的32位微控制器MM32SPIN360C已经被昂科的通用烧录平台AP8000所支持。 MM32SPIN360C使用高性能的ARM CortexM0为内核的32位微控制器&#xff0c;5V输出的L…

九大原则,轻松构建个人高效SOP

1、原则一、工作汇报SOP SCQA模型(升职加薪的关键!&#xff09; 清晰定义问题和提出解决方案 类别 关键词 解读 S - Situation 情景 陈述项目背景&#xff0c;目标&#xff0c;愿景 C - Complication 冲突 讲卡点&#xff0c;讲冲突 Q - Question 疑问-问题 这些冲…

vue2 封装插槽组件

安装 element-ui npm install element-ui --save ---force main.js 导入 import Vue from vue; import ElementUI from element-ui; import element-ui/lib/theme-chalk/index.css; import App from ./App.vue; Vue.use(ElementUI); new Vue({ el: #app, render: h > h(Ap…

未来RPA财税的发展前景

近年来&#xff0c;全球数字化进程持续提速&#xff0c;越来越多企业受到效率及运营成本的压力&#xff0c;正努力寻求企业增长发展的新路径&#xff0c;而财务作为企业战略的“大脑”&#xff0c;成为企业数字化转型的重要突破口。RPA技术由于能够自动化各种重复性和繁琐的任务…

如何利用智能电子工牌提高酒店员工的服务技能和沟通效率?

智能电子语音工牌的基本功能 智能电子录音工牌通常集成了录音、语音识别、数据上传等功能&#xff0c;能够在员工与客户互动的过程中实时录制音频&#xff0c;并将数据上传到云端服务器。这些工牌还可能配备定位功能&#xff0c;以便追踪员工的位置和服务轨迹。通过语音识别技…

职场里有3个错觉,如果正好说中你,抓紧醒过来

1.没有我肯定不行。 其实大多数公司里的绝大多数人都可以被替代&#xff0c;无非是在个体层面替代的成本高一点、低一点罢了。 所以千万别高估自己&#xff0c;否则会造成极其严重的误判。 我之前的公司有一个部门&#xff0c;这里面有个员工&#xff0c;专业对口&#xff0…

vue-cli搭建项目笔记

1. 在指定位置打开终端 2. 运行 指令 vue create xtx选择 vue2 等待创建完成。。。。。 大概5.52s完成 3.启动项目 进入 项目 目录 cd xtx 启动 yarn run serve 4. 访问 浏览器 访问 localhost:8080 5. 项目开发 清空项目 App.vue 注意&#xff1a;每一个vue组件中的…

柯桥零基础学韩语韩语俗语韩语日常口语培训

반모반말모드 반말모드 非敬语模式 존모존댓말모드 존댓말모드 敬语模式 2 비담 비담비주얼담당 비주얼 담당, 意思就是颜值担当。 3 보배 보배보조배터리15857575376# 随身充、充电宝 如果单讲单词&#xff0c;是宝贝的意思。网络用语角度理解的话就是充电宝 4 삼귀다…

这4个思维导图软件剪辑就是整理思维的魔法工具。

思维导图作为一个能够帮助我们呈现可视化内容&#xff0c;激发创意以及提高我们记忆力的工具&#xff1b;正在被越来越多的人使用。然而选择一款适合自己的脑图软件&#xff0c;会为我们的学习、工作和生活带来很大的便利&#xff0c;如果你依旧还在寻找的话&#xff0c;不妨看…

SVM解决线性不可分问题

在一个棋盘上杂糅的摆放着黑白两种棋子&#xff0c;要求我们以最快的速度将它们各自分开&#xff0c;这时我们应该如何做呢&#xff1f;也许喜欢金庸武侠的小伙伴已经想到了答案。 假如你是一位拥有深厚内力的大侠&#xff0c;你直接可以拍盘而起&#xff0c;让棋子们飞起来&am…

secExample靶场之java反序列化漏洞复现

我是使用kali虚拟机搭建的靶场&#xff0c;具体搭建步骤可以看我之前的博客内容。 访问靶机地址&#xff0c;打开java反序列化的 点进去后是如图的页面&#xff0c;随便输入一信息。 可以看到敏感信息直接显示在了页面上。 访问192.168.189.141:8080/cors1&#xff0c;可以看到…

揭秘律师界的“超能力”:AI如何让繁琐工作变轻松?

律师朋友们&#xff0c;你们是否也曾为文书堆成山、法规更新追不上、案例分析耗时久而头疼&#xff1f;我懂你们的痛&#xff0c;毕竟&#xff0c;咱也是从那个“人肉检索机”时代走过来的。不过&#xff0c;今天我要分享一个秘密武器——AI工具&#xff0c;它们真的能帮咱们把…

由浅入深的了解进程(3)

进程状态 进程状态1、Linux中的进程状态1、1、进程状态R和S1、2、进程状态T/t1、3、进程状态D1、4、进程状态X和Z 2、僵尸进程3、孤儿进程4、bash概括5、进程的阻塞&#xff0c;挂起和运行5、1、运行5、2、阻塞5、3、挂起 进程状态 任何一个进程都要有自己的代码和数据一样&am…

CSS雷达光波效果(前端雷达光波效果)

前言 CSS雷达光波效果是一种视觉动画效果&#xff0c;常用于模仿雷达扫描或检测的视觉反馈。这种效果通常涉及到动态的圆形或弧形图案&#xff0c;它们从一个中心点向外扩散&#xff0c;类似于水面上的涟漪或雷达扫描线。以下是创建CSS雷达光波效果的一些关键技术和步骤&#…

[C++] 容器适配器:深入理解Stack与Queue的底层原理

文章目录 容器适配器简介deque的缺陷为什么使用deque作为stack和queue的底层默认容器 stack和queue的简单讲解Stack&#xff08;栈&#xff09;栈的操作图示栈的相关接口 Queue&#xff08;队列&#xff09; Stack和Queue的模拟实现Stack&#xff08;栈&#xff09;作为容器适配…

新时代的北斗时空智能:助力节能减排,共筑低碳未来

随着全球环境问题日益加剧&#xff0c;节能减排和低碳生活成为了全世界共同的目标。中国作为一个崛起中的大国&#xff0c;在这一领域肩负着重要的责任。近年来&#xff0c;中国的北斗卫星导航系统&#xff08;BDS&#xff09;异军突起&#xff0c;其时空智能技术在多个方面特别…

盘点国内ERP系统,看看你都用过哪一款?

在数字化转型的浪潮中,企业资源计划(ERP)系统已成为企业提升管理效率、优化资源配置的关键工具。国内市场上,众多优秀的ERP软件产品如雨后春笋般涌现,为企业提供了丰富多样的选择。 本文将为您深度盘点10款国产高端ERP软件,重点围绕公司背景、专业实力、产品核心能力、解…

【机器学习】逻辑损失函数的基本概念和探索为什么平方误差损失函数不适用于逻辑回归以及探索逻辑损失函数

引言 在机器学习中&#xff0c;逻辑损失函数&#xff08;Logistic Loss Function&#xff09;是用于训练逻辑回归模型的一种损失函数。逻辑回归是一种广泛用于分类问题的模型&#xff0c;特别是用于二分类问题&#xff0c;它的目标是预测一个实例属于两个类别中的一个的概率 文…

经验模态分解 (EMD) 及其在信号降噪中的应用

引言 在信号处理领域&#xff0c;处理非线性和非平稳信号是一个重要的挑战。传统的信号处理方法&#xff0c;如傅里叶变换和小波变换&#xff0c;虽然在处理线性和稳态信号方面表现出色&#xff0c;但在面对复杂信号时往往力不从心。经验模态分解 (Empirical Mode Decompositi…