Back
Database Management

Reliable database management for modern, high-performance systems.

Back
Data Modernisation

Reliable database management for modern, high-performance systems.

Back
Data Engineering

Reliable database management for modern, high-performance systems.

Back
Analytics & Intelligence

Reliable database management for modern, high-performance systems.

Back
Data Strategy Consulting

Reliable database management for modern, high-performance systems.

How to Back Up and Restore a PostgreSQL Database with pg_dump and pg_restore

Surya K
Published on
December 3, 2026

A backup you have never restored is a guess. In this walkthrough, our team tested the whole cycle on a CentOS server running PostgreSQL 17.11: create a database, take a custom-format backup with pg_dump, drop a table on purpose, and restore it with pg_restore. Every screenshot below comes from that run. If you look after PostgreSQL in production, you can repeat the same steps in a lab before you rely on a backup routine.

The backup and restore flow at a glance

StageTool or commandWhat it does
PreparepsqlCreate a database and a table with sample rows
Back uppg_dump -FcWrite a custom-format archive of the database
Simulate lossDROP TABLERemove the table to test recovery
Restorepg_restore --clean --if-existsDrop existing objects, then restore them from the archive

Figure: the four stages of the lab test.

Step 1: Confirm the service and the version

First, check that the PostgreSQL 17 service is running and enabled at boot. The status output shows it active (running) and set to start automatically after a reboot. Then confirm the client version with psql --version, which reports PostgreSQL 17.11 in our lab.

Terminal showing PostgreSQL 17 service status as active (running)
Figure 1. PostgreSQL 17 service status on the CentOS server.
Terminal output of psql --version showing PostgreSQL 17.11
Figure 2. psql --version confirms PostgreSQL 17.11.

Step 2: Create a database and connect to it

Create a dedicated lab database with CREATE DATABASE dba_backup_lab; and confirm the message reads CREATE DATABASE. Then connect with \c dba_backup_lab. The session confirms it is connected as the postgres user.

psql session showing CREATE DATABASE dba_backup_lab
Figure 3. CREATE DATABASE dba_backup_lab completes successfully.
psql message: connected to database dba_backup_lab as user postgres
Figure 4. Connecting to dba_backup_lab as the postgres user.

Step 3: Create a table and insert sample rows

The test table is employees, with four columns: emp_id as a SERIAL PRIMARY KEY, emp_name, department and salary as NUMERIC(10,2). The INSERT adds four employee records, and PostgreSQL reports INSERT 0 4.

psql CREATE TABLE statement for the employees table
Figure 5. CREATE TABLE employees with four columns.
psql INSERT statement adding four rows, returning INSERT 0 4
Figure 6. INSERT of four employee records.

Step 4: Verify the data before you back it up

Run SELECT * FROM employees; to confirm the four rows are in place. Checking the data before the backup gives you a baseline to compare against after the restore.

psql output listing four employee rows
Figure 7. SELECT * FROM employees returns four rows.

Step 5: Take a custom-format backup

Create a backup directory, then run pg_dump with the custom format flag, -Fc. The custom format writes a compressed archive that pg_restore reads. The archive in our test was dba_backup_lab.backup, and the file listing shows it at 3.0 KB.

In this lab we wrote the backup to /tmp/pg_backup_lab. The /tmp directory is cleared on reboot, so treat that path as a lab convenience only. In production, write backups to a directory that survives reboots and is copied off the server.

Terminal showing mkdir -p /tmp/pg_backup_lab
Figure 8. Creating the backup directory with mkdir -p.
Terminal showing pg_dump run with -Fc against dba_backup_lab
Figure 9. pg_dump in custom format (-Fc) writes the backup file.
Directory listing showing dba_backup_lab.backup at 3.0K
Figure 10. The backup file exists at 3.0 KB.

Step 6: Reconnect and simulate accidental data loss

Reconnect to the database with psql -U postgres -d dba_backup_lab, then drop the table with DROP TABLE employees;. PostgreSQL confirms the drop. The table is gone, and this is the situation the backup exists for.

psql prompt after connecting to dba_backup_lab as postgres
Figure 11. Reconnecting to dba_backup_lab.
psql output showing DROP TABLE
Figure 12. DROP TABLE employees; succeeds.

Run \dt to list the tables. The output reads Did not find any relations, which confirms the table is missing.

psql \dt output reading Did not find any relations
Figure 13. \dt shows no relations after the drop.

Step 7: Restore with pg_restore

Restore the database with pg_restore against the custom-format archive. The --clean flag drops existing objects before they are recreated, and --if-exists stops PostgreSQL from raising errors when an object is already missing. --if-exists is only valid when --clean is also set.

