Backup Postgresql Database from Docker » History » Version 2
Viacheslav Anzhiganov, 02/19/2025 03:46 AM
1 | 1 | Viacheslav Anzhiganov | # Backup Postgresql Database from Docker |
---|---|---|---|
2 | |||
3 | 2 | Viacheslav Anzhiganov | ## Backup |
4 | |||
5 | 1 | Viacheslav Anzhiganov | ``` |
6 | docker exec -t your-db-container pg_dumpall -c -U postgres > dump_`date +%Y-%m-%d"_"%H_%M_%S`.sql |
||
7 | ``` |
||
8 | |||
9 | Creates filename like dump_2023-12-25_09_15_26.sql |
||
10 | |||
11 | If you want a smaller file size, use gzip: |
||
12 | |||
13 | ``` |
||
14 | docker exec -t your-db-container pg_dumpall -c -U postgres | gzip > dump_`date +%Y-%m-%d"_"%H_%M_%S`.sql.gz |
||
15 | ``` |
||
16 | |||
17 | If you want even smaller file sizes use brotli or bzip2: |
||
18 | |||
19 | ``` |
||
20 | docker exec -t your-db-container pg_dumpall -c -U postgres | brotli --best > dump_`date +%Y-%m-%d"_"%H_%M_%S`.sql.br |
||
21 | ``` |
||
22 | |||
23 | or |
||
24 | |||
25 | ``` |
||
26 | docker exec -t your-db-container pg_dumpall -c -U postgres | bzip2 --best > dump_`date +%Y-%m-%d"_"%H_%M_%S`.sql.bz2 |
||
27 | ``` |
||
28 | |||
29 | 2 | Viacheslav Anzhiganov | ## Restore your databases |
30 | 1 | Viacheslav Anzhiganov | |
31 | ``` |
||
32 | cat your_dump.sql | docker exec -i your-db-container psql -U postgres |
||
33 | ``` |