2 Min Read
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.
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:
-
Identify Your Container: First, find the container ID or name of your running PostgreSQL instance.
docker ps -
Run the pg_dump Command: Use the
docker execcommand to runpg_dumpinside the PostgreSQL container. Replaceyour_container_namewith your actual container name, andyour_databasewith 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.sqlThis command creates a custom-format dump file, which is a recommended practice for backups.
-
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:
-
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 postgresCreate the new database:
CREATE DATABASE your_new_database; -
Run the pg_restore Command: Use the
docker execcommand to runpg_restoreinside the PostgreSQL container. Replaceyour_new_databasewith 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 -
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:
-
Use the
--cleanand--if-existsoptions. 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 -
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?
Can I run pg_dump against a live database without downtime?
What's the difference between pg_dump and pg_dumpall?
Should I compress PostgreSQL backups?
Where should I store backups for disaster recovery?
Related Services
Working through this problem? These are the services we offer that connect to it.
SaaS Development
Build SaaS that scales from first customer to continent-wide.
SaaS development in Kenya. Afriq Silicon builds multi-tenant platforms with subscription billing, Keycloak authentication, and Africa-ready infrastructure.
Explore serviceSystem Orchestration
Make your infrastructure invisible, reliably fast, quietly resilient.
IT system orchestration and infrastructure in Kenya. Afriq Silicon designs scalable, secure, integrated IT environments for growing African businesses.
Explore serviceRelated
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
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
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
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
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....