您的位置:首页 > 数据库

使用pg_upgrade升级pg数据库的大版本。

2017-03-30 14:20 701 查看
pg_upgrade 是 PostgreSQL 提供的一个大版本升级工具,例如将 PG 从版本 9.4 升级到 9.6, 可以跨多个大版本升级, 当然也可以用 pg_dump ,之后再 pg_restore 到新版本 PG 软件的方法,  当数据库比较大时,比如 1 TB 以上,pg_dump/pg_restore 的方法太费时了,这里介绍使用 pg_upgrade 将 PostgreSQL 版本从 9.4 升级到 9.6 的步骤,跨越了 2 个大版本。

操作系统: redhat 6.5
数据库:     pg9.4  升级到pg9.6
首先,虚拟机中本来已经安装了一套pg9.4 。保留几个表中的数据,以便升级后,在新版本的数据库中查询,并验证是否成功。
本次安装pg9.6,目录不同,端口号不同
安装9.6的过程中,因为不修改环境变量,所以initdb 和 pg_ctl start命令都需要指定运行的目录。
进入解压目录后:
./configure --prefix=/opt/pg96/ --with-pgport=5433
make
make install
/opt/pg96/bin/initdb -D /opt/pg96/data/
/opt/pg96/bin/pg_ctl  -D  /opt/pg96/data/ start

安装完成后。
9.4 数据目录:/opt/pg94/data/
9.4  bin目录: /opt/pg94/bin/
9.6 数据目录:/opt/pg96/data/
9.6  bin目录: /opt/pg96/bin/

pg_upgrade -b oldbindir -B newbindir -d olddatadir -D newdatadir     -c表示只检查不升级, 没有-c就会做升级。

首先将两个数据库都停掉。
[postgres@pg ~]$  /opt/pg96/bin/pg_ctl  -D  /opt/pg96/data/ stop
[postgres@pg ~]$ /opt/pg94/bin/pg_ctl  -D  /opt/pg94/data/ stop
停库以后,使用9.6版本的pg_upgrade 进行
升级前的检查。
[postgres@pg ~]$ /opt/pg96/bin/pg_upgrade   -c    -b /opt/pg94/bin/ -B /opt/pg96/bin/ -d /opt/pg94/data/ -D /opt/pg96/data/ -p 5432 -P 5433
*failure*
Consult the last few lines of "pg_upgrade_server.log" for
the probable cause of the failure.
Performing Consistency Checks on Old Live Server
------------------------------------------------
Checking cluster versions                                   ok
Checking database user is a superuser                       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                 fatal

Your installation references loadable libraries that are missing from the
new installation.  You can add these libraries to the new installation,
or remove the functions using them from the old installation.  A list of
problem libraries is in the file:
loadable_libraries.txt

Failure, exiting
[postgres@pg ~]$ cat loadable_libraries.txt
could not load library "$libdir/pg_stat_statements":
ERROR:  could not access file "$libdir/pg_stat_statements": No such file or directory

上面这种错误还有很多,比如说旧版本有一些扩展工具,新版本没有,会报这个错误。
解决的办法就是在新版本的数据库中安装这些工具。
或者假如旧版本的数据库工具可以删除,那么删除后尝试升级。
以下为
(一)删除后尝试升级
[postgres@pg contrib]$ psql
psql (9.4.4)
Type "help" for help.

postgres=# drop extension pg_stat_statements ;
DROP EXTENSION
[postgres@pg contrib]$ /opt/pg96/bin/pg_upgrade   -c    -b /opt/pg94/bin/ -B /opt/pg96/bin/ -d /opt/pg94/data/ -D /opt/pg96/data/  -p 5432 -P 5433
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

*Clusters are compatible*
成功通过检查。
(二)给新版本安装工具
切换到root用户下, exit
[root@pg ~]#  export PGHOME=/opt/pg96/data/
[root@pg ~]#  export PATH=$PGHOME/bin:$PATH
[root@pg ~]#  export LD_LIBRARY_PATH=/opt/pg96/lib:$LD_LIBRARY_PATH
[root@pg ~]#  cd /tmp/postgresql-9.6.1/contrib
[root@pg contrib]#  make
[root@pg contrib]#   make install pg_stat_statements
[root@pg contrib]# su - postgres
[postgres@pg contrib]$ /opt/pg96/bin/pg_ctl start -D /opt/pg96/data/
[postgres@pg contrib]$ psql -p 5433
psql (9.4.4, server 9.6.1)
WARNING: psql major version 9.4, server major version 9.6.
Some psql features might not work.
Type "help" for help.
postgres=# create extension pg_stat_statements ;
CREATE EXTENSION
9.6停库
[postgres@pg ~]$  /opt/pg96/bin/pg_ctl start -D /opt/pg96/data/
[postgres@pg ~]$
b056
/opt/pg94/bin/pg_ctl start -D /opt/pg94/data/
再次进行检查
[postgres@pg ~]$ /opt/pg96/bin/pg_upgrade   -c    -b /opt/pg94/bin/ -B /opt/pg96/bin/ -d /opt/pg94/data/ -D /opt/pg96/data/ -p 5432 -P 5433
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

