Backup Postgresql Database from Docker » History » Revision 3
Revision 2 (Viacheslav Anzhiganov, 02/19/2025 03:46 AM) → Revision 3/4 (Viacheslav Anzhiganov, 02/19/2025 03:51 AM)
# Backup Postgresql Database from Docker ## Backup ```shell ``` CID=$(docker ps -f name=<container_name> -q) DBN=database_name docker exec -t ${CID} your-db-container pg_dumpall -c -U 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 ```