LoginSignup
2
1

More than 5 years have passed since last update.

pg_upgradeによるPostgreSQLバージョンアップ時の勘所

Last updated at Posted at 2016-12-16

開発環境のMacOS Xのバージョンを上げたらPostgresが強制バージョンアップして起動しなくなった。

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

対処の基本はこれらの記事に従えばよい。

homebrewで PostgreSQL 9.3へバージョンアップ方法
PostgreSQL9.4 から PostgreSQL9.5 に pg_upgrade する

が、ところどころにハマりポイントがあって時間を蒸発させられるので備忘録を記載する。なお筆者の環境では旧バージョンが9.4、アプデ時に適用されたバージョンが9.5で、試行錯誤中にupgradeをかけて9.6が入ったので、Yosemite10.10.5下での9.4→9.6の移行作業について記載する。

パスが通っているコマンドのバージョン

自分の場合は以下を参考に、pg_upgradeをするために9.4をインストールし直した。
Homebrewで旧バージョンをインストールする方法(brew verionsはもう使えない)

このとき、パッケージのコンフリクトにより、unlinkを求められるので、素直に従った。

$ brew install postgresql94
==> Installing postgresql94 from homebrew/versions
Error: Cannot install homebrew/versions/postgresql94 because conflicting formulae are installed.

  postgresql: because Differing versions of the same formula.

Please `brew unlink postgresql` before continuing.

Unlinking removes a formula's symlinks from /usr/local. You can
link the formula again after the install finishes. You can --force this
install, but the build may fail or cause obscure side-effects in the
resulting software.
$ brew unlink postgresql
Unlinking /usr/local/Cellar/postgresql/9.6.1... 375 symlinks removed

そうすると、/user/local/binにあるシンボリックリンクが9.4を指してしまうので、pg_upgradeのコマンドが上手く働かないといったことが発生した。
そのため多少脳筋だが、もう1回brew unlinkして9.6をインストールし直し、シンボリックリンクを作り直した。コマンドのバージョン確認はpsql -Vが手っ取り早い。

環境変数と自動起動ファイルのパス

参考にしたリンクを見るとわかるが、新バージョンのデータディレクトリを別に作るということは環境変数のPGDATAと自動起動ファイル~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist指す向きを変更してやる必要がある。自分の場合は毎回それをやると絶対に管理できなくなる気がしたので、データ移行後にデータディレクトリを改名したが、その結果以下のパーミッション問題が発生した。

postgresディレクトリのパーミッション

移行が完了して起動後、コマンドが通ったのに待てど暮せどデータベースにアクセスできない。確認したところpidファイルが作成されてないので起動までいけていない。そういうときにログを確認するとパーミッションのエラーが出ていることがある。

$ tail postgres/server.log 
FATAL:  data directory "/usr/local/var/postgres" has group or world access
DETAIL:  Permissions should be u=rwx (0700).

今回のように、移行先フォルダを動かすだのなんだのしているといつの間にかパーミッションが変わっている事があるので要求されている通りにchmod 700 postgresと言った具合に設定し直さないといけない。この場合のshouldはRFCと同様に「する必要がある」という強いニュアンスだと解釈すべきなのだろう。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1