LoginSignup
32
20

More than 3 years have passed since last update.

docker-composeでPostgreSQL作成時の注意

Last updated at Posted at 2020-10-01

docker-composeでPostgreSQLを作ろうとした時に、以下のエラーが出た場合の対策

元ネタ
https://stackoverflow.com/questions/51168558/how-to-mount-a-postgresql-volume-using-aws-ebs-in-kubernete

postgres-db | initdb: error: directory "/var/lib/postgresql/data" exists but is not empty
postgres-db | It contains a dot-prefixed/invisible file, perhaps due to it being a mount point.
postgres-db | Using a mount point directly as the data directory is not recommended.
postgres-db | Create a subdirectory under the mount point.
postgres-db exited with code 1

/var/lib/postgresql/dataにデータを作成しようとして失敗しています。

解決策は、環境変数PGDATAを指定するとデータベースの本体の保存場所を指定できるので、これをデフォルトの/var/lib/postgresql/dataから/var/lib/postgresql/data/pgdata(pgdataは他の名前で可)のように1層深くすることで解決できる可能性があります。

次の例の、volumes / postgres_root でコンテナの/var/lib/postgresql/dataをマウントさせて、 environment / PGDATA で、 /var/lib/postgresql/data/pgdata で一層深い場所にデータを作成します。。

20-10-23修正

volumes:postgres_root:driver_opts:type: noneはMacとwindowsではうまくいかないっぽいので、修正

version: '3'
services:
  postgres:
    image: postgres:12.2
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: 'pass'
      POSTGRES_DB: 'pg'
      PGDATA: /var/lib/postgresql/data/pgdata
    ports:
      - 5432:5432
    container_name: postgres-db
    volumes:
      - ./db/postgres:/var/lib/postgresql/data
      - ./db/logs:/var/log
32
20
2

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
32
20