postgres@localhostlab server
pg_restore -U postgres \  --clean \  --if-exists \  -d dba_backup_lab /tmp/pg_backup_lab/dba_backup_lab.backup

Figure 14. pg_restore with --clean and --if-exists, as run in the lab.

Step 8: Confirm the table and the records came back

List the tables again with \dt. The employees table is back in the public schema and owned by postgres. Then check the records: SELECT * returns the four original rows with their original IDs, and SELECT COUNT(*) returns 4.

psql output listing employees as a table in the public schema
Figure 15. \dt shows the employees table restored in the public schema.
dba_backup_labSELECT * FROM employees;
emp_idemp_namedepartmentsalary
1ArunDatabase35000.00
2KumarCloud45000.00
3PriyaTesting40000.00
4RahulSupport30000.00

(4 rows)

Figure 16. All four original rows are back. Recreated from the lab screenshot.

psql output of SELECT COUNT(*) returning 4
Figure 17. COUNT(*) returns 4.

Step 9: Check the totals

As a final check, sum the salaries. The query returns total_records of 4 and a total_salary of 150000.00, which matches the values before the drop.

psql output showing total_records 4 and total_salary 150000.00
Figure 18. Total records and total salary match the original data.

Gotcha: restoring into a database that still has the objects

If the table still exists when you restore, pg_restore tries to create objects that are already there and reports errors. The --clean flag solves that by dropping objects first. Add --if-exists to keep the restore output clean when some objects are missing. Test this on a copy before you run it against a live database.

Limits to know

The PostgreSQL 17 documentation sets out a few rules that matter for a backup routine:

LimitWhat it meansWhat to do instead
Custom format is for pg_restoreThe -Fc archive is not a SQL script. Only pg_restore reads it.Use pg_restore for custom-format archives. Use plain format if you need a SQL script.
Compressed by defaultThe archive is smaller than the raw data.Check archive size and restore time when you plan storage.
--if-exists needs --cleanThe flag is rejected on its own.Always pass both flags together.
Logical snapshotThe archive holds the data as it was at dump time.Schedule dumps and keep the time of each backup in your runbook.

Sources: pg_dump, PostgreSQL 17 and pg_restore, PostgreSQL 17.

Where this fits in a DBA's day

A logical backup like this suits smaller databases, single schemas, and table-level recovery. For larger databases, combine logical backups with physical backups and point-in-time recovery. Whichever method you use, run a test restore on a schedule, and keep a copy of the archive away from the database server. A backup that has never been restored is not yet a recovery plan. For teams that want help designing that routine, see our high availability and disaster recovery services and our DBA managed services.

Wrapping up

The test covered the full cycle: a custom-format backup with pg_dump, a deliberate table drop, and a restore with pg_restore --clean --if-exists that brought back all four rows. The commands are simple. The discipline is in running the restore before you need it. If you want a second pair of eyes on your PostgreSQL backup setup, start with our free database audit or talk to our team. For more practical guides, visit the Geopits blog.

FAQ

1. What does the -Fc option do in pg_dump?
It writes a custom-format archive that pg_restore reads. The archive is compressed by default and lets you restore selected objects.

2. Why does pg_restore need --clean?
--clean drops existing objects before pg_restore recreates them. Without it, a restore into a database that still has those objects reports errors.

3. Can I use --if-exists without --clean?
No. The PostgreSQL documentation states that --if-exists is not valid unless --clean is also specified.

4. Is /tmp a safe place for backups?
Not for production. The /tmp directory is cleared on reboot. Use a directory that survives reboots and is copied off the server.

5. Should I test restores on a schedule?
Yes. A backup that has never been restored has not been proven. Restore to a separate database on a regular schedule and compare the row counts.

Related reading

Sources

PostgreSQL 17 documentation: pg_dump and pg_restore.

Surya K, Database Administrator at Geopits

Surya K

Surya K specializes in database administration, with a focus on database management, monitoring, and performance optimization. His work involves supporting database operations, maintaining database reliability, and ensuring secure, stable, and high-performing database environments.

Our Latest Blogs

From monitoring to modernization, GeoPITS provides expert database management services that enhance performance, strengthen security, and reduce operational costs.

Ready to Transform Your Data?

Geopits works alongside your team as a strategic partner, starting with stabilizing your current databases, then modernizing your data infrastructure, and ultimately helping you unlock the full potential of AI.

170

Happy Clients so far

2100+

Databases Managed

142+

Successful Migrations

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.