skip to Main Content

I have a question. I am pretty new to docker, so what I am trying to do is create a docker-compose file that on compose command will also create the database. Problem is that does not create DB as I ask it nicely. So my docker-compose looks like:

version: '3'

services:
  db:
    image: postgres:latest
    restart: always
    ports:
      - 5432:5432
    environment:
      POSTGRES_PASSWORD: 'postgres'
    volumes:
      - database_data:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql

volumes:
  database_data:
    driver: local

And when I start docker-compose up in log I can see

db_1  | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1  |
db_1  | 2020-01-13 11:14:36.259 UTC [1] LOG:  starting PostgreSQL 12.1 (Debian 12.1-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1  | 2020-01-13 11:14:36.259 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1  | 2020-01-13 11:14:36.260 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1  | 2020-01-13 11:14:36.264 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1  | 2020-01-13 11:14:36.277 UTC [29] LOG:  database system was shut down at 2020-01-13 11:10:57 UTC
db_1  | 2020-01-13 11:14:36.280 UTC [1] LOG:  database system is ready to accept connections

In my init SQL there is only 1 line:

CREATE DATABASE "MyDataBase";

As I list DB is Postgres container my DB is nowhere to be found. What could be the source root of this problem?

6

Answers


  1. According to the documentation of postgres docker image you did everything correct.

    If you would like to do additional initialization in an image derived
    from this one, add one or more *.sql, *.sql.gz, or *.sh scripts under
    /docker-entrypoint-initdb.d (creating the directory if necessary).
    After the entrypoint calls initdb to create the default postgres user
    and database, it will run any *.sql files, run any executable *.sh
    scripts, and source any non-executable *.sh scripts found in that
    directory to do further initialization before starting the service.

    But, there is a catch which I think you missed based on log that you posted above.

    Warning: scripts in /docker-entrypoint-initdb.d are only run if you
    start the container with a data directory that is empty
    ; any
    pre-existing database will be left untouched on container startup.

    So, I would give it a try to empty database_data directory and run again docker-compose up.

    Login or Signup to reply.
  2. Had a similar problem and what worked for me was deleting containers and volumes

    List all containers by id:

    docker container ls -qa
    

    run this to each container:

    docker container rm [id]
    

    And same with volumes:

    docker volume ls
    docker volume rm [VolumeName]
    

    and the when I docker-compose up -d it’s all working well

    reference for removing from docker

    Login or Signup to reply.
  3. If, when you start your Docker Compose, you’re getting:

    PostgreSQL Database directory appears to contain a database; Skipping initialization
    

    you need to proactively remove the volumes which were set up to store the database.

    The command docker-compose down doesn’t do this automatically.

    You can request removal of volumes like this:

    docker-compose down --volumes
    
    Login or Signup to reply.
  4. I had to do a: docker-compose down --volumes to remove the volumes first.

    And then had to do use: docker-compose up --build to make sure PostGres uses the sql files.

    Also make sure that the db directory is on the same level as the docker file.

    Login or Signup to reply.
  5. In my case, I didn’t declare a volume to /var/lib/postgresql/data, but it always has been created a volume in /var/lib/docker/volumes/, and the data persisted after stop and start the container.

    So, I forced the recreation of anonymous volumes.

    docker-compose up --renew-anon-volumes
    

    With that, you won’t need to empty the database_data directory and run again docker-compose up every single time.

    Login or Signup to reply.
  6. From the Initialization Scripts section at https://hub.docker.com/_/postgres:

    Warning: scripts in /docker-entrypoint-initdb.d are only run if you start the container with a data directory that is empty; any pre-existing database will be left untouched on container startup. One common problem is that if one of your /docker-entrypoint-initdb.d scripts fails (which will cause the entrypoint script to exit) and your orchestrator restarts the container with the already initialized data directory, it will not continue on with your scripts.

    This was the cause of the PostgreSQL Database directory appears to contain a database; Skipping initialization error in the log output. An error in the initialization script will cause the container to restart with an intialized data directory, and so your init script will not run.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search