Skip to content

Instantly share code, notes, and snippets.

@illiafox
Created December 6, 2024 23:06
Show Gist options
  • Save illiafox/989d011f2febd18de8500451586ef258 to your computer and use it in GitHub Desktop.
Save illiafox/989d011f2febd18de8500451586ef258 to your computer and use it in GitHub Desktop.
Postgres - Docker backups guide

Backup your databases

docker exec -t your-db-container pg_dumpall -c -U postgres > dump_`date +%Y-%m-%d"_"%H_%M_%S`.sql

or dump only needed tables

docker exec -t your-db-container  pg_dump -c -U test_user --table mytable1 --table mytable12 postgres > dump_`date +%Y-%m-%d"_"%H_%M_%S`.sql

Creates filename like dump_2023-12-25_09_15_26.sql

If you want a smaller file size, use gzip:

docker exec -t your-db-container pg_dumpall -c -U postgres | gzip > dump_`date +%Y-%m-%d"_"%H_%M_%S`.sql.gz

If you want even smaller file sizes use brotli or bzip2:

docker exec -t your-db-container pg_dumpall -c -U postgres | brotli --best > dump_`date +%Y-%m-%d"_"%H_%M_%S`.sql.br

or

docker exec -t your-db-container pg_dumpall -c -U postgres | bzip2 --best > dump_`date +%Y-%m-%d"_"%H_%M_%S`.sql.bz2

Restore your databases

cat your_dump.sql | docker exec -i your-db-container psql -U postgres
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment