pg_upgradeでつまずいた
環境
- CentOS: 7.2.1511 (Core)
- PostgreSQL(old): 9.2.18
- PostgreSQL(new): 9.6.1
経緯
gitlab 9.0からデフォルトのPostgreSQLのバージョンが9.6になる(参考記事)とのことだった。
私の環境では、CentOSのrepoからyumでインストールした9.2が入っていたので、このタイミングで9.6をインストールすることにした。
CentOS7にPostgreSQL9.6をyumで、インストール - Qiita や PostgreSQL9.4 から PostgreSQL9.5 に pg_upgrade する - Qiita を参考にしながら作業を行った。
PostgreSQL9.6のインストール、DBの初期化までは順調に行き、pg_upgrade
を実行した。
$ /usr/pgsql-9.6/bin/pg_upgrade -d /var/lib/pgsql/data -D /var/lib/pgsql/9.6/data -b /usr/bin/ -B /usr/pgsql-9.6/bin/
すると、以下のようなエラーが発生した。
Performing Consistency Checks ----------------------------- Checking cluster versions ok *failure* Consult the last few lines of "pg_upgrade_server.log" for the probable cause of the failure. connection to database failed: could not connect to server: Connection refused Is the server running locally and accepting connections on Unix domain socket "/var/lib/pgsql/data/.s.PGSQL.50432"? could not connect to old postmaster started with the command: "/usr/bin/pg_ctl" -w -l "pg_upgrade_server.log" -D "/var/lib/pgsql/data" -o "-p 50432 -b -c listen_addresses='' -c unix_socket_permissions=0700 -c unix_socket_directory='/var/lib/pgsql/data'" start
ログを見ると、旧バージョンのPostgreSQLの起動に失敗している様子。
加えてpg_upgrade_server.log
を見てみると、
command: "/usr/bin/pg_ctl" -w -l "pg_upgrade_server.log" -D "/var/lib/pgsql/data" -o "-p 50432 -b -c listen_addresses='' -c unix_socket_permissions=0700 -c unix_socket_directory='/var/lib/pgsql/data'" start >> "pg_upgrade_server.log" 2>&1 サーバの起動完了を待っています....FATAL: 設定パラメータ"unix_socket_directory"は不明です 待機処理が停止されました pg_ctl: サーバを起動できませんでした。 ログ出力を確認してください。
設定パラメータ unix_socket_directory
が不明とのエラー。PostgreSQL9.2ではunix_socket_directories
を使います。しかし、このコマンドはpg_upgrade
が自動で生成しているものなのでどうしようもありません。
そこで、「pg_upgrade unix_socket_directory」などで検索。すると以下のような記事を発見。
これを見ると、GET_MAJOR_VERSION
の判定でunix_socket_directory
とunix_socket_directories
をスイッチするが、その判定の条件が間違っているために違うパラメータがセットされているようだ。これにならって、Postgres9.6のpg_upgradeにパッチを当ててみます。
対応
1. ソースをダウンロードして展開
$ cd /usr/local/src $ wget https://ftp.postgresql.org/pub/source/v9.6.1/postgresql-9.6.1.tar.gz $ tar zxf postgresql-9.6.1.tar.gz $ cd postgresql-9.6.1
2. src/bin/pg_upgrade/server.c
を修正
(GET_MAJOR_VERSION(cluster->major_version) < 903) ?
となっているところを
(GET_MAJOR_VERSION(cluster->major_version) < 900) ?
と変更する
3. ビルドする(インストールはしない)
$ ./configure && make
4. yumバージョンとmakeバージョンを置き換え
$ cd /usr/pgsql-9.6/bin/ $ mv pg_upgrade pg_upgrade.rpm $ cp /usr/local/src/postgresql-9.6.1/src/bin/pg_upgrade/pg_upgrade .
ここまで終わればもう一度pg_upgrade
を実行すれば動くはずです
$ /usr/pgsql-9.6/bin/pg_upgrade -d /var/lib/pgsql/data -D /var/lib/pgsql/9.6/data -b /usr/bin/ -B /usr/pgsql-9.6/bin/