Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jligerofleitas/a6055f6881fec487f8efc9791acc2282 to your computer and use it in GitHub Desktop.

Select an option

Save jligerofleitas/a6055f6881fec487f8efc9791acc2282 to your computer and use it in GitHub Desktop.
Import SQL file to PostgreSQL in container
# Single quotes are relevant to delay expansion to be executed in container shell session
# 1. using docker exec:
cat /path/to/file.sql | docker exec -i <running postgres container name> \
bash -c 'PGPASSWORD=${POSTGRES_PASSWORD} psql -U ${POSTGRES_USER} -d ${POSTGRES_DB}'
# 2. using docker compose exec
cat /path/to/file.sql | docker compose exec -i <service name in docker-compose.yml> \
bash -c 'PGPASSWORD=${POSTGRES_PASSWORD} psql -U ${POSTGRES_USER} -d ${POSTGRES_DB}'
# You can also use `docker compose exec`:
# docker compose exec -i <service name in docker-compose.yml> bash -c '...' < /path/to/file.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment