升級你Mac上的PostgreSQL

Upgrade your PostgreSQL in Mac

起因

我本地安裝了PostgreSQL的9.5版本,在某次神秘力量的引導下,我無知無覺的升級了PostgreSQL到9.6版本而沒有檢查兼容性
今天在試圖運行PostgreSQL服務時得到了報錯:

pg_ctl: another server might be running; trying to start server anyway

然后naive的我試圖終止PostgreSQL服務,得到了另一個報錯:

pg_ctl: could not send stop signal (PID: 60961): No such process

嚇得我趕緊查看了一下PostgreSQL的log:

tail -f /usr/local/var/postgres/server.log

得到的日志信息如下:

FATAL:  database files are incompatible with server
DETAIL:  The data directory was initialized by PostgreSQL version 9.5, which is not compatible with this version 9.6.1.

長舒一口氣,只是PostgreSQL例行的版本升級不兼容而已

升級

我是從9.5升級到9.6,因此如果你的PostgreSQL的版本號跟我的不同,需要改動下面命令中的版本號,執(zhí)行的每個命令我都會盡量標注清楚我們在干什么

1、終止PostgreSQL服務
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
launchctl在Apple官網(wǎng)找到的文檔比較老舊,這里附一個wikipedia的鏈接,如果你希望看到更完整的關于該命令的介紹,建議在終端執(zhí)行:man launchctl進行查看
簡單來說:
launchctl命令用于加載(load)和卸載(unload)系統(tǒng)啟動時自動運行的服務,服務的具體描述儲存在對應的plist文件,位置一般是~/Library/LaunchAgents/Library/LaunchAgents。上面命令中的plist很明顯能看出我的PostgreSQL是使用Homebrew安裝的
unload子命令額外能夠終止正在運行的服務
-w參數(shù)用于復寫plist文件中不可用鍵對應的值為false(對應load子命令)或者true(unload子命令),以讓你的命令能夠不被神秘力量阻止運行

2、備份現(xiàn)有PostgreSQL數(shù)據(jù)

mv /usr/local/var/postgres /usr/local/var/postgres95

將現(xiàn)有的PostgreSQL數(shù)據(jù)文件夾postgres備份為postgres95

3、升級Homebrew

brew update

4、升級PostgreSQL

brew upgrade postgresql

5、初始化PostgreSQL

initdb /usr/local/var/postgres -E utf8

這是初始化PostgreSQL數(shù)據(jù)庫的標準方式了,使用utf8字符集編碼

6、升級PostgreSQL的bin文件夾及你舊版本數(shù)據(jù)庫數(shù)據(jù)

pg_upgrade -b /usr/local/Cellar/postgresql/9.5.5/bin -B /usr/local/Cellar/postgresql/9.6.1/bin -d /usr/local/var/postgres95 -D /usr/local/var/postgres

該命令有點長,不過實際很簡單,只需要知道下面一條規(guī)則:

pg_upgrade -b oldbindir -B newbindir -d olddatadir -D newdatadir [option...]

如果你不知道你的PostgreSQL的bin目錄是什么,可以使用ls -f /usr/local/Cellar/postgresql該命令查看你的postgresql目錄下的PostgreSQL版本文件夾,每個版本號下都會有對應的bin目錄
上面命令中:
postgresql/9.5.5/bin是9.5.5(原有)版本數(shù)據(jù)庫的bin文件夾
postgresql/9.6.1/bin是9.6.1(當前)版本數(shù)據(jù)庫的bin文件夾
postgres95是我們在第2步備份的原數(shù)據(jù)文件
postgres是當前PostgreSQL數(shù)據(jù)庫文件目錄

7、拷貝plist文件到正確位置

cp /usr/local/Cellar/postgresql/9.6.1/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/

將升級后的PostgreSQL數(shù)據(jù)庫的plist文件拷貝到其應在的位置

8、清理備份數(shù)據(jù)文件

rm -rf /usr/local/var/postgres95

將第2步備份的數(shù)據(jù)文件刪除掉

9、啟動PostgreSQL

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

數(shù)據(jù)庫版本升級完畢,數(shù)據(jù)文件升級完畢,啟動我們的PostgreSQL試試,理論上,能正常啟動的,相信我

10、關閉PostgreSQL

pg_ctl -D /usr/local/var/postgres stop -s -m fast

關閉PostgreSQL試試,理論上也能正常關閉的

另外

由于PostgreSQL啟動和終止服務的命令過于反人類,建議將其寫入你的bash_profile文件,以下是我本地的alias

#postgresql start&stop
alias psqlstart='pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start'
alias psqlstop='pg_ctl -D /usr/local/var/postgres stop -s -m fast'

記得在每次修改了bash_profile文件后執(zhí)行source ~/.bash_profile或者重啟你的終端或機器

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容