PostgreSQL通过pg_upgrade进行大版本升级

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介: PostgreSQL通过pg_upgrade进行大版本升级

 PostgreSQL自身有两种大版本升级(比如从pg9.2到pg9.4,可以跨一个或多个大版本)的方法:

  1.通过pg_upgrede升级.(该方法对数据量较大时,速度更快)

  2.先从老版本执行pg_dump,再到新版本执行pg_restore.(该方法在数据量较大时,速度较慢)

对于跨版本的PostgreSQL升级,官方给出了以下三种方法:

  • pg_dumpall--把数据从旧版本中导出,然后再导入到新版本,此过程就是一个导入导出的过程。
  • Logical Replication--创建一个高版本的从库,待数据同步完成后,主备角色互换,以达到升级目的。
  • pg_upgrade--官方推荐的快速升级方法,通过创建新的系统表并使用旧的用户表的方式进行升级。

本文通过pg_upgrade工具,快速将PostgreSQL 9.6升级到11.3。其中二进制软件包都是使用官方YUM源。

本文主要介绍使用pg_upgrade的方法、步骤以及相关的注意事项。

一、使用pg_upgrade升级的各步要求

1.移动旧的数据集簇目录(可选的)

如果使用的是基于版本的安装目录,比如:/opt/PostgreSQL/9.1(一般是图形化安装包安装生成目录),那就不需要移动旧的数据库集簇目录,因为图形化安装包都是使用基于版本的安装目录的。

如果使用的不是基于版本的安装目录,比如:/usr/local/pgsql(一般是源码编译安装生成的目录),那最好移动这个数据库集簇的安装目录,否则的话会干扰新数据库的安装。一旦关闭了当前PostgreSQL的服务,那么重命名漆安装目录就会很安全。假设旧的安装目录为:/usr/local/pgsql,可以通过以下命令来重命名该目录:

mv /usr/local/pgsql /usr/local/pgsql.old

2.源码安装一个新版本

用兼容旧数据库集簇的configure 选项,来构建新的PostgreSQL。在开始升级之前,pg_upgrade 将会检查pg_controldata 来确保所有的设置都兼容。

3.安装新的PostgreSQL二进制文件

安装新服务器的二进制可执行文件和支持文件。

用源码安装时,如果你想要在自定义目录安装新服务器,可以使用 prefix 变量:

gmake prefix=/usr/local/pgsql.new install

4.安装pg_upgrade和pg_upgrade_support

在新PostgreSQL的安装中,安装 pg_upgrade 二进制文件 and pg_upgrade_support库文件 。

5.初始化新的PostgreSQL集簇

通过initdb初始化新的集簇,并且,要用兼容旧数据库实例的initdb 选项。很多预构建安装会自动完成这一部。不需要启动新的数据库实例。

6.安装自定义的共享对象文件

安装所有旧数据库集簇用到的自定义共享对象文件(或者是DLL文件)到新的数据库集簇中, 例如 pgcrypto.so, 无论他们来自 contrib 或者其它的源。 不需要安装一类模式的定义, 例如pgcrypto.sql, 因为这些也会从旧数据库实例中升级。还有,所有旧数据库集簇中自定义的全文本搜索文件(dictionary, synonym, thesaurus, stop words) 必须拷贝到新的数据库集簇中。

7.调整认证

pg_upgrade 会数次连到新、旧数据库实例,所以可以修改 pg_hba.conf的认证设置成 trust 或者是 peer ,或者,也可以使用 md5 的认证方式同时使用~/.pgpass密码文件 (参考 Section 31.15).

8.停止新旧数据库

要确保新、旧数据库都停止服务,在linux下:

pg_ctl -D /opt/PostgreSQL/8.4 stop

pg_ctl -D /opt/PostgreSQL/9.0 stop

或者在windows下,使用相应的服务名称:

NET STOP postgresql-8.4

NET STOP postgresql-9.0

NET STOP pgsql-8.3  (PostgreSQL 8.3以及更早的版本使用了像这样的,有所不同的服务名称)

9.运行pg_upgrade

总是执行新数据库的pg_upgrade二进制执行文件,而不是旧数据库的。

pg_upgrade 需要指定新旧数据库实例的数据目录和可执行的 (bin) 目录。 当然你还可以指定用户和端口,以及指定是否使用数据硬链接代替使用数据复制(默认方式)。

