會員註冊 / 登入  |  電腦版  |  Jump to bottom of page

PostgreSQL » 升級PostgreSQL 9.3到PostgreSQL 9.4

發表人: andowson, 七段學員
2015-07-16 00:16:05
本文是以CentOS 6.6 x86_64上面原安裝PostgreSQL 9.3作為說明,預設執行身分為root,如需切換為postgres,會以su - postgres開始

1.安裝新版的PostgreSQL
1.1 安裝新版的PostgreSQL repository
先開啟網頁到http://yum.postgresql.org/,在Available PostgreSQL Releases點選我們要升級的版本的連結,這裡應該是9.4,然後找到目前使用的作業系統版本,例如CentOS 6 - x86_64,在這個連結上按滑鼠右鍵複製連結,取得URL,回到PuTTY視窗
cd /root/setup/database

yum install -y http://yum.postgresql.org/9.4/redhat/rhel-6-x86_64/pgdg-centos94-9.4-1.noarch.rpm


1.2 安裝PostgreSQL新版的套件
先找出之前安裝了那些套件: rpm -qa | grep postgresql93
[root@www database]# rpm -qa | grep postgresql93

postgresql93-9.3.6-1PGDG.rhel6.x86_64
postgresql93-libs-9.3.6-1PGDG.rhel6.x86_64
postgresql93-server-9.3.6-1PGDG.rhel6.x86_64
postgresql93-contrib-9.3.6-1PGDG.rhel6.x86_64


安裝新版的套件(必須安裝postgresql94-contrib套件才會有pg_upgrade)
yum install postgresql94-server postgresql94-libs postgresql94 postgresql94-contrib



2.升級前置動作
2.1初始化新版PostgreSQL資料庫
/etc/init.d/postgresql-9.4 initdb



2.2檢查是否成功建立資料庫所需目錄
ls /var/lib/pgsql/9.4/

cat /var/lib/pgsql/9.4/pgstartup.log



3.停止網頁服務
service tomcat stop


注意/etc/crontab中有無設定定期檢查Tomcat是否有在跑的tomcat_monitor,有的話先註解掉該排程作業,以免系統又自己將Tomcat服務帶起來

4.開始升級資料庫
4.1.備份舊的資料
su - postgres

mkdir /tmp/pgsql
pg_dumpall > /tmp/pgsql/db.out
exit

4.2 關閉資料庫
service postgresql-9.3 stop


4.3 備份檔案系統
切換身分為postgres(再多備份一次檔案系統)
su - postgres

cp -pr /var/lib/pgsql/9.3 /tmp/pgsql/


4.4 執行升版前的檢查pg_upgrade --check
執行pg_upgrade(注意要使用新版的PostgreSQL目錄下的pg_upgrade)
/usr/pgsql-9.4/bin/pg_upgrade -b /usr/pgsql-9.3/bin -B /usr/pgsql-9.4/bin -d /var/lib/pgsql/9.3/data -D /var/lib/pgsql/9.4/data --check


4.5 進行真正的升級動作,執行pg_upgrade
/usr/pgsql-9.4/bin/pg_upgrade -v -b /usr/pgsql-9.3/bin/ -B /usr/pgsql-9.4/bin/ -d /var/lib/pgsql/9.3/data -D /var/lib/pgsql/9.4/data


最後看到下面這些畫面就完成了
reating 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
-bash-4.1$


4.6 調整設定檔: pg_hba.conf, postgresql.conf
可能新版會有新增一些參數,我們用diff比較一下差異,再把舊版修改的地方依據需要反映到新的版本去
diff /var/lib/pgsql/9.3/data/pg_hba.conf /var/lib/pgsql/9.4/data/pg_hba.conf

vi /var/lib/pgsql/9.4/data/pg_hba.conf


postgresql.conf可能新版會有新增一些參數,我們用diff比較一下差異,再把舊版修改的地方依據需要反映到新的版本去
diff /var/lib/pgsql/9.3/data/postgresql.conf /var/lib/pgsql/9.4/data/postgresql.conf


例如,我修改了:
effective_cache_size = 256MB


退出postgres
exit


5.啟動新版的PostgreSQL服務
service postgresql-9.4 start

[root@www database]# service postgresql-9.4 start
正在啟動 postgresql-9.4 服務: [ 確定 ]

6.升級後的優化作業
su - postgres

./analyze_new_cluster.sh

[root@www database]# su - postgres
-bash-4.1$ ./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-9.4/bin/vacuumdb" --all --analyze-only

vacuumdb: vacuuming database "jforum"
Generating minimal optimizer statistics (1 target)
vacuumdb: vacuuming database "postgres"
Generating minimal optimizer statistics (1 target)
vacuumdb: vacuuming database "template1"
Generating minimal optimizer statistics (1 target)
vacuumdb: vacuuming database "jforum"
Generating medium optimizer statistics (10 targets)
vacuumdb: vacuuming database "postgres"
Generating medium optimizer statistics (10 targets)
vacuumdb: vacuuming database "template1"
Generating default (full) optimizer statistics
vacuumdb: vacuuming database "jforum"
Generating default (full) optimizer statistics
vacuumdb: vacuuming database "postgres"
Generating default (full) optimizer statistics
vacuumdb: vacuuming database "template1"

Done
-bash-4.1$


7.啟動網頁服務
service tomcat start


7.1 測試網頁服務是否正常

8. 升級後的清理作業
8.1 刪除舊的資料庫存放區
su - postgres

./delete_old_cluster.sh



8.2 移除舊版的PostgreSQL套件
yum remove postgresql93* pgdg-centos93



8.3 刪除/tmp/pgsql目錄下的備份檔
rm -rf /tmp/pgsql



[選擇性作業]8.4 重新啟用排程/etc/crontab中的tomcat_monitor

至此就完成了PostgreSQL 9.4的升版作業




會員註冊 / 登入  |  電腦版  |  Jump to top of page