LoginSignup
6
7

More than 3 years have passed since last update.

開発環境(docker)にてpostgresのdataディレクトリのバージョンアップをお手軽にやる方法(いろんなバージョンでいけるよ)

Last updated at Posted at 2019-12-01

前提条件

  • dockerでpostgresを動かしている
  • dataディレクトリはホストマシン上等で永続化している。
  • 👆なので、欲しいのはバージョンアップしたDBに対応した新しいdataディレクトリだけでよい
  • ネット上では pg_upgrade を使う方法が書かれているけど面倒そうだからやりたくないな…という強い気持ちを持っている👍

TL;DR

tianonさんの作られた神ツール docker-postgres-upgrade を使うべし。
https://github.com/tianon/docker-postgres-upgrade

アップデートの手順

例として、PostgreSQL9.4系->9.6へアップデートしてみます。

PostgreSQL9.4のdataディレクトリが /Users/hoge/tmp/db にあると仮定します。
その場合以下のようになるはずです。

$ pwd
/Users/hoge/tmp
$ ll db
total 96
-rw-------   1 hoge  staff     4B 11 30 17:17 PG_VERSION
drwx------   9 hoge  staff   288B 11 30 17:18 base
drwx------  56 hoge  staff   1.8K 11 30 17:18 global
drwx------   4 hoge  staff   128B 11 30 17:18 pg_clog
drwx------   2 hoge  staff    64B 11 30 17:17 pg_commit_ts
drwx------   2 hoge  staff    64B 11 30 17:17 pg_dynshmem
-rw-------   1 hoge  staff   4.4K 11 30 17:17 pg_hba.conf
-rw-------   1 hoge  staff   1.6K 11 30 17:17 pg_ident.conf
drwx------   4 hoge  staff   128B 11 30 17:17 pg_logical
drwx------   4 hoge  staff   128B 11 30 17:18 pg_multixact
drwx------   3 hoge  staff    96B 11 30 17:18 pg_notify
...

PostgreSQL9.6系のdataディレクトリを db2 として構築する場合、以下のコマンドをおもむろに叩いてください。なお db2ディレクトリはホストPC上になくても大丈夫です

docker run --rm \
    -v /Users/hoge/tmp/db:/var/lib/postgresql/9.4/data \
    -v /Users/hoge/tmp/db2:/var/lib/postgresql/9.6/data \
    tianon/postgres-upgrade:9.4-to-9.6

するとこんな感じで処理が進んで…

The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/9.6/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... Etc/UTC
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ...

...(省略)...
                                                            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.sh を実行するように言われますが、内部で vacuumdb しているだけなので必要に応じて実行してください。

対応バージョン

docker-postgres-upgrade で対応しているバージョンは現時点(2019/11/30)で以下のとおりでした。

  • 10-to-11
  • 10-to-12
  • 11-to-12
  • 9.2-to-10
  • 9.2-to-11
  • 9.2-to-9.3
  • 9.2-to-9.4
  • 9.2-to-9.5
  • 9.2-to-9.6
  • 9.3-to-10
  • 9.3-to-11
  • 9.3-to-9.4
  • 9.3-to-9.5
  • 9.3-to-9.6
  • 9.4-to-10
  • 9.4-to-11
  • 9.4-to-12
  • 9.4-to-9.5
  • 9.4-to-9.6
  • 9.5-to-10
  • 9.5-to-11
  • 9.5-to-12
  • 9.5-to-9.6
  • 9.6-to-10
  • 9.6-to-11
  • 9.6-to-12

まさに神ツール❗

6
7
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
6
7