LoginSignup
6
5

More than 3 years have passed since last update.

docker-composeでpostgresの設定を変更する方法

Posted at

結論

docker-entrypoint-initdb.dディレクトリに*.sql, *.sql.gz, or *.shファイルを置くと、databaseを作成時に実行してくれます

やりたいこと

開発時docker postgresとAWS RDSの設定が異なるので一致させたい
as is

 SELECT name, setting, context FROM pg_settings WHERE name LIKE 'lc%';
        name     |  setting   |  context
    -------------+------------+-----------
     lc_collate  | en_US.utf8 | internal
     lc_ctype    | en_US.utf8 | internal
     lc_messages | en_US.utf8 | superuser
     lc_monetary | en_US.utf8 | user
     lc_numeric  | en_US.utf8 | user
     lc_time     | en_US.utf8 | user
(6 rows)

to be

SELECT name, setting, context FROM pg_settings WHERE name LIKE 'lc%';
    name     |  setting   |  context
-------------+------------+-----------
 lc_collate  | en_US.utf8 | internal
 lc_ctype    | en_US.utf8 | internal
 lc_messages |            | superuser
 lc_monetary | C          | user
 lc_numeric  | C          | user
 lc_time     | C          | user
(6 rows)

スクリプトとdocker-compose.ymlの修正

今回はinit_dbディレクトリ以下にinit_db.shファイルを作成しました。
やっていることはコンテナ内の/var/lib/postgresql/data/postgresql.confファイルの中身をsedコマンドで書き換えています。
ディレクトリ名はdocker-compose.ymlのvolumesと一致していればなんでもよいです。

init_db/init_db.sh
#!/bin/bash

# 開発環境用のDocker postgresqlの起動設定を変更する
# 本番DBにあわせた設定にする

set -e

sed -i -e"s/^lc_messages = 'en_US.utf8'.*$//" /var/lib/postgresql/data/postgresql.conf
sed -i -e"s/^lc_monetary = 'en_US.utf8'.*$/lc_monetary = 'C'/" /var/lib/postgresql/data/postgresql.conf
sed -i -e"s/^lc_numeric = 'en_US.utf8'.*$/lc_numeric = 'C'/" /var/lib/postgresql/data/postgresql.conf
sed -i -e"s/^lc_time = 'en_US.utf8'.*$/lc_time = 'C'/" /var/lib/postgresql/data/postgresql.conf

volumesでホストのinit_dbディレクトリをコンテナのdocker-entrypoint-initdb.dディレクトリにマウントします。
これでpostgresイニシアライズ時に↑で作成したスクリプトが実行されます。

docker-compose.yml
  postgres:
    image: postgres:9.5.15
    restart: always
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
      - ./init_db:/docker-entrypoint-initdb.d

データベースを作り直す

どうやら作成し直さないと設定が変更にならない項目もあるようなので、データベースを作成しなおします

sudo rm -rf tmp/db

or

# railsなら
docker-compose run --rm web bin/rails db:drop

それでもうまくいかない場合はいったんimageなども削除したほうがよいかもしれません。
※以下はdockerのimagesを全削除するコマンドです。

docker rmi $(docker images -q) -f

before

 SELECT name, setting, context FROM pg_settings WHERE name LIKE 'lc%';
        name     |  setting   |  context
    -------------+------------+-----------
     lc_collate  | en_US.utf8 | internal
     lc_ctype    | en_US.utf8 | internal
     lc_messages | en_US.utf8 | superuser
     lc_monetary | en_US.utf8 | user
     lc_numeric  | en_US.utf8 | user
     lc_time     | en_US.utf8 | user
(6 rows)

after

SELECT name, setting, context FROM pg_settings WHERE name LIKE 'lc%';
    name     |  setting   |  context
-------------+------------+-----------
 lc_collate  | en_US.utf8 | internal
 lc_ctype    | en_US.utf8 | internal
 lc_messages |            | superuser
 lc_monetary | C          | user
 lc_numeric  | C          | user
 lc_time     | C          | user
(6 rows)

成功です!

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