Docker save postgres datazip

Hi ,

I need to take data zip back up from the running container Postgres database and use the same for restore for new Postgres container.

Is there any option for save running Postgres database container and take back and restore.

To get the file on host, you can either use a host folder, which you bind mount into the container, or you use a pipe to cross the container border.

This is what ChatGPT suggests, so no guarantee to work, check and test it first. We use similar commands for MongoDB backup and restore.

All databases

docker exec -t <container_id_or_name> pg_dumpall -c -U <username> > dump.sql

cat dump.sql | docker exec -i <container_id_or_name> psql -U <username> -d <database_name>

Selected database

docker exec -t your-db-container pg_dump -U your-user --format c your-database > your-dump-file.dump

docker exec -i your-db-container pg_restore -U your-user