- Lower Kabete Rd, Nairobi 00100, Kenya
- Mon-Fri, 08:00am - 06:00pm
- +254 (798) 586113 +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.
October 1st, 2026
Outsourcing vs Local Delivery: risk analysis for African public‑sector IT
Find a concise risk‑focused comparison of outsourcing, local vendors and in‑house teams for government software projects, with checklists and next steps.
September 25th, 2026
Running Mission‑Critical Systems in Low‑Connectivity, Unreliable Power
Learn how to design, build and operate resilient enterprise software in Africa where network and electricity are spotty. Get practical steps, trade‑offs and
September 22nd, 2026
Implementing ERP in African Public Sector: procurement, migration, and sustainability
Learn how to navigate public‑sector ERP procurement cycles, migrate legacy data, and build a sustainable system that stays operational for the long term.
September 21st, 2026
Implementing CI/CD with GitHub Actions to Deploy Docker‑Compose Services
Step‑by‑step guide to set up a GitHub Actions pipeline that builds Docker images, pushes them to a registry and runs docker‑compose on a server, with tips
Pages
- - Work
- - Services
- - Our Process
- - Contact Us
Solutions
- - Acts ML
- - Kilelehub
- - Other Projects
Legal
Contact
- - Lower Kabete Rd, Nairobi 00100, Kenya
- - Mon-Fri, 08:00am - 06:00pm
- - +254 (798) 586113
- - +254 (728) 922269
- - info@afriqsilicon.com