LoginSignup
18
17

More than 5 years have passed since last update.

PostgreSQL9.4 から PostgreSQL9.5 に pg_upgrade する

Last updated at Posted at 2014-12-19

開発環境でbrew update; brew upgrade したところ、PostgreSQLがリリースされたばかりのPostgreSQL9.5.0にアップグレードされたので、pg_upgrade でデータベースの内容を移行する。

前準備

brew info postgresql して、PostgreSQL9.4, 9.5 のインストール先を調べておく。ここでは、

  • 9.4が /usr/local/Cellar/postgresql/9.4.5_2
  • 9.5が /usr/local/Cellar/postgresql/9.5.0

だとする。

また、

  • これまでのPostgreSQL9.4のPGDATAフォルダ(古いPGDATA)のパスを/database/data94
  • 新しく作るPostgreSQL9.5のPGDATAフォルダ(新しいPGDATA)のパスを/database/data95
  • PostgreSQLの管理ユーザ をfoo(特に指定していなければログインしているユーザのはず)

だとする。

(念のため)PostgreSQL9.4から9.5へのシンボリックリンクを切り替える

原則、brew upgradeでアップグレードされた時に切り替わっているはずだが、念のため確認する

psql --version
  # psql (PostgreSQL) 9.5.0 と表示
  # もし9.4のままの場合は、brew switchなどでシンボリックリンクを修正
  # ex) brew switch postgresql 9.5.0 

(念のため)PostgreSQL9.4 を停止させる

/usr/local/Cellar/postgresql/9.4.5_2/bin/pg_ctl stop

新しいPGDATAのフォルダを作成し、初期化する

PostgreSQL 9.5でinitdbする。このとき、

古いクラスタに合うように互換性があるinitdbのオプションを使用してください。

ドキュメントに記載されている(つまり9.4の時にPGDATAフォルダをinitdbしたときと同様のオプションにする)ので注意する。本稿では、--no-locale -E UNICODE -U foo(ロケールなし、デフォルトエンコードはUTF8、DBのスーパユーザはfoo)としている。

mkdir /database/data95
initdb -D /database/data95 --no-locale -E UNICODE -U foo

環境変数 $PGDATA のパスを古いPGDATAのフォルダから新しいPGDATAのフォルダに切り替えておく。

echo $PGDATA
  # /database/data94 と表示される
export PGDATA="/database/data95"
  # .bash_profileなどで初期設定している場合は、そちらも変更

pg_upgradeの実行

pg_upgrade コマンドを使って、古いPGDATAフォルダの内容を新しいPGDATAフォルダに反映する。

pg_upgrade -d /database/data94 -D /database/data95 -b /usr/local/Cellar/postgresql/9.4.5_2/bin -B /usr/local/Cellar/postgresql/9.5.0/bin -U foo

実行例

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
Checking for invalid "line" user columns                    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 and epoch for new cluster       ok
Deleting files from new pg_multixact/offsets                ok
Copying old pg_multixact/offsets to new server              ok
Deleting files from new pg_multixact/members                ok
Copying old pg_multixact/members to new server              ok
Setting next multixact ID and offset for new cluster        ok
Resetting WAL archives                                      ok
Setting frozenxid and minmxid 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
Creating newly-required TOAST tables                        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

エラーや中断を表すログが生じなければ成功している。

最後に、pg_upgrade が生成してくれる analyze_new_cluster.sh の実行をして終了。

pg_ctl start
analyze_new_cluster.sh
psql -l
   # 9.4のデータベースの内容が移行されていることを確認

なお、pg_upgrade は古いPGDATAのフォルダ(この例であれば /database/data94)を削除するdelete_old_cluster.sh も吐きだしてくれる。これを実行して古いPGDATAフォルダを削除するか、念のために残しておくかはお好みで。

pg_hda.confpostgresql.conf は移行してくれないので、チューニングする必要があれば自分で修正する。

18
17
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
18
17