Postgresql 使用pg_upgrade升级(9.4.4~9.5.2)

建立新版本数据库目录并修改目录owner
#mkdir -p /opt/pgsql9.5.2
#mkdir -p /opt/pgdata9.5.2/pg_root
#chown -R postgres:postgres /opt/pgsql9.5.2
#chown -R postgres:postgres /opt/pgdata9.5.2/pg_root/

解压源码包并安装新版本数据库,注意目录,要新建目录
#tar -zxvf postgresql-9.5.2.tar
#cd postgresql-9.5.2/
#./configure --prefix=/opt/pgsql9.5.2 --with-pgport=1922 --with-segsize=8 --with-wal-segsize=64 --with-wal-blocksize=64 --with-perl --with-python --with-openssl --with-pam --with-ldap --with-libxml --with-libxslt --enable-thread-safety
#gmake world
#gmake install-world

安装完成后,初始化数据库
$/opt/pgsql9.5.2/bin/initdb  -D /opt/pgdata9.5.2/pg_root/

利用pg_upgrade检查升级是否满足条件
$/opt/pgsql9.5.2/bin/pg_upgrade -c --link -b /opt/pgsql/bin -B /opt/pgsql9.5.2/bin -d /opt/pgdata/pg_root -D /opt/pgdata9.5.2/pg_root/

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 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 presence of required libraries                 ok
Checking database user is the install user                  ok
Checking for prepared transactions                          ok

*Clusters are compatible*

停老库
$pg_ctl stop -m fast

进行升级
$/opt/pgsql9.5.2/bin/pg_upgrade  --link -b /opt/pgsql/bin -B /opt/pgsql9.5.2/bin -d /opt/pgdata/pg_root -D /opt/pgdata9.5.2/pg_root/
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
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

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
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
Creating newly-required TOAST tables                        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/pgdata/pg_root/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

升级完成后,启动新版本数据库
 $/opt/pgsql9.5.2/bin/pg_ctl start -D /opt/pgdata9.5.2/pg_root/
server starting

psql 连接数据库查看一切正常
postgres=# select version();
                                                 version                                                  
----------------------------------------------------------------------------------------------------------
 PostgreSQL 9.5.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-16), 64-bit

对库做analyze生成新的统计信息
 $./analyze_new_cluster.sh 
This script will generate minimal optimizer statistics rapidly
so your system is usable, and then gather statistics twice more
with increasing accuracy.  When it is done, your system will
have the default level of optimizer statistics.

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

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

vacuumdb: processing database "hank": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "local": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "postgres": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "remote": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "template1": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "hank": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "local": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "postgres": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "remote": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "template1": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "hank": Generating default (full) optimizer statistics
vacuumdb: processing database "local": Generating default (full) optimizer statistics
vacuumdb: processing database "postgres": Generating default (full) optimizer statistics
vacuumdb: processing database "remote": Generating default (full) optimizer statistics
vacuumdb: processing database "template1": Generating default (full) optimizer statistics

Done

删除老版本库的数据目录
$cat delete_old_cluster.sh
#!/bin/sh

rm -rf '/opt/pgdata/pg_root'
rm -rf '/data02/pgdata/tbs_hank/PG_9.4_201409291'
./delete_old_cluster.sh 

修改环境变量为新版本路径
PGHOME==/opt/pgsql9.5.2
PGDATA=/opt/pgdata9.5.2/pg_root

你可能感兴趣的:(Postgresql 使用pg_upgrade升级(9.4.4~9.5.2))