
One of the biggest mistakes developers make is assuming Docker automatically protects their data.
It doesn't.
If your PostgreSQL container becomes corrupted, you accidentally delete a volume, or a deployment goes wrong, your database can disappear forever if you don't have backups.
In this guide, I'll show you how to manually back up a PostgreSQL database running inside a Docker container.
My Environment
- Ubuntu Server
- Docker
- PostgreSQL 15
- Production SaaS Application
Check your running containers:
docker ps
Example:
CONTAINER ID IMAGE NAMES
18485bb1e0d6 postgres:15 postgres_db
Step 1 — Find Your Database Information
View the PostgreSQL environment variables:
docker exec -it postgres_db env | grep POSTGRES
Example output:
POSTGRES_USER=postgres
POSTGRES_DB=bdcommerce
POSTGRES_PASSWORD=********
You'll need:
- Database name
- Username
Step 2 — Create a Backup Directory
Store backups somewhere outside the container.
mkdir -p /home/backups
This ensures your backups remain safe even if the container is recreated.
Step 3 — Create the Backup
Run:
docker exec -t postgres_db \
pg_dump -U postgres -Fc bdcommerce \
> /home/backups/bdcommerce_$(date +%F_%H-%M-%S).dump
What each option means
| Option | Description |
|---|---|
-U postgres |
PostgreSQL username |
-Fc |
Custom compressed backup format |
bdcommerce |
Database name |
> |
Save the backup to the host machine |
The resulting file looks like:
bdcommerce_2026-07-09_14-30-55.dump
Step 4 — Verify the Backup
List your backup files:
ls -lh /home/backups
Example:
-rw-r--r-- 1 root root 128M Jul 09 14:30 bdcommerce_2026-07-09_14-30-55.dump
A backup that exists and has a reasonable file size is a good first sign.
Step 5 — Restore the Backup
Create a database if necessary:
createdb new_database
Restore:
pg_restore \
-d new_database \
/home/backups/bdcommerce_2026-07-09_14-30-55.dump
If you're restoring into a Docker container:
docker cp bdcommerce.dump postgres_db:/backup.dump
docker exec -it postgres_db \
pg_restore \
-U postgres \
-d bdcommerce \
/backup.dump
Backup Every Database (Optional)
To export every database, role, and permission:
docker exec -t postgres_db \
pg_dumpall -U postgres \
> all_databases.sql
This is useful when migrating an entire PostgreSQL server.
Why Use the Custom (-Fc) Format?
Instead of exporting plain SQL, PostgreSQL's custom format provides several benefits:
- Smaller file size
- Faster backup
- Faster restore
- Selective restore
- Parallel restore support
- Better suited for production environments
For most production systems, -Fc is the recommended format.

Top comments (0)