DEV Community

Nurul Islam Rimon
Nurul Islam Rimon

Posted on

Manual PostgreSQL Backup in Docker (Production-Safe Guide)


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
Enter fullscreen mode Exit fullscreen mode

Example:

CONTAINER ID   IMAGE          NAMES
18485bb1e0d6   postgres:15    postgres_db
Enter fullscreen mode Exit fullscreen mode

Step 1 — Find Your Database Information

View the PostgreSQL environment variables:

docker exec -it postgres_db env | grep POSTGRES
Enter fullscreen mode Exit fullscreen mode

Example output:

POSTGRES_USER=postgres
POSTGRES_DB=bdcommerce
POSTGRES_PASSWORD=********
Enter fullscreen mode Exit fullscreen mode

You'll need:

  • Database name
  • Username

Step 2 — Create a Backup Directory

Store backups somewhere outside the container.

mkdir -p /home/backups
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Step 4 — Verify the Backup

List your backup files:

ls -lh /home/backups
Enter fullscreen mode Exit fullscreen mode

Example:

-rw-r--r-- 1 root root 128M Jul 09 14:30 bdcommerce_2026-07-09_14-30-55.dump
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Restore:

pg_restore \
-d new_database \
/home/backups/bdcommerce_2026-07-09_14-30-55.dump
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Backup Every Database (Optional)

To export every database, role, and permission:

docker exec -t postgres_db \
pg_dumpall -U postgres \
> all_databases.sql
Enter fullscreen mode Exit fullscreen mode

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)