14
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

updated at

Organization

docker-composeでPostgreSQL作成時の注意

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
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
14
Help us understand the problem. What are the problem?