如果使用链接模式,升级将会非常快(没有文件拷贝)并且占用更少的硬盘,但是你不能再访问你的旧数据库当你升级完成启动新的数据库实例。 链接模式还需要新旧数据库 数据目录使用相同的文件系统。(表空间和 pg_xlog 可以在不同的文件系统。) 参考 pg_upgrade --help 查看完整的帮助选项列表。

10.还原pg_hba.conf配置

如果你修改了pg_hba.conf, 那么还原那些修改的设置. 还有可能需要去调整其它的配置文件去和旧的数据库 实例相匹配,例如:postgresql.conf.

11.升级后的处理

如果有升级后续操作需要执行,pg_upgrade会在完成后发布出警告信息。 同时它会生成必须由管理员运行的脚本文件。 脚本会连到需要执行升级后续操作的新旧数据库。 脚本可以用下面命令执行:

   psql --username postgres --file script.sql postgres

脚本可以任意顺序执行,执行完后可以被删除。

Caution:通常情况下在重建脚本运行结束之前不允许访问被引用的表;这样做可能会出现 意想不到的错误结果或者是性能不佳 。不引用的表可以被立限访问。

12.统计

因为优化统计结果不会被pg_upgrade传递, 你需要指定去运行命令去在升级 完成后生成一些新的信息。你可能需要设置连接参数去匹配新的数据库实例。



二、举例进行pg_upgrade升级(pg9.3升级到9.4)

0.环境介绍

设备:一个centos6.3 32bit虚拟机

原数据库PG9.3,端口5432,安装目录:/opt/PostgreSQL/9.3/

新数据库PG9.4 ,端口5434,源码编译安装目录:/opt/pgsql944/

1.下载linux源码安装包

到官网下载pg9.4.4的源码安装包:postgresql-9.4.4.tar.gz

2.解压、编译、安装PG9.4

su - root

解压

tar jxvf postgresql-9.4.4.tar.gz

编译

cd postgresql-9.4.4 ./configure --prefix=/opt/pgsql944 --with-pgport=5434

安装

gmake world gmake install -world

安装pg_upgrade客户端程序

cd contrib gamke gmake install


注意:编译安装过程中提示缺包错误的处理思路:http://www.linuxidc.com/Linux/2012-02/53982.htm,如:

  1.报错

configure: error: readline library notfound If you have readline already installed, see config.log for detailson the failure.   It is possible the compiler isnt lookingin the proper directory. Use --without-readline to disable readlinesupport.

       2.首先查找readline包

[root@HK81-107postgresql-9.0.0]# rpm -qa | grep readlinereadline-5.1-3.el5

        说明系统已经安装了 readline包。

       3.通过 yum 搜索相关的readline 包

[root@HK81-107postgresql-9.0.0]# yum search readlinelftp.i386 : A sophisticated file transfer programlftp.i386 : A sophisticated file transfer programphp-readline.i386 : Standard PHP module provides readline librarysupportlftp.i386 : A sophisticated file transfer programreadline.i386 : A library for editing typed commandlines.compat-readline43.i386 : The readline 4.3 library for compatibilitywith older software.readline-devel.i386 : Files needed to develop programs which usethe readline library.readline.i386 : A library for editing typed command lines.

根据提示,有一个包引起了我的注意 "readline-devel",猜想可能与这个包有关。

         4安装 readline-devel 包

[root@HK81-107postgresql-9.0.0]# yum -y install -y readline-devel

       5.继续进行原来的操作即可


3.初始化一个库

在安装目录下创建一个data目录

cd /opt/pgsql944 mkdir data2

新建系统用户postgres(并创建home目录),并添加密码:

useradd -m postgres passwd postgres

将data目录授权给postgres

chown postgres data2

切换到postgres用户下

su - postgres

来到bin目录下

cd .. cd bin

初始化数据库,注意locale的设置要与原数据库的设置兼容,一般用C即可兼容大部分.

./initdb  -D ../data2  --locale=C  -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 "en_US.UTF-8". 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 ../data3 ... ok creating subdirectories ... ok selecting default max_connections ... 100 selecting default shared_buffers ... 128MB selecting dynamic shared memory implementation ... posix creating configuration files ... ok creating template1 database in ../data3/base/1 ... ok initializing pg_authid ... ok initializing dependencies ... ok creating system views ... ok loading system objects' descriptions ... ok creating collations ... ok creating conversions ... ok creating dictionaries ... ok setting privileges on built-in objects ... ok creating information schema ... ok loading PL/pgSQL server-side language ... ok vacuuming database template1 ... ok copying template1 to template0 ... ok copying template1 to postgres ... 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:     ./postgres -D ../data2  or    ./pg_ctl -D ../data2 -l logfile start

