1

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'.

1
  • 1
    You shouldn't need docker exec here; you should be able to run pg_dump directly on the host (provided you have PostgreSQL client tools installed, and the database container has a published port). That might resolve ambiguity around host vs. container paths. Commented Nov 29, 2022 at 11:27

1 Answer 1

8

try this example:

docker exec -it <container> sh -c 'pg_dump -U postgres <mydb> > /pg-snaps/<mydb>_$(date).sql'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. This does exactly what I intended

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.