How to Backup and Restore a Dockerized PostgreSQL Database

PostgreSQL is one of the most popular open-source relational database systems, widely used in modern applications. When running PostgreSQL inside Docker, managing backups and restores becomes a critical task to ensure data safety, especially in production environments.

In this blog, we’ll cover simple and reliable methods to backup and restore a Dockerized PostgreSQL database using built-in PostgreSQL tools like pg_dump and pg_restore.


✅ Why Backup and Restore Matter

  • Protect against accidental data loss or corruption.
  • Migrate data between environments (e.g., from development to production).
  • Restore data after container upgrades or failures.

✅ 1️⃣ Backup a PostgreSQL Database Running in Docker

Assuming you already have a running PostgreSQL container:

docker ps

Example output:

CONTAINER ID   IMAGE          COMMAND                  NAMES
a1b2c3d4e5f6   postgres:14   "docker-entrypoint.s…"   pg-database

Backup Command Using pg_dump

docker exec -t pg-database pg_dump -U postgres -F c my_database > backup.dump
  • pg-database: The container name.
  • -U postgres: PostgreSQL user.
  • -F c: Output in custom format (compressed).
  • my_database: Database name inside PostgreSQL.
  • The output is redirected to backup.dump on the host system.

Alternatively, you can run pg_dump interactively and copy the output elsewhere.


✅ 2️⃣ Restore a PostgreSQL Database into Docker

Assuming you want to restore from the backup.dump file into the same or a new container.

Option A – Into the Existing Container

cat backup.dump | docker exec -i pg-database pg_restore -U postgres -d my_database
  • This feeds the backup file into pg_restore inside the running container.
  • Ensure the target database exists or create it before restoring:
docker exec -it pg-database psql -U postgres -c "CREATE DATABASE my_database;"

Option B – Into a New Container

docker run --rm -v $(pwd):/backup --name pg-restore -e POSTGRES_PASSWORD=mysecretpassword -d postgres:14

# Wait for the container to be ready, then restore:
cat backup.dump | docker exec -i pg-restore pg_restore -U postgres -d postgres

✅ 3️⃣ Automating Backup with Docker Volumes

For regular backups, it’s recommended to mount a volume:

version: '3.8'

services:
  postgres:
    image: postgres:14
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: mysecretpassword
    volumes:
      - ./pgdata:/var/lib/postgresql/data
      - ./backups:/backups

Then, you can periodically run:

docker exec -t pg-database pg_dump -U postgres -F c my_database > ./backups/backup_$(date +%F).dump

This way, backups are persistent on the host machine.


✅ 4️⃣ Best Practices

  • Automate regular backups via cron jobs or external schedulers.
  • Store backups in separate storage (e.g., cloud storage) for disaster recovery.
  • Test restores regularly to verify backup integrity.
  • Use secure storage and encrypted backups if dealing with sensitive data.

✅ Conclusion

Backing up and restoring a Dockerized PostgreSQL database is easy using pg_dump and pg_restore inside Docker containers.
Whether for development, production, or disaster recovery, following these methods ensures your data remains safe, portable, and easy to manage.

Sharing Is Caring:

Leave a Comment