此时新的PG9.4已安装成功,可以尝试启动服务并登陆。

4.配置两个库的pg_hba.conf文件,使得postgres是受信任的.

# "local" is for Unix domain socket connections only local all postgres trust


5.将两个库都停止服务

分别执行

/opt/PostgreSQL/9.3/bin/pg_ctl stop -D ../data/opt/pgsql944/bin/pg_ctl stop -D ../data2


6.执行pg_upgrade

6.1 首先创建一个用户执行升级的目录(postgres有权的)

 执行pg_upgrade,会在当前目录生成日志,所以该命令执行时的当前目录,必须是postgres有权限的目录(可以用root创建一个目录,授权给postgres之后,到那个目录下去执行,最好不要去data目录下,因为升级过程汇总data目录下的文件可能会被改变),否则无法写日志,导致命令执行失败。

cannot write to log file pg_upgrade_internal.logFailure, exiting

所以我们先创建目录如下:

su - root

cd /opt/pg944

mkdir upgrade

chown postgres

su - postgres

cd /opt/pg944/upgrade

6.2进行pg_upgrade检查(新旧版本安装拓展要一样,否则报错。)

首先进行使用-c命令进行检查(可以通过pg_upgrade --help查看相关命令选项的含义):

/opt/pgsql944/bin/pg_upgrade -c -b /opt/PostgreSQL/9.3/bin -B /opt/pgsql944/bin -d /opt/PostgreSQL/9.3/data -D /opt/pgsql944/data -p 5432 -P 5434

/opt/pgsql944/bin/pg_upgrade -c -b /opt/PostgreSQL/9.3/bin -B /opt/pgsql944/bin -d /opt/PostgreSQL/9.3/data -D /opt/pgsql944/data2 -p 5432 -P 5434 Performing Consistency Checks ----------------------------- Checking cluster versions                                   ok Checking database user is a superuser                       ok Checking database connection settings                       ok Checking for prepared transactions                          ok Checking for reg* system OID user data types                ok Checking for contrib/isn with bigint-passing mismatch       ok Checking for invalid "line" user columns                    ok Checking for presence of required libraries                 ok Checking database user is a superuser                       ok Checking for prepared transactions                          ok *Clusters are compatible*


6.3进行pg_upgrade升级

 pg_upgrade有两种升级方式,一个是缺省的通过拷贝数据文件到新的data目录下一个是创建硬链接。拷贝的方式升级较慢,但是原库还可用;硬链接的方式升级较快,但是原库不可用。

 缺省拷贝方式升级的命令:(硬链接方式升级的命令只需要添加--link)

/opt/pgsql944/bin/pg_upgrade -b /opt/PostgreSQL/9.3/bin -B /opt/pgsql944/bin -d /opt/PostgreSQL/9.3/data -D /opt/pgsql944/data -p 5432 -P 5434

Performing Consistency Checks ----------------------------- Checking cluster versions                                   ok Checking database user is a superuser                       ok Checking database connection settings                       ok Checking for prepared transactions                          ok Checking for reg* system OID user data types                ok Checking for contrib/isn with bigint-passing mismatch       ok Checking for invalid "line" 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 a superuser                       ok Checking for prepared transactions                          ok If pg_upgrade fails after this point, you must re-initdb thenew cluster before continuing. Performing Upgrade ------------------ Analyzing all rows in the new cluster                       ok Freezing all rows on the new cluster                        ok Deleting files from new pg_clog                             ok Copying old pg_clog to new server                           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 Adding support functions to new cluster                     ok Restoring database schemas in the new cluster               ok Creating newly-required TOAST tables                        ok Removing support functions from new cluster                 ok Copying user relation files                                 ok Setting next OID for new cluster                            ok Sync data directory to disk                                 ok Creating script to analyze new cluster                      ok Creating script to delete old cluster                       ok Upgrade Complete ---------------- Optimizer statistics are not transferred by pg_upgrade  so,once you start the new server, consider running: analyze_new_cluster.sh Running this script will delete the old cluster's data files: delete_old_cluster.sh


6.4启动PG9.4

启动PG9.4并查看数据库内容,能看到升级成功,数据已经从pg9.3移植到9.4.

-bash-4.1$./pg_ctl start -D ../data2 -bash-4.1$ ./psqlpsql  (9.4.4) Type "help" for help. postgres=# show port; port ------  5432 (1 row) postgres=# select version();                             version                      

