本文利用pg_upgrade實現將8.4.18版本升級到9.5.0版本,8.4.18版本爲RedHat系統自帶pg數據庫版本。html
環境:Red Hat Enterprise Linux Server release 6.5 (Santiago) X86-64sql
查看手冊看到能夠利用pg_upgrade實現從8.4到9.5版本直接升級:shell
pg_upgrade supports upgrades from 8.3.X and later to the current major release of PostgreSQL, including snapshot and alpha releases.數據庫
下載postgresql9.5.0版本:centos
1.解壓:api
tar jxvf postgresql-9.5.0.tar.bz2安全
2.編譯:bash
./configure --prefix=/usr/local/pgsql9.5.0--with-pgport=5434 --with-wal-blocksize=16
這裏在編譯時帶with-wal-blocksize=16配置參數致使後面升級出現錯誤,具體到後面解釋。同時注意指定目錄,方便後面管理dom
編譯過程可能碰到錯誤:socket
configure: error: readline library notfound If you have readline already installed, see config.log for detailson the failure. It is possible the compiler isnt lookingin the proper directory. Use -- without-readline to disable readlinesupport.
解決方法:
rpm -qa | grep readline yum search readline yum -y install -y readline-deve
安裝完畢readline,從新執行上面編譯命令。
3.安裝:
gmake world gmake install -world //此處要加world
4.初始化數據庫:
4.1 建立data數據目錄:(可另指定其餘位置,此處將指定在pg安裝目錄)
cd /usr/local/pgsql9.5.0 mkdir data
4.2 修改data目錄權限,將其賦給postgres:
[root@localhost pgsql9.5.0]# chown -R postgres:postgres /usr/local/pgsql9.5.0/data [root@localhost pgsql9.5.0]# chmod -R 700 /usr/local/pgsql9.5.0/data
4.3 初始化:
-bash-4.1$ /usr/local/pgsql9.5.0/bin/initdb -E UTF8 -D /usr/local/pgsql9.5.0/data --locale=C -U postgres -W //locale=C這裏在升級時報錯,後續具體解釋
執行後:
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 "C".
The default text search configuration will be set to "english".
Data page checksums are disabled.
....
初始化過程當中會要求設置一個新的密碼:postgres
...
Success. You can now start the database server using:
/usr/local/pgsql9.5.0/bin/pg_ctl -D /usr/local/pgsql9.5.0/data -l logfile start
注意:
初始化須要在postgres用戶下進行,不能再root下:
[root@localhost pgsql9.5.0]# /usr/local/pgsql9.5.0/bin/initdb -E UTF8 -D /usr/local/pgsql9.5.0/data --locale=C -U postgres -W initdb: cannot be run as root Please log in (using, e.g., "su") as the (unprivileged) user that will own the server process.
4.3 安裝pg_upgrade插件:
cd contrib gamke gmake install
:其餘插件相似安裝,以安裝第三方插件函數調試工具pldebugger爲示例:http://my.oschina.net/ensn/blog/644727
5.修改postgresql.conf和pg_hba.conf文件:
由於升級須要屢次鏈接新老集羣數據庫實例, 因此修改成使用本地trust認證.
host all all 127.0.0.1/32 trust
端口使用注意使用不一樣的監聽端口.(使用編譯時候的指定端口:5434)
6.停老庫:(新庫沒有啓動,故這裏不停)
-bash-4.1$ pg_ctl stop -m fast -D /var/lib/pgsql/data waiting for server to shut down.... done server stopped
7.更新:
首先建立一個用戶執行升級的目錄,該目錄權限給postgres用戶(執行更新時,會產一些升級文件,建立upgrade目錄存儲該些文件)
su - root cd /usr/local/pgsql9.5.0 mkdir upgrade chown postgres:postgres upgrade su - postgres cd /usr/local/pgsql9.5.0/upgrade
7.1檢驗更新:
-bash-4.1$ /usr/local/pgsql9.5.0/bin/pg_upgrade -c -b /usr/bin -B /usr/local/pgsql9.5.0/bin -d /var/lib/pgsql/data -D /usr/local/pgsql9.5.0/data -p 5432 -P 5434 -U postgres //-c表示檢驗,-b/-B 表示老/新庫bin目錄,-d/-D表示老/新庫數據目錄,具體命令參考手冊
檢驗時遇見如下問題:
1).wal-blocksize 問題
該問題是由先後兩個版本wal-blocksize不一樣,由於以前在安裝新版本時指定了wal-blocksize的大小爲16k,但老版本是默認安裝的,默認大小爲8k,該參數大小隻能在安裝時改變,故須要從新安裝pg數據庫來解決該問題。
2).lc_collate values for database "postgres" do not match: old "en_US.UTF-8", new "C" Failure, exiting
解決:刪除data數據目錄,從新作一次初始化,修改locale=en_US.UTF-8
/usr/local/pgsql9.5.0/bin/initdb -E UTF8 -D /usr/local/pgsql9.5.0/data --locale=en_US.UTF-8 -U postgres -W
解決以上問題之後從新檢驗更新:
-bash-4.1$ /usr/local/pgsql9.5.0/bin/pg_upgrade -c -b /usr/bin -B /usr/local/pgsql9.5.0/bin -d /var/lib/pgsql/data -D /usr/local/pgsql9.5.0/data -p 5432 -P 5434 -U postgres
執行後:
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 invalid "line" user columns ok Checking for large objects ok Checking for presence of required libraries ok Checking database user is the install user ok Checking for prepared transactions ok *Clusters are compatible*
以上說明能夠執行更新。
7.2 執行更新
-bash-4.1$ /usr/local/pgsql9.5.0/bin/pg_upgrade -b /usr/bin -B /usr/local/pgsql9.5.0/bin -d /var/lib/pgsql/data -D /usr/local/pgsql9.5.0/data -p 5432 -P 5434
pg_upgrade有兩種升級方式,一個是缺省的經過拷貝數據文件到新的data目錄下,一個是建立硬連接。拷貝的方式升級較慢,可是原庫還可用;硬連接的方式升級較快,可是原庫不可用。以上是缺省的方式,硬連接方式升級的命令只須要添加--link
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 invalid "line" user columns ok Checking for large objects 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 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 Restoring database schemas in the new cluster ok Setting minmxid counter in new cluster ok Creating newly-required TOAST tables 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 large objects 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
升級過程是拷貝原來目錄的數據到新版本指定的目錄中,最後提示生成兩個腳本,一個是analyze_new_cluster.sh,須要在新版本中執行,用 來收集統計信息,另外一個是 delete_old_cluster.sh,用來刪除舊版本集羣數據,固然爲了安全起見能夠等系統運行幾天沒問題再來刪除。
8.啓動新數據庫
-bash-4.1$ pg_ctl start -D /usr/local/pgsql9.5.0/data
報錯:
server starting -bash-4.1$ FATAL: unrecognized configuration parameter "dynamic_shared_memory_type"
解決方法:
緣由是由於版本不一致致使,多是個人主機上面被我編譯了兩個版本致使的,以前8.4postgresql.conf文件中沒有該參數,升級後9.5的配置文件中有該參數,故出現不一致,
經過絕對路徑啓動數據庫:
[root@localhost ~]# su - postgres -bash-4.1$ /usr/local/pgsql9.5.0/bin/pg_ctl start -D /usr/local/pgsql9.5.0/data server starting -bash-4.1$ LOG: database system was shut down at 2016-03-08 10:55:18 CST LOG: MultiXact member wraparound protections are now enabled LOG: autovacuum launcher started LOG: database system is ready to accept connections
進入數據庫,此時psql沒法進入:
-bash-4.1$ psql psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
經過指定psql路徑後進入:
-bash-4.1$ /usr/local/pgsql9.5.0/bin/psql --指定路徑 psql (9.5.0) Type "help" for help. postgres=#
9.執行收集統計信息的腳本:
-bash-4.1$ /usr/local/pgsql9.5.0/upgrade/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: "/usr/local/pgsql9.5.0/bin/vacuumdb" --all --analyze-onlyvacuumdb: processing database "gedbs": Generating minimal optimizer statistics (1 target)vacuumdb: processing database "postgres": Generating minimal optimizer statistics (1 target)vacuumdb: processing database "template1": Generating minimal optimizer statistics (1 target)vacuumdb: processing database "gedbs": Generating medium optimizer statistics (10 targets)vacuumdb: processing database "postgres": Generating medium optimizer statistics (10 targets)vacuumdb: processing database "template1": Generating medium optimizer statistics (10 targets)vacuumdb: processing database "gedbs": Generating default (full) optimizer statisticsvacuumdb: processing database "postgres": Generating default (full) optimizer statisticsvacuumdb: processing database "template1": Generating default (full) optimizer statistics Done
10.修改bash_profile文件中的PGDATA, PGPORT等參數(對postgres用戶有效,客戶端用其餘用戶登錄可能無效,修改/etc/profile對全部用戶有效),修改PATH搜索路徑,
否則在執行psql是會報版本不一致警告:
WARNING: psql version 8.4, server version 9.5.
11.啓動數據庫,pgadmin鏈接
報錯:
configure FATAL: password authentication failed for user "postgres"
解決方法:
修改postgres用戶密碼,
ALTER USER postgres PASSWORD 'newPassword';
參考:http://stackoverflow.com/questions/7695962/postgresql-password-authentication-failed-for-user-postgres
12.新數據庫穩定後,刪除老版本軟件
[root@localhost upgrade]# cat delete_old_cluster.sh #!/bin/sh rm -rf '/var/lib/pgsql/data'
$ ./delete_old_cluster.sh
參考:
http://www.postgresql.org/docs/current/static/pgupgrade.html
http://my.oschina.net/liuyuanyuangogo/blog/500229
http://xmarker.blog.163.com/blog/static/22648405720145203428811/
http://906179271.iteye.com/blog/2263218
http://francs3.blog.163.com/blog/static/40576727201519521050/
http://wangwei.info/upgrade-postgresql-centos/