pg_upgrade大版本升级

本文涉及的产品
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
简介: pg_upgrade(1)PostgreSQL提供大版本升级的一个工具,比如说从9.1到9.2,也可以一次跨多个大版本,直接从9.1到9.5等,它的优点是不需要把数据导入导出,这在数据量比较大的时候,非常方便。

pg_upgrade

(1)PostgreSQL提供大版本升级的一个工具,比如说从9.1到9.2,也可以一次跨多个大版本,直接从9.1到9.5等,它的优点是不需要把数据导入导出,这在数据量比较大的时候,非常方便。

(2)不适合小版本升级,比如说从9.0.1到9.0.4

(3)PostgreSQL8.4.x之后才能使用pg_upgrade

(4)如果数据量不大,可以使用pg_dump/pg_restore升级数据库


环境

Old:postgresql-9.5.9

New:postgresql-9.6.5


安装postgresql-9.5.9

[root@Darren2 ~]# mkdir -p /usr/local/pgsql/pgsql9.5.9/{data,arch}

[root@Darren2 pgsql]# groupadd dba

[root@Darren2 pgsql]# useradd -g dba -G root postgres -d /usr/local/pgsql

[root@Darren2 pgsql9.5.9]# tar xf postgresql-9.5.9.tar.gz

[root@Darren2 pgsql]# chmod -R 755 /usr/local/pgsql

[root@Darren2 pgsql]# chown -R postgres:dba /usr/local/pgsql

[root@Darren2 pgsql]# chmod -R 700 /usr/local/pgsql/pgsql9.5.9/data/

Darren2:postgres:/usr/local/pgsql:>cat .bash_profile

# .bash_profile

# Get the aliases and functions

if [ -f ~/.bashrc ]; then

    . ~/.bashrc

fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PGHOME=/usr/local/pgsql/pgsql9.5.9

export PGDATA=$PGHOME/data

export PATH=$PATH:$PGHOME/bin

#stty erase

set umask to 022

umask 022

PS1=`uname -n`":"'$USER'":"'$PWD'":>"; export PS1


[root@Darren2 postgresql-9.5.9]# ./configure --prefix=/usr/local/pgsql/pgsql9.5.9/

[root@Darren2 postgresql-9.5.9]# make world && make install-world

Darren2:postgres:/usr/local/pgsql:>initdb -D $PGDATA -U postgres -E UTF8  -W


Darren2:postgres:/usr/local/pgsql/pgsql9.5.9/data:>vim pg_hba.conf

host all all 0.0.0.0/0 md5


Darren2:postgres:/usr/local/pgsql/pgsql9.5.9/data:>vim postgresql.conf

listen_addresses = '*'

wal_level = archive

port = 5432              

max_connections = 300         

shared_buffers = 128MB          

logging_collector = on     

log_directory = 'pg_log'       

log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'  

archive_mode = on

archive_command = 'test ! -f /usr/local/pgsql/pgsql9.5.9/arch/%f  &&  cp %p /usr/local/pgsql/pgsql9.5.9/arch/%f'


#启动数据库

Darren2:postgres:/usr/local/pgsql:>pg_ctl start


#创建测试数据

Darren2:postgres:/usr/local/pgsql/pgsql9.5.9:>cd /usr/local/pgsql/pgsql9.5.9

Darren2:postgres:/usr/local/pgsql/pgsql9.5.9:>mkdir tbs1

Darren2:postgres:/usr/local/pgsql/pgsql9.5.9/data:>psql

postgres=# create role cdhu1 login encrypted password '147258';

postgres=# create tablespace tbs1 location '/usr/local/pgsql/pgsql9.5.9/tbs1';

postgres=# create database testdb1 template template0 encoding 'UTF8' tablespace tbs1;

postgres=# grant all on database testdb1 to cdhu1;

postgres=# grant all on tablespace tbs1 to cdhu1;

Darren2:postgres:/usr/local/pgsql/pgsql9.5.9/data:>psql -d testdb1 -U cdhu1

testdb1=> create table t1(id int primary key, info text);

testdb1=> insert into t1 select generate_series(1,2000000),'helloWorld';


安装新版本postgresql-9.6.5

[root@Darren2 ~]# mkdir -p /usr/local/pgsql/pgsql9.6.5/{data,arch}

[root@Darren2 pgsql9.6.5]# tar xf postgresql-9.6.5.tar.gz

[root@Darren2 pgsql]# chmod -R 755 /usr/local/pgsql

[root@Darren2 pgsql]# chown -R postgres:dba /usr/local/pgsql

[root@Darren2 pgsql]# chmod -R 700 /usr/local/pgsql/pgsql9.6.5/data/

