🚧 Under construction — VoTechWorks is being built. A free tech program — complimentary, as our commitment to get America working.
voTechWorksGrade 13 · a vocational technology learning center, by urbanicity
newSchoolWorks.com › VoTechWorks › Technology & Design › Data Management / Database Administration › Database Backup and Recovery › Lesson 2
TEC-DATA86-04 · Lesson 2 · 12 clock hours · lab

PostgreSQL Logical Backups with pg_dump and pg_dumpall

Objective. Students will use pg_dump and pg_dumpall to create plain-text and custom-format backups of a multi-schema database and restore individual objects using pg_restore.

⚠ Pending professional review

This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.

VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.

PostgreSQL Logical Backups with pg_dump and pg_dumpall

What Is a Logical Backup?

A logical backup captures the data and structure of a database as SQL statements or a portable archive, rather than copying raw data files from disk. PostgreSQL provides two command-line tools for this purpose: `pg_dump` exports a single database, and `pg_dumpall` exports every database in a cluster, including global objects such as roles and tablespaces. Logical backups are essential in your DBA toolkit because they are portable across different PostgreSQL versions (within supported upgrade paths), can be restored into any accessible cluster, and allow you to recover individual schemas, tables, or even single objects without restoring an entire server.

pg_dump Output Formats

`pg_dump` supports several output formats, selected with the `-F` flag:

For this lab you will work with custom, tar, and plain SQL formats.

Running pg_dump

The basic syntax is:

```

pg_dump [connection_options] [format_options] -f output_file dbname

```

Examples:

```bash

pg_dump -U postgres -Fc -f dvdrental_custom.dump dvdrental

pg_dump -U postgres -Ft -f dvdrental_tar.tar dvdrental

pg_dump -U postgres -Fp -f dvdrental_plain.sql dvdrental

```

You can restrict the dump to a single schema with `-n schema_name` or a single table with `-t table_name`.

Running pg_dumpall

`pg_dumpall` always produces plain SQL and is the correct tool when you need to capture roles, passwords, and tablespace definitions that exist at the cluster level, outside any single database. Typical use:

```bash

pg_dumpall -U postgres -f cluster_full.sql

```

Because `pg_dumpall` output is plain SQL, you restore it with `psql -f cluster_full.sql`.

Restoring with pg_restore

`pg_restore` works only with custom, tar, or directory formats. Key flags:

Selective restore workflow:

```bash

pg_restore --list dvdrental_custom.dump > full_toc.txt

pg_restore -d dvdrental_new --use-list=filtered_toc.txt dvdrental_custom.dump

```

Restoring Plain SQL

For plain SQL dumps, use `psql`:

```bash

psql -U postgres -d dvdrental_new -f dvdrental_plain.sql

```

You cannot do selective object restoration directly from a plain SQL file without manually editing it — one reason custom format is preferred for production.

Verifying the Restore

After any restore, always verify that row counts match the source:

```sql

SELECT COUNT(*) FROM public.rental; -- run on source, then on restored DB

```

Mismatches indicate an incomplete restore. Check PostgreSQL logs and `pg_restore` output for errors.

Safety and Good Practice

Logical backups run as the PostgreSQL user, not as root. Never run `pg_dump` with elevated OS privileges unless your site policy requires it and the risks are understood. Store backup files on a separate volume or host — a backup on the same disk as the data it protects offers little protection against disk failure. Test restores regularly; an untested backup is not a backup. When working at the command line on shared lab workstations, confirm you are connected to the correct database and cluster before issuing destructive commands such as `DROP TABLE`. Verify your `$PGDATA` and connection string before any restore to avoid overwriting a production cluster with test data.

Connection to Your Credentials

Logical backup and recovery with `pg_dump` and `pg_restore` is a core topic in the PostgreSQL CE (EDB) exam. Understanding backup strategy — including format selection, granularity of recovery, and verification — also maps to DAMA CDMP Associate data protection competencies and is assumed knowledge for Oracle DBA CA and Microsoft DP-300 candidates who work in mixed-vendor environments.

Key terms8

pg_dumpA PostgreSQL command-line utility that creates a logical backup of a single database in plain SQL, custom, tar, or directory format.
pg_dumpallA PostgreSQL command-line utility that dumps all databases in a cluster plus global objects (roles, tablespaces) as plain SQL.
pg_restoreA PostgreSQL utility that restores a database from a custom, tar, or directory format archive created by pg_dump; supports selective object restore.
Custom format (-Fc)A compressed, PostgreSQL-specific archive format produced by pg_dump that supports parallel and selective restoration via pg_restore.
Table of Contents (TOC)A listing of every object stored in a pg_dump custom or tar archive; generated with pg_restore --list and used to control selective restores.
Selective restoreA restore operation that recovers only specified schemas, tables, or other objects from an archive rather than the entire database.
Logical backupA backup that captures database objects and data as portable SQL statements or structured archives, as opposed to a physical/binary copy of data files.
Plain SQL format (-Fp)A pg_dump output format that produces a human-readable file of SQL DDL and DML statements, restored with psql but not compatible with pg_restore.
In class: Run pg_dump with -Fc (custom format), -Ft (tar format), and plain SQL against a provided sample database; compare file sizes, inspect the content of the plain-SQL file, and record results in a lab log. · Use pg_restore with --list and --use-list flags to selectively restore a single schema and a single table into a fresh database cluster; verify row counts match the source using SELECT COUNT(*). · Deliberately corrupt a table by dropping it, then time and document a full restore from each backup format, noting which format restored fastest and which allowed the most granular recovery.
Materials: Lab workstations running PostgreSQL 15 or later; psql, pg_dump, pg_restore installed; sample Northwind or DVD-rental database restored; PostgreSQL documentation Chapter 26 (Backup and Restore); lab log template
Check: Instructor reviews completed lab log showing commands, output, row-count verification, and timing data; all three backup formats must be demonstrated with correct restore; lab log scored on a provided rubric with 80% as passing.

Practice check8

Answer each question, then check it. Nothing is recorded; this is your own practice.

PostgreSQL CE (EDB) — Backup and Recovery
1. Which pg_dump format flag produces a compressed, PostgreSQL-specific archive that supports both selective and parallel restore with pg_restore?
PostgreSQL CE (EDB) — Backup and Recovery
2. A DBA needs to recover only the 'inventory' table from a custom-format dump without restoring the rest of the database. What is the correct sequence of steps?
PostgreSQL CE (EDB) — Backup and Recovery
3. Which tool must you use to restore a plain SQL file created by pg_dumpall?
DAMA CDMP Associate — Data Quality and Protection
4. After restoring a database from a custom-format dump, how should a DBA confirm that the restore was complete and correct?
PostgreSQL CE (EDB) — Performance and Administration
5. A pg_restore command is taking a long time on a large custom-format dump. Which flag can speed up the restore by using multiple CPU cores?
PostgreSQL CE (EDB) — Backup and Recovery
6. Which statement about the plain SQL (-Fp) format from pg_dump is TRUE?
PostgreSQL CE (EDB) — Backup and Recovery
7. A DBA accidentally drops the 'orders' table in a development database. She has a custom-format dump taken 30 minutes ago. Which command restores ONLY the orders table data into the existing database without affecting other tables?
DAMA CDMP Associate — Data Governance and Protection
8. From a data governance perspective, which practice best ensures that logical backups will actually protect the organization's data in a real outage?
Record this lesson on my transcript. Sign in with your key and enrollment email to record lessons (enroll free if you have no key). This page stays open to read and practise.