当前位置: 首页 > 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,备注:水煮数据库, 我拉你入群。
----------------------------------------------------------------------

   

相关文章:

PG如何实现跨大版本升级

数据库进行升级&#xff0c;是一个再正常不过的功能&#xff0c;比如功能的需要&#xff0c;遇到BUG&#xff0c;安全漏洞等等&#xff0c;具体升级包含子版本升级&#xff0c;主版本升级。如果用过ORACLE的朋友&#xff0c;一定知道&#xff0c;在ORACLE中&#xff0c;如果要实…...

JDK 8 升级 17 及 springboot 2.x 升级 3.x 指南

JDK 8 升级 17 简介 从 JDK 8 升级到 JDK 17 的过程中&#xff0c;有几个主要的变化&#xff0c;特别是 Java Platform Module System (JPMS) 的引入&#xff0c;以及一些包路径的调整。以下是与 JDK 17 相关的一些重要变化&#xff1a; Java Platform Module System (JPMS) …...

基于java的人居环境整治管理系统(源码+lw+部署文档+讲解等)

前言 &#x1f497;博主介绍&#xff1a;✌全网粉丝20W,CSDN特邀作者、博客专家、CSDN新星计划导师、全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战✌&#x1f497; &#x1f447;&#x1f3fb;…...

深入了解Pip:Python包管理器的详细指南

目录 Pip简介Pip的安装与升级Pip的基本使用 安装包卸载包列出已安装的包查看包的信息 管理依赖 使用requirements.txt冻结当前环境的包 Pip进阶用法 安装特定版本的包使用代理安装包从本地文件安装包 创建和发布Python包 创建一个Python包编写setup.py文件发布到PyPI 常见问题…...

Corsearch 用 ClickHouse 替换 MySQL 进行内容和品牌保护

本文字数&#xff1a;3357&#xff1b;估计阅读时间&#xff1a;9 分钟 作者&#xff1a;ClickHouse Team 本文在公众号【ClickHouseInc】首发 Chase Richards 自 2011 年在初创公司 Marketly 担任工程负责人&#xff0c;直到 2020 年公司被收购。他现在是品牌保护公司 Corsear…...

常见的应急救援设备有哪些_鼎跃安全

在我们的生活中&#xff0c;应急事件的发生常常是突如其来的&#xff0c;它们对人民的生命财产安全构成重大威胁&#xff0c;同时也对社会稳定提出严峻挑战。在这样的紧急情况下&#xff0c;迅速开展有效的救援工作显得尤为重要。而在整个救援过程中&#xff0c;应急设备的使用…...

Vue 项目部署后首页白屏问题排查与解决

引言 在部署 Vue.js 项目时&#xff0c;有时会遇到首页加载后出现白屏的情况&#xff0c;这可能是由于多种原因造成的。本文将介绍一些常见的排查方法和解决方案&#xff0c;帮助开发者快速定位问题并解决。 1. 常见原因分析 首页白屏的问题可能由以下几个方面的原因导致&am…...

STM32 定时器移相任意角度和占空比,频率可调

由于使用了中断修改翻转的CCR值&#xff0c;对于频率超250K以上不太适用. void Motor1_Init(Motor MotorChValue) { GPIO_InitTypeDef GPIO_InitStructure;TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;TIM_OCInitTypeDef TIM_OCInitStructure;NVIC_InitTypeDef NVIC_Ini…...

C++ 与其他编程语言区别_C++11/14/17新特性总结

C11 decltype类型推导 decltype不依赖于初始化&#xff0c;根据表达式类推导类型 auto b &#xff1a;根据右边a的初始值来推导出变量的类型&#xff0c;然后将该初始值赋给bdecltype 则是根据a表达式来推导类型&#xff0c;变量的初始值与表达式的值无关表达式类型注意点&…...

玩转云服务:Google Cloud谷歌云永久免费云服务器「白嫖」 指南

前几天&#xff0c;和大家分享了&#xff1a; 玩转云服务&#xff1a;Oracle Cloud甲骨文永久免费云服务器注册及配置指南 相信很多同学都卡在了这一步&#xff1a; 可用性域 AD-1 中配置 VM.Standard.E2.1.Micro 的容量不足。请在其他可用性域中创建实例&#xff0c;或稍后…...