[root@Darren2 postgresql-9.5.9]# ./configure --prefix=/usr/local/pgsql/pgsql9.6.5/

[root@Darren2 postgresql-9.5.9]# make world && make install-world

Darren2:postgres:/usr/local/pgsql/pgsql9.6.5/bin:>cd /usr/local/pgsql/pgsql9.6.5/bin

Darren2:postgres:/usr/local/pgsql:>./initdb -D /usr/local/pgsql/pgsql9.6.5/data  -U postgres -E UTF8  -W


Darren2:postgres:/usr/local/pgsql/pgsql9.5.9/data:>vim pg_hba.conf

host all all 0.0.0.0/0 md5


Darren2:postgres:/usr/local/pgsql/pgsql9.6.5/data:>vim postgresql.conf

listen_addresses = '*'

wal_level = replica

port = 5431       #必须和上面端口号的不一样    

max_connections = 300         

shared_buffers = 128MB          

logging_collector = on     

log_directory = 'pg_log'       

log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'  

archive_mode = on

archive_command = 'test ! -f /usr/local/pgsql/pgsql9.6.5/arch/%f  &&  cp %p /usr/local/pgsql/pgsql9.6.5/arch/%f'


#多实例的启动数据库服务

Darren2:postgres:/usr/local/pgsql:>/usr/local/pgsql/pgsql9.5.9/bin/pg_ctl start -D /usr/local/pgsql/pgsql9.5.9/data/

Darren2:postgres:/usr/local/pgsql:>/usr/local/pgsql/pgsql9.6.5/bin/pg_ctl start -D /usr/local/pgsql/pgsql9.6.5/data


#多实例的停止数据服务(升级前需要停止数据库服务)

Darren2:postgres:/usr/local/pgsql:>/usr/local/pgsql/pgsql9.5.9/bin/pg_ctl stop -m fast -D /usr/local/pgsql/pgsql9.5.9/data

Darren2:postgres:/usr/local/pgsql:>/usr/local/pgsql/pgsql9.6.5/bin/pg_ctl stop -m fast -D /usr/local/pgsql/pgsql9.6.5/data


pg_upgrade参数解析

-b, --old-bindir=BINDIR       old cluster executable directory

-B, --new-bindir=BINDIR       new cluster executable directory

-d, --old-datadir=DATADIR     old cluster data directory

-D, --new-datadir=DATADIR     new cluster data directory

-p, --old-port=PORT           old cluster port number (default 50432)

-P, --new-port=PORT           new cluster port number (default 50432)

-c, --check                   check clusters only, don't change any data

-j, --jobs                    number of simultaneous processes or threads to use

-k, --link                    link instead of copying files to new cluster

-r, --retain                  retain SQL and log files after success

-U, --username=NAME           cluster superuser (default "postgres")

-v, --verbose                 enable verbose internal logging


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


使用9.6.5的pg_upgrade检测兼容性

Darren2:postgres:/usr/local/pgsql:>/usr/local/pgsql/pgsql9.6.5/bin/pg_upgrade -b /usr/local/pgsql/pgsql9.5.9/bin -B /usr/local/pgsql/pgsql9.6.5/bin -d /usr/local/pgsql/pgsql9.5.9/data -D /usr/local/pgsql/pgsql9.6.5/data -p 5432 -P 5431 -U postgres -j 8 -k -c

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 reg* system OID user data types                ok

Checking for contrib/isn with bigint-passing mismatch       ok

Checking for roles starting with 'pg_'                      ok

Checking for presence of required libraries                 ok

Checking database user is the install user                  ok

Checking for prepared transactions                          ok


正式升级

Darren2:postgres:/usr/local/pgsql:>/usr/local/pgsql/pgsql9.6.5/bin/pg_upgrade -b /usr/local/pgsql/pgsql9.5.9/bin -B /usr/local/pgsql/pgsql9.6.5/bin -d /usr/local/pgsql/pgsql9.5.9/data -D /usr/local/pgsql/pgsql9.6.5/data -p 5432 -P 5431 -U postgres -j 8 -k -r -v

......

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


#生成2个脚本, 用于收集统计信息和删除老集群,和一些有关升级过程的日志信息

Darren2:postgres:/usr/local/pgsql:>ls -ltr

-rw-r--r--. 1 postgres dba   2215 Oct 18 01:37 pg_upgrade_dump_globals.sql

-rw-------. 1 postgres dba   2237 Oct 18 01:37 pg_upgrade_dump_13241.custom

-rw-------. 1 postgres dba   2228 Oct 18 01:37 pg_upgrade_dump_1.custom

-rw-------. 1 postgres dba   3733 Oct 18 01:37 pg_upgrade_dump_16386.custom

