Skip to content

Instantly share code, notes, and snippets.

@mochiz
Last active December 29, 2015 19:29
Show Gist options
  • Save mochiz/7717917 to your computer and use it in GitHub Desktop.
Save mochiz/7717917 to your computer and use it in GitHub Desktop.
Postgres.appを9.2から9.3にアップグレードした時の手順メモ

Postgres.appを9.2から9.3にアップグレードした時の手順メモ

基本は @junyagist の手順通りですが、途中でいろいろつまづいたところがあったので備忘録的に。

私が遭遇した問題は以下のとおりです。

  • ロケールが違う => 既存DBのcollate, ctypeがja_JPだった
  • 同名のデータベースが存在する => 新DBにasakuraというユーザー名のデータベースが作成されていた

解決方法を含めた手順はこんな感じ。

Step 1 アプリをインストールする

Postgres93.app を ホームページからダウンロードして起動します。(初回起動時にデータベースも初期化される)
ここでいったんPostgres93.appを終了しましょう。

Step 2 pg_upgradeする

オプションに新旧のbinディレクトリとデータディレクトリを指定して、pg_upgradeを実行します。

$ /Applications/Postgres93.app/Contents/MacOS/bin/pg_upgrade \
  -b /Applications/Postgres.app/Contents/MacOS/bin \
  -B /Applications/Postgres93.app/Contents/MacOS/bin \
  -d ~/Library/Application\ Support/Postgres/var \
  -D ~/Library/Application\ Support/Postgres93/var

ここで一つ目の問題「ロケールが違うのでアップグレードできない」という問題に遭遇しました。

$ /Applications/Postgres93.app/Contents/MacOS/bin/pg_upgrade \
  -b /Applications/Postgres.app/Contents/MacOS/bin \
  -B /Applications/Postgres93.app/Contents/MacOS/bin \
  -d ~/Library/Application\ Support/Postgres/var \
  -D ~/Library/Application\ Support/Postgres93/var
Performing Consistency Checks
-----------------------------
Checking cluster versions                                   ok
Checking database user is a superuser                       ok
Checking for prepared transactions                          ok
Checking for reg* system OID user data types                ok
Checking for contrib/isn with bigint-passing mismatch       ok
Creating dump of global objects                             ok
Creating dump of database schemas
                                                            ok

lc_collate cluster values do not match:  old "ja_JP", new "ja_JP.UTF-8"
Failure, exiting

新旧のPostgres.appで確認したところ、たしかにcollate, ctypeが違っています。

mochiz-mac% psql -l
                                    List of databases
            Name             |  Owner  | Encoding | Collate | Ctype |  Access privileges  
-----------------------------+---------+----------+---------+-------+---------------------
 asakura                     | asakura | UTF8     | ja_JP   | ja_JP | 
 postgres                    | asakura | UTF8     | ja_JP   | ja_JP | 
 template0                   | asakura | UTF8     | ja_JP   | ja_JP | =c/asakura         +
                             |         |          |         |       | asakura=CTc/asakura
 template1                   | asakura | UTF8     | ja_JP   | ja_JP | =c/asakura         +
                             |         |          |         |       | asakura=CTc/asakura
(20 rows)

mochiz-mac% psql -l
                                    List of databases
       Name       |  Owner  | Encoding |   Collate   |    Ctype    |  Access privileges  
------------------+---------+----------+-------------+-------------+---------------------
 postgres         | asakura | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | 
 template0        | asakura | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/asakura         +
                  |         |          |             |             | asakura=CTc/asakura
 template1        | asakura | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/asakura         +
                  |         |          |             |             | asakura=CTc/asakura
(5 rows)

Step 2.1 既存DBのロケールをja_JP.UTF-8に変更する

ロケールの変更はinitdbする必要があるようなので、次のように対応しました。

# 既存DBをバックアップする。
$ pg_dumpall > backup.sql

# 念のため既存データをディレクトリ名を変えて保存しておく。
$ mv ~/Library/Application\ Support/Postgres/var ~/Library/Application\ Support/Postgres/var-9.2

# initdbでロケールをja_JP.UTF-8にしてデータベースを作りなおす。
$ initdb --locale=ja_JP.UTF-8 -D ~/Library/Application\ Support/Postgres/var

# バックアップからデータを復元する。
$ psql -f backup.sql postgres

# ロケールが変更されたことを確認する。
$ psql -l
                                    List of databases
       Name       |  Owner  | Encoding |   Collate   |    Ctype    |  Access privileges  
------------------+---------+----------+-------------+-------------+---------------------
 postgres         | asakura | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | 
 template0        | asakura | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/asakura         +
                  |         |          |             |             | asakura=CTc/asakura
 template1        | asakura | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/asakura         +
                  |         |          |             |             | asakura=CTc/asakura
(5 rows)

# 不要になった別名のデータを削除する。
$ rm -r ~/Library/Application\ Support/Postgres/var-9.2

Step 2.2 新DBの不要なデータベースを削除する

もう一度pg_upgradeしてみます。 ここで二つ目の問題「同名のデータベースが存在する」に遭遇しました。

$ /Applications/Postgres93.app/Contents/MacOS/bin/pg_upgrade -b /Applications/Postgres.app/Contents/MacOS/bin -B /Applications/Postgres93.app/Contents/MacOS/bin -d ~/Library/Application\ Support/Postgres/var -D ~/Library/Application\ Support/Postgres93/var
Performing Consistency Checks
-----------------------------
Checking cluster versions                                   ok
Checking database user is a superuser                       ok
Checking for prepared transactions                          ok
Checking for reg* system OID user data types                ok
Checking for contrib/isn with bigint-passing mismatch       ok
Creating dump of global objects                             ok
Creating dump of database schemas
                                                            ok