用18讲必看:宇哥亲划重点内容+核心题总结

25考研结束之后&#xff0c;张宇老师的风评可能会两极分化 其中一波把张宇老师奉为考研数学之神&#xff0c;吹捧「三向解题法」天下无敌。 另外一波对张宇老师的评价负面&#xff0c;在网上黑张宇老师&#xff01; 为什么会这么说&#xff0c;因为张宇老师的新版36讲争议太…...

什么是安全生产痕迹化管理?如何做到生产过程中全程痕迹化管理?

安全生产痕迹化管理&#xff0c;简单来说&#xff0c;就是通过记录一些“信息”来确保安全工作的进展。这些方法包括记会议内容、写安全日记、拍照片、签字盖章、指纹识别、面部识别还有手机定位等。记录下来的文字、图片、数据和视频&#xff0c;就像一个个“脚印”&#xff0…...

VIsual Studio:为同一解决方案下多个项目分别指定不同的编译器

一、引言 如上图&#xff0c;我有一个解决方案【EtchDevice】&#xff0c;他包含两个&#xff08;甚至更多个&#xff09;子项目&#xff0c;分别是【DeviceRT】和【DeviceWin】&#xff0c;见名知意&#xff0c;我需要一个项目编译运行在RTOS上&#xff0c;譬如一个名叫INTime…...

Flat Ads资讯:Meta、Google、TikTok 7月产品政策速递

Flat Ads拥有全球媒介采买(MediaBuy)业务,为方便广告主及时了解大媒体最新政策,Flat Ads将整理大媒体产品更新月报,欢迎大家关注我们及时了解最新行业动向。 一、Meta 1、Reels 应用推广现可突出显示应用评分、点评和下载量 为了不断优化 Instagram 上的广告体验和广告表现,…...

嵌入式C++、ROS 、OpenCV、SLAM 算法和路径规划算法:自主导航的移动机器人流程设计(代码示例)

在当今科技迅速发展的背景下&#xff0c;嵌入式自主移动机器人以其广泛的应用前景和技术挑战吸引了越来越多的研究者和开发者。本文将详细介绍一个嵌入式自主移动机器人项目&#xff0c;涵盖其硬件与软件系统设计、代码实现及项目总结&#xff0c;并提供相关参考文献。 项目概…...

数据安全堡垒:SQL Server数据库备份验证与测试恢复全攻略

数据安全堡垒&#xff1a;SQL Server数据库备份验证与测试恢复全攻略 在数据库管理中&#xff0c;备份是确保数据安全的关键环节&#xff0c;但仅仅拥有备份是不够的&#xff0c;验证备份的有效性并能够从备份中成功恢复数据同样重要。SQL Server提供了一系列的工具和方法来执…...

嵌入式人工智能(40-基于树莓派4B的水滴传感器和火焰传感器)

虽然这两个传感器水火不容&#xff0c;我还是把他们放到一起了。本文是有线传感器的最后一个部分了。后面如果还有文章介绍有线传感器&#xff0c;也是补充学习其他内容不得已而为之。如果不是&#xff0c;就当我没说&#xff0c;哈哈。 1、水滴传感器 水滴传感器又称雨滴传感…...

EF访问PostgreSql,如何判断jsonb类型的数组是否包含某个数值

下面代码判断OpenUserIds&#xff08;long[]类型的jsonb&#xff09;字段&#xff0c;是否包含 8 basequery basequery.Where(m > Microsoft.EntityFrameworkCore.NpgsqlJsonDbFunctionsExtensions.JsonContains(EF.Functions, m.OpenUserIds, new long[] { 8 }));...

Qt 实战(8)控件 | 8.1、QComboBox

文章目录 一、QComboBox1、简介2、功能特性2.1、添加和移除项目2.2、设置和获取当前选中项2.3、模型/视图架构2.4、信号与槽 3、总结 前言&#xff1a; QComboBox 是 Qt 框架中一个非常实用的控件&#xff0c;它允许用户从一个下拉列表中选择一个项目。这个控件广泛应用于需要用…...

模拟算法概览

