PostgreSQL 9.x系から10系にアップデート
年末にかけて、自分のMacの環境をアップデートしたり、整理したりしてたので、その備忘録です。
発生していたエラー
2018-01-02 16:49:48.272 JST [51799] FATAL: database files are incompatible with server 2018-01-02 16:49:48.272 JST [51799] DETAIL: The data directory was initialized by PostgreSQL version 9.6, which is not compatible with this version 10.1.
dataディレクトリのバージョンが9.6だと怒られて、PostgreSQLサーバが起動してない状態でした。
対応した内容
- 9.6のデータディレクトリを退避
mv /usr/local/var/postgres /usr/local/var/postgres96
- 10系でinitdbを実行
initdb /usr/local/var/postgres
- 9.6系のデータをバックアップ
/usr/local/Cellar/postgresql/9.6.2/bin/pg_ctl start -D /usr/local/var/postgres96 /usr/local/Cellar/postgresql/9.6.2/bin/pg_dumpall > pgalldump.sql /usr/local/Cellar/postgresql/9.6.2/bin/pg_ctl stop -D /usr/local/var/postgres96
- plistファイル修正(pg_ctlコマンドに変更)
~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>KeepAlive</key> <true/> <key>Label</key> <string>homebrew.mxcl.postgresql</string> <key>ProgramArguments</key> <array> <string>/usr/local/opt/postgresql/bin/pg_ctl</string> <string>start</string> <string>-D</string> <string>/usr/local/var/postgres</string> </array> <key>RunAtLoad</key> <true/> <key>WorkingDirectory</key> <string>/usr/local</string> <key>StandardErrorPath</key> <string>/usr/local/var/log/postgres.log</string> </dict> </plist>
- 10系を起動
brew services start postgresql
- 9系のデータからリストア
psql -d postgres -f pgalldump.sql
以上で、9系のデータを無事10系に移行することが出来ました。