*Clusters are compatible*

以上两种情况根据实际情况选择。

假如升级失败,需要重新初始化新库,并重新安装工具。
[postgres@pg ~]$  rm -rf /opt/pg96/data/*
设置临时环境变量
[postgres@pg ~]$  export LD_LIBRARY_PATH=/opt/pg96/lib:$HOME/lib
[postgres@pg ~]$  /opt/pg96/bin/initdb -D /opt/pg96/data/
然后确保可以进入旧库和新库。旧库直接psql就行,新库需要如下命令:
[postgres@pg ~]$ /opt/pg96/bin/pg_ctl start -D /opt/pg96/data/
[postgres@pg ~]$ /opt/pg96/bin/psql
psql (9.6.1)
Type "help" for help.
postgres=# create extension pg_stat_statements ;
CREATE EXTENSION

停库!
/opt/pg96/bin/pg_ctl stop -D /opt/pg96/data/
/opt/pg94/bin/pg_ctl stop -D /opt/pg94/data/

正式升级:
[postgres@pg ~]$  /opt/pg96/bin/pg_upgrade  -b /opt/pg94/bin/ -B /opt/pg96/bin/ -d /opt/pg94/data/ -D /opt/pg96/data/ -p 5432 -P 5433

Performing Consistency Checks
-----------------------------
Checking cluster versions                                                 ok
Checking database user is a superuser                            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 the
new 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
Setting oldest multixact ID on 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
Setting minmxid counter in new cluster                                ok
Creating newly-required TOAST tables                                ok
Removing support functions from new cluster                      ok
Adding ".old" suffix to old global/pg_control                          ok

If you want to start the old cluster, you will need to remove
the ".old" suffix from /opt/pg9.6/data/global/pg_control.old.
Because "link" mode was used, the old cluster cannot be safely
started once the new cluster has been started.

Linking 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

使用新版本软件起库:
[postgres@pg ~]$  /opt/pg96/bin/pg_ctl --version
pg_ctl (PostgreSQL) 9.6.1
[postgres@pg ~]$  /opt/pg96/bin/pg_ctl start -D /opt/pg96/data/
server starting
正常启动。
[postgres@pg ~]$ /opt/pg96/bin/psql
psql (9.6.1)
Type "help" for help.
postgres=# select version();
version
---------------------------------------------------------------------------------------------------------
PostgreSQL 9.6.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4), 64-bit
(1 row)

postgres=# \d
List of relations
Schema |        Name        | Type  |  Owner
--------+--------------------+-------+----------
public | a                  | table | postgres
public | pg_stat_statements | view  | postgres
(2 rows)

postgres=# select * from a;
a
---
1
2
3
(3 rows)

到此可以确认升级成功。最后运行一下上面标注出的两个脚本。
收集新库统计信息:
analyze_new_cluster.sh
删除老版本的软件:
delete_old_cluster.sh
[postgres@pg ~]$ cd /home/postgres/
[postgres@pg upgrade]$ ll
total 8
-rwx------. 1 postgres postgres 747 Mar 30 01:33 analyze_new_cluster.sh
-rwx------. 1 postgres postgres  36 Mar 30 01:33 delete_old_cluster.sh
依次运行这两个脚本:
[postgres@pg upgrade]$ ./analyze_new_cluster.sh
[postgres@pg upgrade]$ ./delete_old_cluster.sh
完成。

看一下脚本具体内容:
[postgres@pg ~]$ cat ./analyze_new_cluster.sh
#!/bin/sh

echo 'This script will generate minimal optimizer statistics rapidly'
echo 'so your system is usable, and then gather statistics twice more'
echo 'with increasing accuracy.  When it is done, your system will'
echo 'have the default level of optimizer statistics.'
echo

echo 'If you have used ALTER TABLE to modify the statistics target for'
echo 'any tables, you might want to remove them and restore them after'
echo 'running this script because they will delay fast statistics generation.'
echo

echo 'If you would like default statistics as quickly as possible, cancel'
echo 'this script and run:'
echo '    "/opt/pg96/bin/vacuumdb" --all --analyze-only'
echo

"/opt/pg96/bin/vacuumdb" --all --analyze-in-stages
echo

echo 'Done'
[postgres@pg ~]$ cat  ./delete_old_cluster.sh
#!/bin/sh
rm -rf '/opt/pg94/data'
如果有问题,欢迎交流。博主新人,请多指教。



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