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