docker-compose and create db in Postgres on init

ID : 131360

viewed : 10

Tags : dockerdocker-composedocker

Top 5 Answer for docker-compose and create db in Postgres on init

vote vote


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 
vote vote


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.

vote vote


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

vote vote


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.

vote vote


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.

Top 3 video Explaining docker-compose and create db in Postgres on init