I have a dump.sql file that I would like to load with docker-compose.
docker-compose.yml:
services:
postgres:
environment:
POSTGRES_DB: my_d
You can use pg_restore as well ... for example:
cat {BACKUP.SQL.File} | docker exec -i {working_container_name} pg_restore \
--verbose --clean --no-acl --no-owner -U {USER} -d {YOURDATABASE}
CONTAINER_NAME="postgres"
DB_USER=postgres
LOCAL_DUMP_PATH="..."
docker run --name "${CONTAINER_NAME}" postgres
docker exec -i "${CONTAINER_NAME}" psql -U "${DB_USER}" < "${LOCAL_DUMP_PATH}"
After the docker-compose up, do docker ps it will give you a list of active docker containers. From that, you can get the container ID.
Then,
docker exec -i {CONTAINER_ID} psql -U {USER} < {.SQL FILE}
Reading https://hub.docker.com/_/postgres/, the section 'Extend this image' explains that any .sql in /docker-entrypoint-initdb.d will be executed after build.
I just needed to change my Dockerfile.db to:
FROM postgres
ADD ./devops/db/dummy_dump.sql /docker-entrypoint-initdb.d
And it works!
sudo docker exec postgres psql -U postgres my_db_name < dump.sql