user3776598
user3776598

Reputation: 426

.sql file in Docker Postgres Container not automatically executed

I have a .sql file created from a pg_dump command that I would like to have automatically inserted into my PostgreSQL instance in my Docker container. I mount my .sql file into the docker-entrypoint-initdb.ddirectory using volume. My Docker YML file looks like the following:

postgres: 
 environment: 
  - POSTGRES_USER=user
  - POSTGRES_PASSWORD=pw
  - POSTGRES_DB=db
 volumes:
  - ./out.sql:/docker-entrypoint-initdb.d/out.sql
 image: "postgres:9.6"

Since the sql file has been mounted into docker-entrypoint-initdb.d I expect it to automatically run and insert my data when I run docker-compose -f myyml.yml up -d. It does not execute. However, when I run bash in my container I do see my .sql file. Also, I can manually run the file with a command similar to psql -U user -a -f out.sql. Why is it not automatically running when I run docker-compose?

Upvotes: 1

Views: 2736

Answers (2)

user3776598
user3776598

Reputation: 426

So after a lot of trial and error, I realized that I needed to remove all images and containers and restart. I ran a docker-compose myyml.yml down and reran and it seemed to work.

Upvotes: 3

Muh faris
Muh faris

Reputation: 196

i think you can add command in docker compose, and as following psql -U user -a -f out.sql

postgres: 
 environment: 
  - POSTGRES_USER=user
  - POSTGRES_PASSWORD=pw
  - POSTGRES_DB=db
 volumes:
  - ./out.sql:/docker-entrypoint-initdb.d/out.sql
 image: "postgres:9.6"
 command: bash -c "psql -U user -a -f out.sql"

note : if with use command bash -c is not work, remove it

Upvotes: 1

Related Questions