Checking for presence of required libraries                 ok
Checking database user is a superuser                       ok
Checking for prepared transactions                          ok

If pg_upgrade fails after this point, you must re-initdb the
new cluster before continuing.

Performing Upgrade
------------------
Analyzing all rows in the new cluster                       ok
Freezing all rows on the new cluster                        ok
Deleting files from new pg_clog                             ok
Copying old pg_clog to new server                           ok
Setting next transaction ID for new cluster                 ok
Setting oldest multixact ID on new cluster                  ok
Resetting WAL archives                                      ok
Setting frozenxid counters in new cluster                   ok
Restoring global objects in the new cluster                 
*failure*

Consult the last few lines of "pg_upgrade_utility.log" for
the probable cause of the failure.
Failure, exiting

$ tail ./pg_upgrade_utility.log
CREATE DATABASE "asakura" WITH TEMPLATE = template0 OWNER = "asakura";
psql:pg_upgrade_dump_globals.sql:45: ERROR:  database "asakura" already exists

ログを見てみると、asakuraというデータベースが作成済みなので失敗したとあります。
おそらくユーザー名のデータベースが自動的に作成されたのでしょう。
もう調べるのめんどいし、別にいらないっぽいので手動削除します。

$ psql postgres
postgres=# DROP DATABASE asakura;
DROP DATABASE
postgres=# \q
$ psql -l
                                List of databases
   Name    |  Owner  | Encoding |   Collate   |    Ctype    |  Access privileges  
-----------+---------+----------+-------------+-------------+---------------------
 postgres  | asakura | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | 
 template0 | asakura | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/asakura         +
           |         |          |             |             | asakura=CTc/asakura
 template1 | asakura | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/asakura         +
           |         |          |             |             | asakura=CTc/asakura
(3 rows)

Step 2.3 再度pg_upgradeする

$ /Applications/Postgres93.app/Contents/MacOS/bin/pg_upgrade \
  -b /Applications/Postgres.app/Contents/MacOS/bin \
  -B /Applications/Postgres93.app/Contents/MacOS/bin \
  -d ~/Library/Application\ Support/Postgres/var \
  -D ~/Library/Application\ Support/Postgres93/var
Performing Consistency Checks
-----------------------------
Checking cluster versions                                   ok
Checking database user is a superuser                       ok
Checking for prepared transactions                          ok
Checking for reg* system OID user data types                ok
Checking for contrib/isn with bigint-passing mismatch       ok
Creating dump of global objects                             ok
Creating dump of database schemas
                                                            ok
Checking for presence of required libraries                 ok
Checking database user is a superuser                       ok
Checking for prepared transactions                          ok

If pg_upgrade fails after this point, you must re-initdb the
new cluster before continuing.

Performing Upgrade
------------------
Analyzing all rows in the new cluster                       ok
Freezing all rows on the new cluster                        ok
Deleting files from new pg_clog                             ok
Copying old pg_clog to new server                           ok
Setting next transaction ID for new cluster                 ok
Setting oldest multixact ID on new cluster                  ok
Resetting WAL archives                                      ok
Setting frozenxid counters in new cluster                   ok
Restoring global objects in the new cluster                 ok
Adding support functions to new cluster                     ok
Restoring database schemas in the new cluster
                                                            ok
Removing support functions from new cluster                 ok
Copying user relation files
                                                            ok 
Setting next OID for new cluster                            ok
Sync data directory to disk                                 ok
Creating script to analyze new cluster                      ok
Creating script to delete old cluster                       ok

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

アップグレードに成功したようです、長い道のりでした。
あとは、analyze_new_cluster.shdelete_old_cluster.shを実行すれば良いようです。

Step 3 analyze_new_cluster.shを実行する

$ ./analyze_new_cluster.sh
This script will generate minimal optimizer statistics rapidly
so your system is usable, and then gather statistics twice more
with increasing accuracy.  When it is done, your system will
have the default level of optimizer statistics.

If you have used ALTER TABLE to modify the statistics target for
any tables, you might want to remove them and restore them after
running this script because they will delay fast statistics generation.

If you would like default statistics as quickly as possible, cancel
this script and run:
    "/Applications/Postgres93.app/Contents/MacOS/bin/vacuumdb" --all --analyze-only

Generating minimal optimizer statistics (1 target)
--------------------------------------------------
vacuumdb: could not connect to database template1: could not connect to server: No such file or directory
	Is the server running locally and accepting
	connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

The server is now available with minimal optimizer statistics.
Query performance will be optimal once this script completes.

Generating medium optimizer statistics (10 targets)
---------------------------------------------------
vacuumdb: could not connect to database template1: could not connect to server: No such file or directory
	Is the server running locally and accepting
	connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

Generating default (full) optimizer statistics (100 targets?)
-------------------------------------------------------------
vacuumdb: could not connect to database template1: could not connect to server: No such file or directory
	Is the server running locally and accepting
	connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

Done

Step 4 delete_old_cluster.shを実行する

$ ./delete_old_cluster.sh

これで古いPostgres.appのデータが削除されます。

おわり

私からは以上です。

参考URL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment