標籤:postgresql 升級
PostgreSQL中的升級,如果針對小版本的升級,比如9.6.1升級到9.6.2(當前的最新版本),只需要用9.6.2版本的軟體替換9.6.1版本的軟體即可,不需要做額外的操作,因為整個大版本是相互相容的,內部儲存形式也是相容的。但如果涉及到跨大版本升級比如9.4.11升級到9.6.2,這種直接替換軟體就不行了,因為跨版本的內部儲存形式發生了變化。
官方給了三種升級的方式來解決跨版本升級:
pg_dumpall
pg_upgrade
通過複製
pg_dumpall是一種把資料從舊版本邏輯匯出,再匯入新版本的方法,就是一個匯出匯入的過程。
通過複製的方式是建立一個高版本的從庫,等資料同步完後主變備,備變主,達到升級的目的。
再一種是通過pg_upgrade命令的升級方式,它是一種快速升級的方法,通過建立新的系統資料表並使用舊的使用者表的方式進行升級。它又分為兩種方式:原地升級和非原地升級,原地升級需要指定--link參數。
下面介紹一下使用pg_upgrade做升級的大體步驟:
樣本是從9.4.11升級到9.6.2。
1、安裝新版本軟體
新版本的軟體需要保證與舊版本的軟體在配置上相容,pg_upgrade會在升級前檢查pg_controldata,確保所有的設定是相容的。
2、用新版本初始化一個新的資料庫
[[email protected] ~]$ /opt/pgsql-9.6.2/bin/initdb -D /pgdata-new/The files belonging to this database system will be owned by user "postgres".This user must also own the server process.The database cluster will be initialized with locale "en_US.UTF-8".The default database encoding has accordingly been set to "UTF8".The default text search configuration will be set to "english".Data page checksums are disabled.fixing permissions on existing directory /pgdata-new ... okcreating subdirectories ... okselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting dynamic shared memory implementation ... posixcreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... oksyncing data to disk ... okWARNING: enabling "trust" authentication for local connectionsYou can change this by editing pg_hba.conf or using the option -A, or--auth-local and --auth-host, the next time you run initdb.Success. You can now start the database server using: /opt/pgsql-9.6.2/bin/pg_ctl -D /pgdata-new/ -l logfile start
3、設定pg_hba.conf,保證pg_upgrade通過串連新舊兩個庫
4、停止舊庫
#建立測試表[[email protected] ~]$ psqlpsql (9.4.11)Type "help" for help. ^postgres=# create table zx (id int);CREATE TABLEpostgres=# \d List of relations Schema | Name | Type | Owner --------+------+-------+---------- public | zx | table | postgres(1 row)postgres=# insert into zx values(1);INSERT 0 1postgres=# select * from zx; id ---- 1(1 row)#停止舊庫[[email protected] ~]$ /opt/pgsql-9.4/bin/pg_ctl stop -D /usr/local/pgsql/data/waiting for server to shut down.... doneserver stopped
5、使用pg_upgrade執行升級
[[email protected] ~]$ /opt/pgsql-9.6.2/bin/pg_upgrade -d /usr/local/pgsql/data/ -D /pgdata-new/ -b /opt/pgsql-9.4/bin/ -B /opt/pgsql-9.6.2/bin/ Performing Consistency Checks-----------------------------Checking cluster versions okChecking database user is the install user okChecking database connection settings okChecking for prepared transactions okChecking for reg* system OID user data types okChecking for contrib/isn with bigint-passing mismatch okChecking for roles starting with ‘pg_‘ okCreating dump of global objects okCreating dump of database schemas okChecking for presence of required libraries okChecking database user is the install user okChecking for prepared transactions okIf pg_upgrade fails after this point, you must re-initdb thenew cluster before continuing.Performing Upgrade------------------Analyzing all rows in the new cluster okFreezing all rows on the new cluster okDeleting files from new pg_clog okCopying old pg_clog to new server okSetting next transaction ID and epoch for new cluster okDeleting files from new pg_multixact/offsets okCopying old pg_multixact/offsets to new server okDeleting files from new pg_multixact/members okCopying old pg_multixact/members to new server okSetting next multixact ID and offset for new cluster okResetting WAL archives okSetting frozenxid and minmxid counters in new cluster okRestoring global objects in the new cluster okRestoring database schemas in the new cluster okCopying user relation files okSetting next OID for new cluster okSync data directory to disk okCreating script to analyze new cluster okCreating script to delete old cluster okUpgrade Complete----------------Optimizer statistics are not transferred by pg_upgrade so,once you start the new server, consider running: ./analyze_new_cluster.shRunning this script will delete the old cluster‘s data files: ./delete_old_cluster.sh
介紹下使用的參數-b指定舊版本軟體的bin目錄-B指定新版本軟體的bin目錄,-d指定舊版本對應的資料目錄,-D指定新版本對應的資料目錄。
6、啟動新版本資料庫並做檢查
[[email protected] ~]$ /opt/pgsql-9.6.2/bin/pg_ctl start -D /pgdata-new/ -l logfile server starting[[email protected] ~]$ psql psql (9.6.2)Type "help" for help.postgres=# \d List of relations Schema | Name | Type | Owner --------+------+-------+---------- public | zx | table | postgres(1 row)postgres=# select * from zx; id ---- 1(1 row)
7、恢複設定檔如pg_hba.conf、postgresql.conf等
8、收集統計資訊
由於升級過程中不會把統計資訊傳到新庫系統資料表中,需要重新收集統計資訊。pg_upgrade的最給出了收集統計資訊的指令碼:
[[email protected] ~]$ ./analyze_new_cluster.sh This script will generate minimal optimizer statistics rapidlyso your system is usable, and then gather statistics twice morewith increasing accuracy. When it is done, your system willhave the default level of optimizer statistics.If you have used ALTER TABLE to modify the statistics target forany tables, you might want to remove them and restore them afterrunning this script because they will delay fast statistics generation.If you would like default statistics as quickly as possible, cancelthis script and run: "/opt/pgsql-9.6.2/bin/vacuumdb" --all --analyze-onlyvacuumdb: processing database "postgres": 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 "template1": Generating medium optimizer statistics (10 targets)vacuumdb: processing database "postgres": Generating default (full) optimizer statisticsvacuumdb: processing database "template1": Generating default (full) optimizer statisticsDone
9、升級成功後刪除舊版本軟體和資料。
官方文檔:https://www.postgresql.org/docs/9.6/static/pgupgrade.html
https://www.postgresql.org/docs/9.6/static/upgrading.html
本文出自 “DBA Fighting!” 部落格,請務必保留此出處http://hbxztc.blog.51cto.com/1587495/1910475
PostgreSQL升級之pg_upgrade升級