-----------------------------------------------------PostgreSQL 9.4.4 on i686-pc-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-11), 32-bit(1 row) postgres=# \l                                  List of databases      Name  |  Owner   | Encoding |   Collate   |    Ctype    | Access privileges                      ----------+----------+----------+-------------+-------------+-----------------------   postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |    template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres+                                                                                       | postgres=CTc/postgres   template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | postgres=CTc/postgres+                                                                             | =c/postgres  (3 rows)postgres=# \d       List of relations Schema | Name | Type  |  Owner   --------+------+-------+----------  public  | lyy  | table | postgres (1 row) postgres=# select * from lyy; id | name  ----+------   1 | lily  2 | lucy (2 rows)


7.pg_upgrade之后的操作

7.1根据情况执行pg_upgrade生成的脚本文件:

Optimizer statistics are not transferred by pg_upgrade so,once you start the new server, consider running:  

analyze_new_cluster.sh

Running this script will delete the old cluster's data files:    

delete_old_cluster.sh

7.2配置pg9.4的pg_hba.conf

 配置pg9.4的pg_hba.conf为应用实际需要的情况。

7.3配置pg9.4的postgresql.conf

配置pg9.4的postgresql.conf,使其还原pg9.3中的设置,或者做其他实用的配置。


8.面向实际应用

本案例仅作pg_upgrade的执行示例,实际应用中请自行做相应的调整。


pg_class 里面relhasoids字段已在12版本里面被移除了,navicat老版本登录时还是回去查询

所以要把navicat升级到最新版。

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
4月前
|
人工智能 运维 关系型数据库
媒体声音|PolarDB再升级:欢迎来到云数据库 x AI新时代
让个人开发者和企业用户都可以像“搭积木”一样开发和管理数据库
媒体声音|PolarDB再升级:欢迎来到云数据库 x AI新时代
|
4天前
|
SQL 运维 关系型数据库
PolarDB产品使用合集之PolarDB 2.3.0 版本的 CDC 功能支持 Polardb-X 到 Polardb-X 的数据同步吗
PolarDB产品使用合集涵盖了从创建与管理、数据管理、性能优化与诊断、安全与合规到生态与集成、运维与支持等全方位的功能和服务,旨在帮助企业轻松构建高可用、高性能且易于管理的数据库环境,满足不同业务场景的需求。用户可以通过阿里云控制台、API、SDK等方式便捷地使用这些功能,实现数据库的高效运维与持续优化。
|
3月前
|
Cloud Native 关系型数据库 分布式数据库
|
4月前
|
Cloud Native 关系型数据库 分布式数据库
富友支付 x PolarDB:助力金融支付数字化升级
富友支付与阿里云瑶池数据库团队深度合作,不断的推进支付系统的升级迭代,逐步替代传统商业数据库,全面拥抱云原生PolarDB,与阿里云一同为支付行业赋能。
|
4月前
|
关系型数据库 MySQL 分布式数据库
PolarDB MySQL企业版与标准版功能对比:如何选择适合您的版本?
随着数字化时代的到来,企业对于数据处理的需求越来越高,而数据库作为数据处理的核心,其性能和成本成为了企业关注的焦点。阿里云全新推出的PolarDB MySQL企业版和标准版,以全新的架构和优化,为企业提供了高性能、低成本的数据库解决方案。但在功能上,这两个版本有很多差异,我们该如何选择呢?
63 2
|
5月前
|
安全 关系型数据库 数据库
上新|阿里云RDS PostgreSQL支持PG 16版本,AliPG提供丰富自研能力
AliPG在社区版16.0的基础上,在安全、成本、可运维性等多个方面做了提升,丰富的内核/插件特性支持,满足业务场景的需求
|
6月前
|
SQL 关系型数据库 PostgreSQL
PostgreSQL查看版本
PostgreSQL查看版本
|
7月前
|
存储 关系型数据库 Go
深入理解 PostgreSQL 中的 MVCC(多版本并发控制)机制
深入理解 PostgreSQL 中的 MVCC(多版本并发控制)机制
92 0
|
8月前
|
SQL Oracle 关系型数据库
Polar DB-O (兼容 Oracle 语法版本)和Polar DB PostgreSQL 版本概述(二)
Polar DB-O (兼容 Oracle 语法版本)和Polar DB PostgreSQL 版本概述(二)
729 0
|
9月前
|
Cloud Native 关系型数据库 数据挖掘
AnalyticDB PostgreSQL版7.0版本公测期间,享优先购买福利!一次性购买6个月资源,可享0.1折!
云原生向量数据库AnalyticDB PostgreSQL版全新发布7.0公测版本!版本性能较开源实现开箱5X性能提升!