我爲我的Ruby on Rails應用程序(在Mac OS X 10.9上)使用PostgreSQL數據庫。如何將PostgreSQL從版本9.6升級到版本10.1而不丟失數據?
是否有關於如何升級PostgreSQL數據庫的任何詳細說明?
恐怕我會銷燬數據庫中的數據或搞砸了。
我爲我的Ruby on Rails應用程序(在Mac OS X 10.9上)使用PostgreSQL數據庫。如何將PostgreSQL從版本9.6升級到版本10.1而不丟失數據?
是否有關於如何升級PostgreSQL數據庫的任何詳細說明?
恐怕我會銷燬數據庫中的數據或搞砸了。
假設您使用home-brew安裝並升級Postgres,您可以執行以下步驟。
停止當前的Postgres服務器:
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
初始化新的10.1數據庫:
initdb /usr/local/var/postgres10.1 -E utf8
運行pg_upgrade
(注:換倉的版本,如果你」從下面以外的東西升級):
pg_upgrade -v \
-d /usr/local/var/postgres \
-D /usr/local/var/postgres10.1 \
-b /usr/local/Cellar/postgresql/9.6.5/bin/ \
-B /usr/local/Cellar/postgresql/10.1/bin/
移動新的數據到位:
cd /usr/local/var
mv postgres postgres9.6
mv postgres10.1 postgres
重新啓動Postgres的:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
檢查/usr/local/var/postgres/server.log
的細節,以確保新的服務器啓動正常。
最後,重新安裝滑軌pg
寶石
gem uninstall pg
gem install pg
我建議你花一些時間來閱讀PostgreSQL documentation明白你在上面的步驟做什麼,以儘量減少挫折。
謝謝!所有作品都像魅力! – Drakmail
我必須使用以下命令來初始化數據庫:'initdb /usr/local/var/postgres9.4 -E utf8 --lc-collate = C --lc-ctype = utf-8 --lc-monetary = C --lc-numeric = C' – sunsations
謝謝,工作就像一個魅力。 – Tache
user manual涵蓋了這個話題的深度。您可以:
pg_upgrade
in-place;或
pg_dump
和pg_restore
。
如果有疑問,請用垃圾箱處理。不要刪除舊的數據目錄,只要保存它以防萬一出錯/你犯了一個錯誤;這樣你就可以回到原來的9.3版本。
有關詳細信息,請參閱手冊。
如果您遇到困難,請發表詳細的問題,解釋您卡住的方式,位置以及您先嚐試的方式。這也取決於你如何安裝PostgreSQL,因爲PostgreSQL for OS X有幾種不同的「發行版」(不幸的是)。所以你需要提供這些信息。
這使用的是Ubuntu的pg_upgradecluster工具,它比PostgreSQL的pg_upgrade工具慢得多,當然只能在Ubuntu上使用。 – alfonx
@alfonx不正確。我把它放在我的Debian'jessie'上。有10多個數據庫和大約400MB的數據庫數據在轉眼間轉換。然後,我再次在SSD上使用虛擬Debian。 – dimitarvp
讓我糾正自己:pg_upgradecluster是「Debian PostgreSQL基礎架構」的一部分,因此只適用於基於Debian的發行版。關於速度pg_upgrade提供了「 - 鏈接」選項,鏈接不會在可能的情況下複製數據:http://www.postgresql.org/docs/9.4/static/pgupgrade.html – alfonx
站在其他貧困的生物通過本渣土trodding的肩膀上,我能夠按照以下步驟重新站起來,並升級到優勝美地運行後:
假設你已經使用家庭沖泡安裝並升級Postgres,您可以執行以下步驟。
停止當前的Postgres服務器:
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
初始化新的9.4數據庫:
initdb /usr/local/var/postgres9.4 -E utf8
安裝的Postgres 9.3(因爲它不再出現在我的機器):
brew install homebrew/versions/postgresql93
約塞米蒂升級過程中添加刪除的目錄:
mkdir -p /usr/local/var/postgres/{pg_tblspc,pg_twophase,pg_stat_tmp}/touch /usr/local/var/postgres/{pg_tblspc,pg_twophase,pg_stat_tmp}/.keep
運行pg_upgrade
:
pg_upgrade -v -d /usr/local/var/postgres -D /usr/local/var/postgres9.4 -b /usr/local/Cellar/postgresql93/9.3.5/bin/ -B /usr/local/Cellar/postgresql/9.4.0/bin/
將新的數據到位:
cd /usr/local/var
mv postgres postgres9.3
mv postgres9.4 postgres
重新啓動Postgres的:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
檢查/usr/local/var/postgres/server.log
的細節,以確保新服務器正常啓動。
最後,重新安裝相關的庫?
pip install --upgrade psycopg2
gem uninstall pg
gem install pg
pg_upgrade -v -d/usr/local/var/postgres -D /usr/local/var/postgres9.4 -b /usr/local/Cellar/postgresql93/9.3.*/bin/ -B/usr/local/Cellar/postgresql/9.4。*/bin /#次要版本可能不同。 –
謝謝你。我在遷移數據之前意外地運行了'brew cleanup',導致postgres9.3被卸載。這有幫助。 :) – markquezada
這裏是我們必須停止PostgreSQL的
sudo /etc/init.d/postgresql stop
創建一個名爲/etc/apt/sources.list.d/新文件Ubuntu用戶
首先解決pgdg.list並加入下面的行
deb http://apt.postgresql.org/pub/repos/apt/ utopic-pgdg main
Fol下面的命令低
wget -q -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-9.4
sudo pg_dropcluster --stop 9.4 main
sudo /etc/init.d/postgresql start
現在我們所擁有的一切,只是需要將其升級爲以下
sudo pg_upgradecluster 9.3 main
sudo pg_dropcluster 9.3 main
就是這樣。晴升級的集羣將運行在端口號5433.下面命令檢查它
sudo pg_lsclusters
如果您說「大多數升級的集羣將在端口號5433上運行」,則應該說「9.3集羣將在端口號5433上運行,以便您可以根據需要恢復」。 –
注意:對於ubuntu 14.04,請使用「trusty-pgdb」而不是utopic-pgdb – Johnny
請注意,這樣做不會執行就地升級。因此在1TB捲上我的700 GB數據庫完全沒有用處。 –
我的解決辦法做這兩種資源的組合:
https://gist.github.com/tamoyal/2ea1fcdf99c819b4e07d
和
http://www.gab.lc/articles/migration_postgresql_9-3_to_9-4
The second one 幫助更多然後第一個。也不要,不要按照步驟進行,因爲有些沒有必要。 另外,如果您無法通過postgres控制檯備份數據,您可以使用其他方法,並使用pgAdmin 3或其他程序對其進行備份,就像我的情況一樣。
此外,鏈接:https://help.ubuntu.com/stable/serverguide/postgresql.html 幫助設置加密的密碼,並設置md5用於驗證postgres用戶。
所有步驟完成後,要檢查的Postgres的終端server
版本上運行:
SHOW SERVER_VERSION;
這將輸出類似:
sudo -u postgres psql postgres
在Postgres的終端輸入密碼運行後
server_version
----------------
9.4.5
對於設置和啓動postgres我已經使用命令:
> sudo bash # root
> su postgres # postgres
> /etc/init.d/postgresql start
> /etc/init.d/postgresql stop
然後從文件還原數據庫:
> psql -f /home/ubuntu_username/Backup_93.sql postgres
或者,如果不起作用嘗試這一個:
> pg_restore --verbose --clean --no-acl --no-owner -h localhost -U postgres -d name_of_database ~/your_file.dump
如果你正在使用Rails做bundle exec rake db:migrate
拉代碼後:)
儘管所有的答案上面,這裏是我的5美分。
它適用於任何操作系統和任意到任意的Postgres版本。
postgresql.conf
→port
從5432
改爲5433
;bin
文件夾;pg_dumpall -p 5433 -U <username> | psql -p 5432 -U <username>
謝謝克里斯蒂安,這絕對是一個偉大而簡單的解決方案,我成功地從9.3這樣做到了9.5這樣 – fnicollet
在Windows 2012服務器上從9.1升級到9.5,工作得很好。 – Rolf
這個解決方案的一個問題是,如果你改變了一些Postgres配置文件(例如''postgresql.conf'或'pg_hba.conf'),你需要在新安裝中手動複製這些改變。相反,如果使用'pg_upgradecluster',配置文件將被複制到新集羣:http://manpages.ubuntu.com/manpages/precise/man8/pg_upgradecluster.8.html – Alphaaa
對於Mac通過自制軟件:
brew tap petere/postgresql
,
brew install <formula>
(如:brew install petere/postgresql/postgresql-9.6
)
刪除舊的Postgres:
brew unlink postgresql
brew link -f postgresql-9.6
如果發生任何錯誤,請不要忘記在每個步驟中閱讀並遵循brew指令。
在的Windows我一直在嘗試使用pg_upgrade
當面對不同的錯誤消息。
節省了大量的時間,我剛:
這對於9.5到9.6並且完美地工作。很高興能夠做到簡單準確的升級,而不是「最新」。我使用Big SQL來獲得正確的安裝程序(https://www.openscg.com/bigsql/postgresql/installers.jsp/)。 –
更新:升級9.6到10的過程是一樣的;只需修改命令以反映版本9.6
和10
,其中9.6
是舊版本版本並且10
是新版本版本。一定要相應地調整「舊」和「新」目錄。
我剛剛升級的PostgreSQL 9.5至9.6 Ubuntu上,以爲我會分享我的發現,因爲有一對夫婦要注意的OS /包特有的細微差別。
(我不想有轉儲和恢復數據手動,所以一些其他的答案在這裏是行不通的。)
總之,過程包括安裝PostgreSQL的新版本與舊版本(例如9.5和9.6)一起運行,然後運行pg_upgrade
二進制文件,在https://www.postgresql.org/docs/9.6/static/pgupgrade.html(詳細說明)中詳細介紹了該二進制文件。
pg_upgrade
唯一「棘手」的方面是未能爲參數傳遞正確的值,或者未能在執行命令之前以正確的用戶或cd
登錄到正確的位置,可能導致隱晦錯誤消息。
在Ubuntu(也可能是Debian)上,假設您使用的是「官方」回購deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main
,並且假定您沒有更改默認文件系統路徑或運行時選項,則以下過程應該執行此作業。
安裝新版本(注意,我們指定9.6
,明確地):
sudo apt install postgresql-9.6
一旦安裝成功,兩個版本將運行並排的一面,但在不同的端口。安裝輸出提到了這一點,在底部,但它很容易忽視:
Creating new cluster 9.6/main ...
config /etc/postgresql/9.6/main
data /var/lib/postgresql/9.6/main
locale en_US.UTF-8
socket /var/run/postgresql
port 5433
停止兩個服務器實例(這將在同一時間同時停止):
sudo systemctl stop postgresql
切換到專用的PostgreSQL系統用戶:
su postgres
搬進他的家目錄(未能做到這一點會導致錯誤):
cd ~
pg_upgrade
需要下列輸入(pg_upgrade --help
告訴我們這一點):
When you run pg_upgrade, you must provide the following information:
the data directory for the old cluster (-d DATADIR)
the data directory for the new cluster (-D DATADIR)
the "bin" directory for the old version (-b BINDIR)
the "bin" directory for the new version (-B BINDIR)
這些輸入可以用 「長文件名」,被指定爲使他們更容易想象:
-b, --old-bindir=BINDIR old cluster executable directory
-B, --new-bindir=BINDIR new cluster executable directory
-d, --old-datadir=DATADIR old cluster data directory
-D, --new-datadir=DATADIR new cluster data directory
我們還必須通過--new-options
開關,因爲如果不這樣做會導致以下情況:
connection to database failed: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/lib/postgresql/.s.PGSQL.50432"?
發生這種情況是因爲缺少此開關時會應用默認配置選項,這會導致使用不正確的連接選項,從而導致套接字錯誤。
從新的PostgreSQL版本執行pg_upgrade
命令:
/usr/lib/postgresql/9.6/bin/pg_upgrade --old-bindir=/usr/lib/postgresql/9.5/bin --new-bindir=/usr/lib/postgresql/9.6/bin --old-datadir=/var/lib/postgresql/9.5/main --new-datadir=/var/lib/postgresql/9.6/main --old-options=-cconfig_file=/etc/postgresql/9.5/main/postgresql.conf --new-options=-cconfig_file=/etc/postgresql/9.6/main/postgresql.conf
註銷專用系統用戶帳戶:
exit
升級已經完成,現在但,新實例將綁定到端口5433
(標準默認值爲5432
),因此如果嘗試測試在「切入」它之前的新實例。
啓動服務器正常(再次,這將同時啓動新老實例):
systemctl start postgresql
如果你想在新版本的默認,你需要編輯的有效配置文件,例如,/etc/postgresql/9.6/main/postgresql.conf
,並確保該端口被定義爲這樣的:
port = 5432
如果你做到這一點,無論是在同一時間改變了舊版本的端口號5433
(啓動服務之前),或者乾脆刪除舊版本(這將不刪除您的實際的數據庫內容,你將需要使用apt --purge remove postgresql-9.5
要做到這一點):
apt remove postgresql-9.5
上面的命令將停止所有實例,所以你需要開始新的實例最後一次與:
systemctl start postgresql
作爲筆記的最後一點,不要忘了考慮pg_upgrade
的好建議:
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
對於我在Mac Yosemite,PostgreSQL 9.2 - > 9。5:'sudo su postgres',在安裝的pg_hba.conf中將所有METHOD都更改爲在pg_upgrade之前進行信任,在/ private/tmp中運行pg_upgrade not〜沒有工作,因此''sudo mkdir/foobar'使用'chmod 777/foobar'和在那裏運行。最後是pg_upgrade命令:'/Library/PostgreSQL/9.5/bin/pg_upgrade -b /Library/PostgreSQL/9.2/bin -B/Library/PostgreSQL/9.5/bin -d /Library/PostgreSQL/9.2/data -D/Library/PostgreSQL/9.5/data -o -cconfig_file =/Library/PostgreSQL/9.2/data/postgresql.conf -O -cconfig_file =/Library/PostgreSQL/9.5/data/postgresql.conf' –
我覺得這是最好的鏈接,您的解決方案,以更新的Postgres 9.6
https://sandymadaan.wordpress.com/2017/02/21/upgrade-postgresql9-3-9-6-in-ubuntu-retaining-the-databases/
貌似解決方案已經出爐到現在家釀:
$ brew info postgresql
...
==> Caveats
To migrate existing data from a previous major version of PostgreSQL run:
brew postgresql-upgrade-database
....
如果您使用的自制和自制軟件服務,你可能只是做:
brew services stop postgresql
brew upgrade postgresql
brew postgresql-upgrade-database
brew services start postgresql
我覺得如果你使用的是先進的Postgres的功能,這可能不是完全的工作,但它完美地爲我工作。
無論如何都要進行備份。 –