•

2 Min Read

How to Backup and Restore a PostgreSQL Database Using Docker

How to Backup and Restore a PostgreSQL Database Using Docker

Backing up and restoring a Dockerized PostgreSQL database with pg_dump and pg_restore, including the schema conflicts that surface during a restore.

A client came to us needing to restore a PostgreSQL database without a recent backup to restore from. This is the backup and restore procedure we put in place afterwards.

Prerequisites

  • Docker installed on your system
  • A PostgreSQL database running in a Docker container
  • Basic knowledge of PostgreSQL and Docker commands

Step 1: Backup Your PostgreSQL Database

Backing up a PostgreSQL database involves using the pg_dump utility, which creates a logical backup of your database. Here’s how to do it with Docker:

  1. Identify Your Container: First, find the container ID or name of your running PostgreSQL instance.

    docker ps
    
  2. Run the pg_dump Command: Use the docker exec command to run pg_dump inside the PostgreSQL container. Replace your_container_name with your actual container name, and your_database with the name of your database.

    docker exec -t your_container_name pg_dump -U postgres -F c your_database > /path/to/backup/your_database_backup.sql
    

    This command creates a custom-format dump file, which is a recommended practice for backups.

  3. Verify the Backup: Check the backup file to ensure it has been created successfully.

    ls /path/to/backup
    

Step 2: Restore Your PostgreSQL Database

Restoring a PostgreSQL database from a backup involves using the pg_restore utility. Here’s how to restore your database:

  1. Create a New Database: Before restoring, you need to create a new database in which the data will be restored. Access the PostgreSQL container:

    docker exec -it your_container_name psql -U postgres
    

    Create the new database:

    CREATE DATABASE your_new_database;
    
  2. Run the pg_restore Command: Use the docker exec command to run pg_restore inside the PostgreSQL container. Replace your_new_database with the name of the new database.

    docker exec -i your_container_name pg_restore -U postgres -d your_new_database /path/to/backup/your_database_backup.sql
    
  3. Verify the Restoration: Check the new database to ensure that the data has been restored correctly. Access the PostgreSQL container and list the tables:

    docker exec -it your_container_name psql -U postgres -d your_new_database \dt
    

Handling Common Issues

During the restoration process, you might encounter errors such as conflicts with existing schemas or sequences. Here are a few tips to handle such issues:

  1. Use the --clean and --if-exists options. These drop existing objects before recreating them, which avoids the conflict entirely.

    pg_restore --clean --if-exists -U postgres -d your_new_database /path/to/backup/your_database_backup.sql
    
  2. Drop the conflicting objects by hand. If only a few specific objects are causing problems, remove them before rerunning the restore.

    DROP SCHEMA IF EXISTS schema_name CASCADE; DROP SEQUENCE IF EXISTS sequence_name;
    

One thing worth doing before you need it

Run the restore once, against a throwaway database, before you are relying on it. An untested backup is a file, not a recovery plan, and the time to find that out is not the day you need it.

Full options are in the PostgreSQL backup documentation.


Frequently Asked Questions

Common questions on this topic, answered by the Afriq Silicon team.

How often should I back up a production PostgreSQL database?
Take a daily backup for most workloads, and for high-transaction systems layer in incremental backups every 15 minutes to an hour using WAL archiving.
Can I run pg_dump against a live database without downtime?
Yes, pg_dump takes a consistent snapshot without locking the database, so users can keep reading and writing throughout.
What's the difference between pg_dump and pg_dumpall?
pg_dump backs up a single database while pg_dumpall covers the entire cluster including roles and tablespaces, so use pg_dumpall for full disaster recovery and pg_dump when you only need one database.
Should I compress PostgreSQL backups?
Yes, and the easiest way is to use pg_dump's custom format (-Fc), which compresses by default and also lets you restore selective tables when you need to.
Where should I store backups for disaster recovery?
Store them off the host machine in at least two locations, typically object storage like S3 or GCS with versioning turned on, so a failed server never means lost data.

Related

Similar Articles

Stay Informed with Our Latest Articles: Explore the most recent insights, trends, and updates from our industry experts. Dive into a wealth of knowledge to keep you ahead in the ever-evolving tech landscape.

Team‑as‑a‑Service for Kenyan public‑sector procurement cycles

October 29th, 2026

project

Team‑as‑a‑Service for Kenyan public‑sector procurement cycles

Learn how Team‑as‑a‑Service can fit Kenya’s government procurement rules, what trade‑offs to expect, and the steps to embed senior engineers safely.

By Titus Mwangi 4 Min Read
Procurement‑Ready RFP Template for AI/ML Projects in African Public Sector

October 28th, 2026

project

Procurement‑Ready RFP Template for AI/ML Projects in African Public Sector

Get a ready‑to‑use RFP template for AI/ML contracts, with Kenyan data‑protection guidance, evaluation criteria and step‑by‑step assembly for public sector

By Titus Mwangi 5 Min Read
Choosing or Building a Core Banking Platform for Kenyan SACCOs

October 16th, 2026

project

Choosing or Building a Core Banking Platform for Kenyan SACCOs

Get a practical decision framework for a sacco core banking system – architecture, compliance, scalability, integration and implementation steps for Kenyan

By Titus Mwangi 4 Min Read
Procurement‑friendly software contract clauses for Kenyan government projects

October 13th, 2026

project

Procurement‑friendly software contract clauses for Kenyan government projects

Learn the essential contract clauses Kenyan public sector buyers need—scope, data protection, IP, support, and more—aligned with local procurement rules.

By Titus Mwangi 5 Min Read
noise

Let’s Build Something
Amazing Together

Afriq Silicon

We will help you turn ideas into digital reality whatever industry you want to revolutionize

Solutions

Contact

© 2026 Afriq Silicon, Inc. All rights reserved