前言 LeetCode上的模拟算法题目主要考察通过直接模拟问题的实际操作和过程来解决问题。这类题目通常不需要高级的数据结构或复杂的算法&#xff0c;而是通过仔细的逻辑和清晰的步骤逐步解决。 适合解决的问题 模拟算法适合用来解决那些逻辑明确、步骤清晰且可以逐步执行的问…...

黑马Mybatis

Mybatis 表现层&#xff1a;页面展示 业务层&#xff1a;逻辑处理 持久层&#xff1a;持久数据化保存 在这里插入图片描述 Mybatis快速入门 ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/6501c2109c4442118ceb6014725e48e4.png //logback.xml <?xml ver…...

云启出海,智联未来|阿里云网络「企业出海」系列客户沙龙上海站圆满落地

借阿里云中企出海大会的东风&#xff0c;以**「云启出海&#xff0c;智联未来&#xff5c;打造安全可靠的出海云网络引擎」为主题的阿里云企业出海客户沙龙云网络&安全专场于5.28日下午在上海顺利举办&#xff0c;现场吸引了来自携程、小红书、米哈游、哔哩哔哩、波克城市、…...

MMaDA: Multimodal Large Diffusion Language Models

CODE &#xff1a; https://github.com/Gen-Verse/MMaDA Abstract 我们介绍了一种新型的多模态扩散基础模型MMaDA&#xff0c;它被设计用于在文本推理、多模态理解和文本到图像生成等不同领域实现卓越的性能。该方法的特点是三个关键创新:(i) MMaDA采用统一的扩散架构&#xf…...

华为OD机试-食堂供餐-二分法

import java.util.Arrays; import java.util.Scanner;public class DemoTest3 {public static void main(String[] args) {Scanner in new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextLine()) { // 注意 while 处理多个 caseint a in.nextIn…...

Psychopy音频的使用

Psychopy音频的使用 本文主要解决以下问题&#xff1a; 指定音频引擎与设备&#xff1b;播放音频文件 本文所使用的环境&#xff1a; Python3.10 numpy2.2.6 psychopy2025.1.1 psychtoolbox3.0.19.14 一、音频配置 Psychopy文档链接为Sound - for audio playback — Psy…...

【配置 YOLOX 用于按目录分类的图片数据集】

现在的图标点选越来越多&#xff0c;如何一步解决&#xff0c;采用 YOLOX 目标检测模式则可以轻松解决 要在 YOLOX 中使用按目录分类的图片数据集&#xff08;每个目录代表一个类别&#xff0c;目录下是该类别的所有图片&#xff09;&#xff0c;你需要进行以下配置步骤&#x…...

Linux-07 ubuntu 的 chrome 启动不了

文章目录 问题原因解决步骤一、卸载旧版chrome二、重新安装chorme三、启动不了&#xff0c;报错如下四、启动不了&#xff0c;解决如下 总结 问题原因 在应用中可以看到chrome&#xff0c;但是打不开(说明&#xff1a;原来的ubuntu系统出问题了&#xff0c;这个是备用的硬盘&a…...

力扣-35.搜索插入位置

题目描述 给定一个排序数组和一个目标值&#xff0c;在数组中找到目标值&#xff0c;并返回其索引。如果目标值不存在于数组中&#xff0c;返回它将会被按顺序插入的位置。 请必须使用时间复杂度为 O(log n) 的算法。 class Solution {public int searchInsert(int[] nums, …...

浪潮交换机配置track检测实现高速公路收费网络主备切换NQA

浪潮交换机track配置 项目背景高速网络拓扑网络情况分析通信线路收费网络路由 收费汇聚交换机相应配置收费汇聚track配置 项目背景 在实施省内一条高速公路时遇到的需求&#xff0c;本次涉及的主要是收费汇聚交换机的配置&#xff0c;浪潮网络设备在高速项目很少&#xff0c;通…...

C++课设:简易日历程序(支持传统节假日 + 二十四节气 + 个人纪念日管理)

名人说:路漫漫其修远兮,吾将上下而求索。—— 屈原《离骚》 创作者:Code_流苏(CSDN)(一个喜欢古诗词和编程的Coder😊) 专栏介绍:《编程项目实战》 目录 一、为什么要开发一个日历程序?1. 深入理解时间算法2. 练习面向对象设计3. 学习数据结构应用二、核心算法深度解析…...