對於跨版本的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*