1

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?

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

I am getting the error postgres_1 | psql: could not connect to server: No such file or directory postgres_1 | Is the server running locally and accepting postgres_1 | connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
@user3776598 please re-create/re-build image, you can use command sudo docker-compose up --force-recreate service. than try again, in my tested in successful,... tell me if you get error again... i hope can help you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.