Precautions when creating PostgreSQL with docker-compose

What to do if you get the following error when trying to create PostgreSQL with docker-compose

Former story 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

Attempting to create data in / var / lib / postgresql / data has failed.

The solution is to specify the environment variable PGDATA to specify the location of the database body, which is changed from the default / var / lib / postgresql / data to / var / lib / postgresql / data / pgdata. There is a possibility that it can be solved by deepening one layer like (pgdata can be another name).

In the following example, volumes / postgres_root mounts the container / var / lib / postgresql / data, environment / PGDATA, and / var / lib / postgresql / data / pgdata. Create data in the location. ..

20-10-23 fix

volumes: postgres_root: driver_opts: type: none doesn't seem to work on Mac and windows, so fix it

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

Recommended Posts

Precautions when creating PostgreSQL with docker-compose
Precautions when replacing backticks with gsub
[Java] Precautions when comparing character strings with character strings
(RubyMine) Check when you cannot debug with docker-compose
Start k3s with docker-compose
[Rails] Precautions when comparing date and time with DateTime
A memorandum when creating a REST service with Spring Boot
Rough procedure verbalized output when creating an app with Rails
Summary of initial work when creating an app with Rails
Precautions when generating a table with a composite key with Iciql + SQLite
How to specify db when creating an app with rails
Precautions when using Mockito.anyString as an argument when Mocking with Mockito
Run batch with docker-compose with Java batch
Error when playing with java
Docker-compose deploying Neo4j with APOC
I tried UPSERT with PostgreSQL.
[Rails] Avoid SELECT issued when creating a model with belongs_to defined!
[Ansible] Precautions when testing with docker driver with molecule v3.1 or later
What I did when the DB did not start with docker-compose up
Naming convention when creating a new controller or model with rails
Do not add @GeneratedValue unnecessarily when creating an Entity with JPA