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 Dockerised 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.

Software Development in Kenya: 2026 Buyer's Guide
project trends

Software Development in Kenya: 2026 Buyer's Guide

A market briefing for procurement teams, programme directors, and technology leaders considering Kenya as a software delivery base....

Enterprise System Integration in Africa: A Practical Guide
trends project

Enterprise System Integration in Africa: A Practical Guide

What connecting business systems actually involves in African markets: M-Pesa callbacks, platforms with no API, and designing for the failure path....

Custom Software vs Off-the-Shelf in Kenya: 2026 Buyer's Guide
trends project

Custom Software vs Off-the-Shelf in Kenya: 2026 Buyer's Guide

Off-the-shelf looks cheaper on paper. Here's the real Total Cost of Ownership comparison for Kenyan institutions in 2026....

Agile Development at Afriq Silicon
project design

Agile Development at Afriq Silicon

How we actually run two-week sprints, and the deployment pipeline that makes each increment something you can click on rather than a status update....

noise

Let’s Build Something
Amazing Together

Afriq Silicon

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

© 2026 Afriq Silicon, Inc. All rights reserved