<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "升級PostgreSQL 9.4到PostgreSQL 12"]]></title>
		<link>https://forum.andowson.com/posts/list/12.page</link>
		<description><![CDATA[Latest messages posted in the topic "升級PostgreSQL 9.4到PostgreSQL 12"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>升級PostgreSQL 9.4到PostgreSQL 12</title>
				<description><![CDATA[ [size=18]升級PostgreSQL 9.4到PostgreSQL 12[/size] 
<br>
<br>
0.前言 
<br>
因CentOS 6.10已經EOL，故我已將其透過[url=https://docs.oracle.com/zh-tw/solutions/migrate-centos-ora-linux/index.html]script[/url]轉換成Oracle Linux 6.10 
<br>
<br>
目前[url=https://yum.postgresql.org/rpmchart/]yum.postgresql.org[/url]網站上面對RHEL/CentOS/Oracle Linux 6只有支援到PostgreSQL 12，沒有到PostgreSQL 13，因此以升級到PostgreSQL 12為目標 
<br>
<br>
本文是以Oracle Linux 6.10 x86_64上面原安裝PostgreSQL 9.4作為說明，預設執行身分為root，如需切換帳號為postgres，會以su - postgres開始 
<br>
<br>
警語: 升級資料庫有其風險，本文為作者自行實測心得，照作不保證一定可以成功或沒有其他未知風險，讀者應先於測試環境驗證新版資料庫對應用程式的相容性，並備妥資料庫備份還原步驟，以防萬一失敗時可以回到原先狀態。 
<br>
<br>
1.安裝新版的PostgreSQL 
<br>
1.1 安裝新版的PostgreSQL yum repository 
<br>
先開啟網頁到[url]https://www.postgresql.org/download/linux/redhat/[/url]，在Select platform找到目前使用的作業系統版本，例如Red Hat Enterprise, CentOS, Scientific or Oracle version 6，有個Copy script 
<br>
[code]cd /root/setup/database 
<br>
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-6-x86_64/pgdg-redhat-repo-latest.noarch.rpm[/code] 
<br>
<br>
1.2 安裝PostgreSQL新版的套件 
<br>
安裝新版的套件(必須安裝postgresql12-contrib套件才會有pg_upgrade) 
<br>
[code]sudo yum install -y postgresql12-server postgresql12-contrib[/code] 
<br>
<br>
2.升級前置動作 
<br>
2.1初始化新版PostgreSQL資料庫 
<br>
[code]sudo service postgresql-12 initdb[/code] 
<br>
<br>
2.2檢查是否成功建立資料庫所需目錄 
<br>
[code]ls /var/lib/pgsql/12/ 
<br>
cat /var/lib/pgsql/12/pgstartup.log[/code] 
<br>
<br>
<br>
3.停止網頁服務 
<br>
[code]sudo service tomcat stop[/code] 
<br>
<br>
注意/etc/crontab中有無設定定期檢查Tomcat是否有在跑的tomcat_monitor，有的話先註解掉該排程作業，以免系統又自己將Tomcat服務帶起來 
<br>
<br>
4.開始升級資料庫 
<br>
4.1.備份舊的資料 
<br>
執行pg_dumpall(建議使用新版的PostgreSQL目錄下的pg_dumpall) 
<br>
[code]su - postgres 
<br>
mkdir /tmp/pgsql 
<br>
/usr/pgsql-12/bin/pg_dumpall &gt; /tmp/pgsql/db.out 
<br>
exit[/code] 
<br>
4.2 關閉資料庫 
<br>
[code]sudo service postgresql-9.4 stop[/code] 
<br>
<br>
4.3 備份檔案系統 
<br>
切換身分為postgres(再多備份一次檔案系統) 
<br>
[code]su - postgres 
<br>
cp -pr /var/lib/pgsql/9.4 /tmp/pgsql/[/code] 
<br>
<br>
4.4 執行升版前的檢查pg_upgrade --check 
<br>
執行pg_upgrade(注意要使用新版的PostgreSQL目錄下的pg_upgrade) 
<br>
[code]/usr/pgsql-12/bin/pg_upgrade -b /usr/pgsql-9.4/bin -B /usr/pgsql-12/bin -d /var/lib/pgsql/9.4/data -D /var/lib/pgsql/12/data --check[/code] 
<br>
<br>
4.5 進行真正的升級動作，執行pg_upgrade 
<br>
[code]/usr/pgsql-12/bin/pg_upgrade -v -b /usr/pgsql-9.4/bin/ -B /usr/pgsql-12/bin/ -d /var/lib/pgsql/9.4/data -D /var/lib/pgsql/12/data[/code] 
<br>
<br>
最後看到下面這些畫面就完成了 
<br>
[code]Upgrade Complete 
<br>
---------------- 
<br>
Optimizer statistics are not transferred by pg_upgrade so, 
<br>
once you start the new server, consider running: 
<br>
 ./analyze_new_cluster.sh 
<br>
<br>
Running this script will delete the old cluster's data files: 
<br>
 ./delete_old_cluster.sh 
<br>
-bash-4.1$[/code] 
<br>
<br>
<br>
4.6 調整設定檔: pg_hba.conf, postgresql.conf 
<br>
可能新版會有新增一些參數，我們用diff比較一下差異，再把舊版修改的地方依據需要反映到新的版本去 
<br>
[code]diff /var/lib/pgsql/9.4/data/pg_hba.conf /var/lib/pgsql/12/data/pg_hba.conf 
<br>
vi /var/lib/pgsql/12/data/pg_hba.conf[/code] 
<br>
例如，我把local的peer改成trust，host的ident改成md5，有關replication的部分都先mark掉: 
<br>
[code] 
<br>
# TYPE DATABASE USER ADDRESS METHOD 
<br>
<br>
# "local" is for Unix domain socket connections only 
<br>
local all all trust 
<br>
# IPv4 local connections: 
<br>
host all all 127.0.0.1/32 md5 
<br>
# IPv6 local connections: 
<br>
host all all ::1/128 md5 
<br>
# Allow replication connections from localhost, by a user with the 
<br>
# replication privilege. 
<br>
#local replication all peer 
<br>
#host replication all 127.0.0.1/32 ident 
<br>
#host replication all ::1/128 ident[/code] 
<br>
postgresql.conf可能新版會有新增一些參數，我們用diff比較一下差異，再把舊版修改的地方依據需要反映到新的版本去 
<br>
[code]diff /var/lib/pgsql/9.4/data/postgresql.conf /var/lib/pgsql/12/data/postgresql.conf[/code] 
<br>
<br>
例如，我修改了: 
<br>
effective_cache_size = 256MB 
<br>
<br>
退出postgres 
<br>
[code]exit[/code] 
<br>
<br>
5.啟動新版的PostgreSQL服務 
<br>
[code]sudo chkconfig postgresql-12 on 
<br>
sudo service postgresql-12 start[/code] 
<br>
[code][root@www ~]# sudo service postgresql-12 start 
<br>
正在啟動 postgresql-12 服務： [ 確定 ][/code] 
<br>
<br>
6.升級後的優化作業 
<br>
[code]su - postgres 
<br>
./analyze_new_cluster.sh[/code] 
<br>
<br>
[code][root@www ~]# su - postgres 
<br>
-bash-4.1$ ./analyze_new_cluster.sh 
<br>
This script will generate minimal optimizer statistics rapidly 
<br>
so your system is usable, and then gather statistics twice more 
<br>
with increasing accuracy. When it is done, your system will 
<br>
have the default level of optimizer statistics. 
<br>
<br>
If you have used ALTER TABLE to modify the statistics target for 
<br>
any tables, you might want to remove them and restore them after 
<br>
running this script because they will delay fast statistics generation. 
<br>
<br>
If you would like default statistics as quickly as possible, cancel 
<br>
this script and run: 
<br>
 "/usr/pgsql-12/bin/vacuumdb" --all --analyze-only 
<br>
<br>
.... 
<br>
<br>
Done 
<br>
-bash-4.1$[/code] 
<br>
退出postgres 
<br>
[code]exit[/code] 
<br>
<br>
7.啟動網頁服務 
<br>
[code]sudo service tomcat start[/code] 
<br>
<br>
7.1 測試網頁服務是否正常 
<br>
<br>
8. 升級後的清理作業 
<br>
8.1 刪除舊的資料庫存放區 
<br>
[code]su - postgres 
<br>
./delete_old_cluster.sh[/code] 
<br>
<br>
<br>
8.2 移除舊版的PostgreSQL套件 
<br>
[code]yum remove postgresql94*[/code] 
<br>
<br>
<br>
8.3 刪除/tmp/pgsql目錄下的備份檔 
<br>
[code]rm -rf /tmp/pgsql[/code] 
<br>
<br>
[選擇性作業]8.4 重新啟用排程/etc/crontab中的tomcat_monitor 
<br>
<br>
至此就完成了PostgreSQL 12的升版作業]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/780/1554.page</guid>
				<link>https://forum.andowson.com/posts/preList/780/1554.page</link>
				<pubDate><![CDATA[Tue, 21 Sep 2021 03:48:54]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
	</channel>
</rss>