-rw-------. 1 postgres dba   2422 Oct 18 01:37 pg_upgrade_dump_13241.log

-rw-------. 1 postgres dba   2409 Oct 18 01:37 pg_upgrade_dump_1.log

-rw-------. 1 postgres dba   2686 Oct 18 01:37 pg_upgrade_dump_16386.log

-rw-------. 1 postgres dba 240810 Oct 18 01:37 pg_upgrade_utility.log

-rwx------. 1 postgres dba    112 Oct 18 01:37 delete_old_cluster.sh

-rwx------. 1 postgres dba    809 Oct 18 01:37 analyze_new_cluster.sh

-rw-------. 1 postgres dba   2956 Oct 18 01:37 pg_upgrade_server.log

-rw-------. 1 postgres dba  42227 Oct 18 01:37 pg_upgrade_internal.log


#启动新的数据库集群

Darren2:postgres:/usr/local/pgsql:>/usr/local/pgsql/pgsql9.6.5/bin/pg_ctl start -D /usr/local/pgsql/pgsql9.6.5/data


#根据脚本analyze_new_cluster.sh执行统计信息收集

Darren2:postgres:/usr/local/pgsql:>/usr/local/pgsql/pgsql9.6.5/bin/vacuumdb -U 'postgres' --all --analyze-in-stages -p 5431

vacuumdb: processing database "postgres": Generating minimal optimizer statistics (1 target)

vacuumdb: processing database "template1": Generating minimal optimizer statistics (1 target)

vacuumdb: processing database "testdb1": Generating minimal optimizer statistics (1 target)

vacuumdb: processing database "postgres": Generating medium optimizer statistics (10 targets)

vacuumdb: processing database "template1": Generating medium optimizer statistics (10 targets)

vacuumdb: processing database "testdb1": Generating medium optimizer statistics (10 targets)

vacuumdb: processing database "postgres": Generating default (full) optimizer statistics

vacuumdb: processing database "template1": Generating default (full) optimizer statistics

vacuumdb: processing database "testdb1": Generating default (full) optimizer statistics


#查看数据是否存在

Darren2:postgres:/usr/local/pgsql:>psql -p 5431

postgres=# select version();

                                                 version                                                 

---------------------------------------------------------------------------------------------------------

PostgreSQL 9.6.5 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4), 64-bit

testdb1=# \dt+

                  List of relations

Schema | Name | Type  | Owner | Size  | Description

--------+------+-------+-------+-------+-------------

public | t1   | table | cdhu1 | 85 MB |


#通过上述脚本删除老数据

Darren2:postgres:/usr/local/pgsql:>cat delete_old_cluster.sh

#!/bin/sh

rm -rf '/usr/local/pgsql/pgsql9.5.9/data'

rm -rf '/usr/local/pgsql/pgsql9.5.9/tbs1/PG_9.5_201510051'

Darren2:postgres:/usr/local/pgsql:>bash delete_old_cluster.sh


最后再修改环境变量和端口号等和原来版本保持一致。


Error

Darren2:postgres:/usr/local/pgsql:>/usr/local/pgsql/pgsql9.6.5/bin/pg_ctl start -D /usr/local/pgsql/pgsql9.6.5/data

pg_ctl: error while loading shared libraries: libpq.so.5: cannot open shared object file: No such file or directory

解决方法:

[root@Darren2 postgresql-9.6.5]# ln -s /usr/local/pgsql/pgsql9.6.5/lib/libpq.so.5 /usr/lib64/


相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
3月前
|
存储 监控 关系型数据库
如何升级MySQL版本?
如何升级MySQL版本?
229 2
|
3月前
|
监控 关系型数据库 MySQL
如何升级mysql的版本
如何升级mysql的版本
654 2
LXJ
|
安全 关系型数据库 Linux
PostgreSQL通过pg_upgrade进行大版本升级
PostgreSQL通过pg_upgrade进行大版本升级
LXJ
2967 0
|
关系型数据库 MySQL Linux
MySQL集群升级从5.5版本升级到10.*以上版本(CentOS7.5 MariaDB集群升级)
MySQL集群升级从5.5版本升级到10.*以上版本(CentOS7.5 MariaDB集群升级)
334 0
MySQL集群升级从5.5版本升级到10.*以上版本(CentOS7.5 MariaDB集群升级)
|
SQL 关系型数据库 数据库
【DB吐槽大会】第20期 - PG pg_upgrade大版本升级不支持增量
大家好,这里是DB吐槽大会,第20期 - PG pg_upgrade大版本升级不支持增量
|
关系型数据库 数据库 开发工具
|
关系型数据库 数据库 PostgreSQL