对于跨版本的PostgreSQL升级,官方给出了如下三种方法:sql
[root@db04 ~]# yum install postgresql11.x86_64 postgresql11-contrib.x86_64 postgresql11-devel.x86_64 postgresql11-docs.x86_64 postgresql11-libs.x86_64 postgresql11-pltcl.x86_64 postgresql11-server.x86_64 [postgres@db04 ~]$ /usr/pgsql-11/bin/initdb -D /var/lib/pgsql/11/data
[postgres@db04 ~]$ cp /var/lib/pgsql/9.6/data/postgresql.conf /var/lib/pgsql/11/data [postgres@db04 ~]$ cp /var/lib/pgsql/9.6/data/pg_hba.conf /var/lib/pgsql/11/data
[root@db04 ~]# systemctl stop postgresql-9.6
pg_upgrade的参数能够参考官方文档。数据库
[postgres@db04 ~]$ /usr/pgsql-11/bin/pg_upgrade -b /usr/pgsql-9.6/bin -B /usr/pgsql-11/bin -d /var/lib/pgsql/9.6/data -D /var/lib/pgsql/11/data Performing Consistency Checks ----------------------------- Checking cluster versions ok Checking database user is the ×××tall user ok Checking database connection settings ok Checking for prepared transactions ok Checking for reg* data types in user tables ok Checking for contrib/isn with bigint-passing mismatch ok Checking for invalid "unknown" 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 the ×××tall 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 in the new cluster ok Deleting files from new pg_xact 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 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 Checking for hash indexes 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
整个数据库大约12G,耗时两分钟完成升级。api
在升级过程当中不会把统计信息传到新库系统表中,因此须要从新收集统计信息。根据升级过程当中产生的脚本,生成新的统计信息。ide
[postgres@db04 ~]$ ./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: "/usr/pgsql-11/bin/vacuumdb" --all --analyze-only vacuumdb: processing database "postgres": Generating minimal optimizer statistics (1 target) vacuumdb: processing database "rhnschema": 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 "rhnschema": 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 "rhnschema": Generating default (full) optimizer statistics vacuumdb: processing database "template1": Generating default (full) optimizer statistics Done
使用升级过程当中产生的删除脚本清楚旧库信息并使用yum删除旧软件包。工具
[postgres@db04 ~]$ ./delete_old_cluster.sh [root@db04 ~]# yum -y remove postgresql96*