Search code examples
postgresqldockerpg-dump

Postgres + Docker: pg_dump does not work from outside


I want to setup a cron script that automatically creates dumps from a specific Postgres database running in a Docker container. I know how to execute commands in a container from outside and also am familiar with pg_dump.

Somehow, for my container and database , I can't seem to make it work:

docker exec <container> pg_dump -U postgres <mydb> > /pg-snaps/<mydb>_$(date).sql

I get the following error:

zsh: no such file or directory: /pg-snaps/<mydb>_<date>.sql

The directory /pg-snaps exists. I can execute the same command inside the container, and it works. I have no idea why it doesn't work with docker exec. I looked up the methodology on how to do this, and it suggests the same as I want to do. Adding " " around the command to be executed also results in a 'no such file or directory'.


Solution

  • try this example:

    docker exec -it <container> sh -c 'pg_dump -U postgres <mydb> > /pg-snaps/<mydb>_$(date).sql'