Back up and restore with pg_dump
- Version
- 19
- Since
- 7.1
- Review
- Draft
Summary#
Use pg_dump --format=custom to save a database's schema and data in an archive. Restore it with pg_restore into an empty scratch database, then query the restored objects to check that they work and contain the rows you expect.
Syntax#
pg_dump --format=custom --file=backup.dump source_db
createdb --template=template0 scratch_db
pg_restore --single-transaction --dbname=scratch_db backup.dump
Example#
Save the script as example.sh. Run env -i PATH="$PATH" sh example.sh as an unprivileged OS user on a Unix-like system with the PostgreSQL 19 server and client tools on PATH. The script runs offline. It puts the cluster, socket and archive in one private temporary directory, disables TCP listening and removes the directory on exit. The final queries check the restored table rows and the view that filters them.
set -eu
work=$(mktemp -d /tmp/dlr-p1-postgresql-core-dump.XXXXXXXX)
cleanup() {
if [ -s "$work/data/postmaster.pid" ]; then
pg_ctl -D "$work/data" -m fast -w stop >/dev/null || exit 1
fi
rm -rf "$work"
}
trap cleanup EXIT
trap 'exit 1' HUP INT TERM
export PGHOST="$work" PGPORT=5432 PGUSER=backup_demo
initdb -D "$work/data" --username="$PGUSER" --auth=trust --locale=C >/dev/null
pg_ctl -D "$work/data" -l "$work/server.log" \
-o "-h '' -k $PGHOST -p $PGPORT" -w start >/dev/null
createdb dispatch_source
psql -X -q -A -P null=NULL -v ON_ERROR_STOP=1 -d dispatch_source -f - <<'SQL'
CREATE SCHEMA dispatch;
CREATE TABLE dispatch.parcels (
parcel_id integer PRIMARY KEY,
state text NOT NULL
);
INSERT INTO dispatch.parcels VALUES (101, 'queued'), (102, 'sent');
CREATE VIEW dispatch.queue AS
SELECT parcel_id FROM dispatch.parcels WHERE state = 'queued';
SQL
pg_dump --format=custom --file="$work/dispatch.dump" dispatch_source
createdb --template=template0 dispatch_restore
pg_restore --single-transaction --dbname=dispatch_restore "$work/dispatch.dump"
psql -X -q -A -P null=NULL -v ON_ERROR_STOP=1 -d dispatch_restore -f - <<'SQL'
SELECT parcel_id, state FROM dispatch.parcels ORDER BY parcel_id;
SELECT parcel_id AS queued_parcel FROM dispatch.queue ORDER BY parcel_id;
SQL
Expected output#
parcel_id|state
101|queued
102|sent
(2 rows)
queued_parcel
101
(1 row)
Common mistakes#
Create the scratch database from template0 to avoid bringing in objects from a customized template1. Leave out pg_restore --create when restoring under a new name. That option uses the database name stored in the archive. --single-transaction rolls the restore back on error. Use pg_restore --list to inspect the archive's table of contents and a test restore to verify the data.
A single-database dump omits cluster-wide objects such as roles, so another cluster may need those roles to restore ownership and grants. Restore trusted archives because a restore executes SQL supplied by the source.
Version applicability#
PostgreSQL 7.1 introduced custom archives and pg_restore. The example needs PostgreSQL 8.2 or later for pg_restore --single-transaction. For a same-version restore, use tools from the server's major version. pg_dump refuses to read a server newer than its own major version. Restoring to a server older than pg_dump can fail.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0