pg_dump and pg_restore
Learn the full backup and restore cycle with pg_dump and pg_restore on a real PostgreSQL stand — plain SQL dumps, custom-format archives, and selective table restores — so you know exactly what happens when you need to recover data.
Knowing how to dump a database is not the same as knowing how to restore it. This course covers both, on a real stand with 100 000 rows. We walk every format pg_dump can produce — plain SQL and custom binary — compare their sizes and contents, then restore each one and verify the row counts are right. We cover selective restore with -t, look at what pg_restore --list reveals before you pull the trigger, and finish with a clear picture of what plain SQL restore cannot do that the custom format can. You leave with a workflow you can run any time, not just a command you copy-pasted once.
What you'll build
- Produce plain-SQL and custom-format dumps with pg_dump
- Restore a full database from both dump formats
- Use pg_restore --list to inspect an archive before restoring
- Restore a single table with pg_restore -t
- Understand what the FK constraint warning means in selective restore
Contents
- A database to back up
- Starting the stand
- Connect with psql
- The users table
- The orders table and its foreign key
- The products table
- Creating the tables
- Seeding users with a fixed random seed
- One hundred thousand orders
- A thousand products
- Loading the seed data
- Confirming what is in the database
- Keeping dump files out of git
- Plain SQL dump
- What is inside the plain dump
- Custom-format dump
- Inspecting the archive table of contents
- A query to confirm the table is gone
- Confirming the damage
- The verification query
- Verifying the plain restore
- Dropping products to test the custom restore
- Selective table restore with pg_restore
- Verifying the custom restore
- Preparing for the FK dependency exercise
- Selective restore when the referenced table is missing
- The data loaded despite the FK issue
- Logical vs physical backups
- When pg_dump is not enough
- A backup you have never restored is not a backup
- Choosing the right format for the job
- Dumping only the schema
- Clean restore: drop before recreate