- Lower Kabete Rd, Nairobi 00100, Kenya
- Mon-Fri, 08:00am - 06:00pm
- +254 (183) 700754 +254 (728) 922269
- info@afriqsilicon.com
2 Min Read
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.
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:
-
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.
System Orchestration
Make your infrastructure invisible, reliably fast, quietly resilient.
IT system orchestration and infrastructure from Afriq Silicon. We design scalable, secure, integrated IT environments for growing organizations.
Explore serviceMulti-Tenant Platform Development
Platforms that scale from first deployment to millions of users.
Multi-tenant platform development from Afriq Silicon. We build platforms with tenant isolation, subscription billing, enterprise SSO, and resilient infrastructure.
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.
November 9th, 2026
Step‑by‑step guide to rescuing stalled government digital transformation projects
Learn how to rescue a stalled government project in Kenya with a clear sequence, from audit and NDA to sprint delivery and hand‑over, using Afriq Silicon’s
November 6th, 2026
Choosing the right hospital management system for Kenyan health providers
Learn how Kenyan hospitals can evaluate and select a hospital management system that meets data residency, offline‑first, integration and procurement
November 4th, 2026
How to adopt business process management in Kenyan enterprises
Learn a step‑by‑step guide to implementing BPM in Kenya and Africa, from mapping critical flows to choosing a platform that fits low‑connectivity and
November 3rd, 2026
Designing and Implementing an ERP for African Public Institutions
A step‑by‑step guide for designing and implementing an ERP in African public institutions, covering discovery, procurement, architecture, development
Pages
- - Work
- - Services
- - Our Process
- - Contact Us
Solutions
Legal
Contact
- - Lower Kabete Rd, Nairobi 00100, Kenya
- - Mon-Fri, 08:00am - 06:00pm
- - +254 (183) 700754
- - +254 (728) 922269
- - info@afriqsilicon.com