Postgresql - Backup & Recovery
pg_dump & pg_dumpall
PostgreSQL provides powerful tools for backing up and restoring databases, including `pg_dump` and `pg_dumpall`. These tools allow you to create backups of individual databases or entire clusters, ensuring data integrity and availability in case of failures or data loss.
-
pg_dump
pg_dump is a utility for backing up a single PostgreSQL database. It creates a text file containing SQL commands that can be used to recreate the database schema and data. Basic Syntax:pg_dump [options] dbname
Common Options:-
[postgres@dbapage ~]$ pg_dump --help
pg_dump dumps a database as a text file or to other formats.
Usage:
pg_dump [OPTION]... [DBNAME]
General options:
-f, --file=FILENAME output file or directory name
-F, --format=c|d|t|p output file format (custom, directory, tar,
plain text (default))
-j, --jobs=NUM use this many parallel jobs to dump
-v, --verbose verbose mode
-V, --version output version information, then exit
-Z, --compress=METHOD[:DETAIL] [ gzip , lz4, zstd ]
compress as specified
--lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock
--no-sync do not wait for changes to be written safely to disk
--sync-method=METHOD set method for syncing files to disk
-?, --help show this help, then exit
Options controlling the output content:
-a, --data-only dump only the data, not the schema
-b, --large-objects include large objects in dump
--blobs (same as --large-objects, deprecated)
-B, --no-large-objects exclude large objects in dump
--no-blobs (same as --no-large-objects, deprecated)
-c, --clean clean (drop) database objects before recreating
-C, --create include commands to create database in dump
-e, --extension=PATTERN dump the specified extension(s) only
-E, --encoding=ENCODING dump the data in encoding ENCODING
-n, --schema=PATTERN dump the specified schema(s) only
-N, --exclude-schema=PATTERN do NOT dump the specified schema(s)
-O, --no-owner skip restoration of object ownership in
plain-text format
-s, --schema-only dump only the schema, no data
-S, --superuser=NAME superuser user name to use in plain-text format
-t, --table=PATTERN dump only the specified table(s)
-T, --exclude-table=PATTERN do NOT dump the specified table(s)
-x, --no-privileges do not dump privileges (grant/revoke)
--binary-upgrade for use by upgrade utilities only
--column-inserts dump data as INSERT commands with column names
--disable-dollar-quoting disable dollar quoting, use SQL standard quoting
--disable-triggers disable triggers during data-only restore
--enable-row-security enable row security (dump only content user has
access to)
--exclude-extension=PATTERN do NOT dump the specified extension(s)
--exclude-table-and-children=PATTERN
do NOT dump the specified table(s), including
child and partition tables
--exclude-table-data=PATTERN do NOT dump data for the specified table(s)
--exclude-table-data-and-children=PATTERN
do NOT dump data for the specified table(s),
including child and partition tables
--extra-float-digits=NUM override default setting for extra_float_digits
--filter=FILENAME include or exclude objects and data from dump
based on expressions in FILENAME
--if-exists use IF EXISTS when dropping objects
--include-foreign-data=PATTERN
include data of foreign tables on foreign
servers matching PATTERN
--inserts dump data as INSERT commands, rather than COPY
--load-via-partition-root load partitions via the root table
--no-comments do not dump comments
--no-publications do not dump publications
--no-security-labels do not dump security label assignments
--no-subscriptions do not dump subscriptions
--no-table-access-method do not dump table access methods
--no-tablespaces do not dump tablespace assignments
--no-toast-compression do not dump TOAST compression methods
--no-unlogged-table-data do not dump unlogged table data
--on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands
--quote-all-identifiers quote all identifiers, even if not key words
--rows-per-insert=NROWS number of rows per INSERT; implies --inserts
--section=SECTION dump named section (pre-data, data, or post-data)
--serializable-deferrable wait until the dump can run without anomalies
--snapshot=SNAPSHOT use given snapshot for the dump
--strict-names require table and/or schema include patterns to
match at least one entity each
--table-and-children=PATTERN dump only the specified table(s), including
child and partition tables
--use-set-session-authorization
use SET SESSION AUTHORIZATION commands instead of
ALTER OWNER commands to set ownership
Connection options:
-d, --dbname=DBNAME database to dump
-h, --host=HOSTNAME database server host or socket directory
-p, --port=PORT database server port number
-U, --username=NAME connect as specified database user
-w, --no-password never prompt for password
-W, --password force password prompt (should happen automatically)
--role=ROLENAME do SET ROLE before dump
If no database name is supplied, then the PGDATABASE environment
variable value is used.
Description Command Full database backup in plain/text format: pg_dump > backup1.dmp( plain format)
pg_dump -U postgres -h localhost -p 5432 -Ft -f backup_file.dump postgresFull database backup in tar format: [postgres@dbapage backup]$ pg_dump -Ft -f bk1.dmp [postgres@dbapage backup]$ tar -tvf bk1.dmp -rw------- postgres/postgres 7682 2025-07-15 13:02 toc.dat -rw------- postgres/postgres 4722173 2025-07-15 13:02 4425.dat -rw------- postgres/postgres 2759515 2025-07-15 13:02 4426.dat -rw------- postgres/postgres 2671089 2025-07-15 13:02 4427.dat -rw------- postgres/postgres 2249551 2025-07-15 13:02 4428.dat -rw------- postgres/postgres 2550189 2025-07-15 13:02 4429.dat -rw------- postgres/postgres 28 2025-07-15 13:02 4430.dat -rw------- postgres/postgres 5 2025-07-15 13:02 4431.dat -rw------- postgres/postgres 5 2025-07-15 13:02 4432.dat -rw------- postgres/postgres 7 2025-07-15 13:02 4433.dat -rw------- postgres/postgres 7281 2025-07-15 13:02 restore.sql
Full database backup in directory format: [postgres@dbapage backup]$ pg_dump -Fd -f mydir [postgres@dbapage backup]$ du -a mydir/ 8 mydir/toc.dat 432 mydir/4425.dat.gz 252 mydir/4426.dat.gz 244 mydir/4427.dat.gz 208 mydir/4428.dat.gz 236 mydir/4429.dat.gz 4 mydir/4430.dat.gz 4 mydir/4431.dat.gz 4 mydir/4432.dat.gz 4 mydir/4433.dat.gz 1396 mydir/
Full database backup in normal compression format: [postgres@dbapage backup]$ pg_dump -Fc -f bkp.dmp [postgres@dbapage backup]$ ls -lrt total 1376 -rw-r--r--. 1 postgres postgres 1407361 Jul 15 13:08 bkp.dmp Full database backup in gzip compression:(normal speed with good compression) [postgres@dbapage backup]$ time pg_dump -Z gzip -f gzip.dmp real 0m1.444s user 0m0.398s sys 0m0.267s [postgres@dbapage backup]$ ls -lrt *gzip* -rw-r--r--. 1 postgres postgres 1400305 Jul 15 15:30 gzip.dmp
Full database backup in lz4 compression: (fast with low compression) [postgres@dbapage backup]$ time pg_dump -Z lz4 -f lz4.dmp real 0m0.507s user 0m0.062s sys 0m0.039s [postgres@dbapage backup]$ ls -lrt *lz4* -rw-r--r--. 1 postgres postgres 3260751 Jul 15 15:30 lz4.dmp
Full database backup in zstd compression:(best for speed and compression) [postgres@dbapage backup]$ time pg_dump -Z zstd -f zstd.dmp real 0m0.597s user 0m0.056s sys 0m0.066s [postgres@dbapage backup]$ ls -lrt *zst* -rw-r--r--. 1 postgres postgres 720440 Jul 15 15:29 zstd.dmp
Full database backup in Normal Fc and Z 0-9 compression : [postgres@dbapage backup]$ time pg_dump -Fc -Z 0 -f 0.dmp real 0m0.819s user 0m0.067s sys 0m0.072s [postgres@dbapage backup]$ time pg_dump -Fc -Z 3 -f 3.dmp real 0m0.868s user 0m0.164s sys 0m0.126s [postgres@dbapage backup]$ time pg_dump -Fc -Z 9 -f 9.dmp real 0m1.472s user 0m0.451s sys 0m0.343s [postgres@dbapage backup]$ time pg_dump -Fc -f N.dmp real 0m0.712s user 0m0.252s sys 0m0.124s [postgres@dbapage backup]$ ls -lrth total 21M -rw-r--r--. 1 postgres postgres 17M Jul 15 16:08 0.dmp -rw-r--r--. 1 postgres postgres 1.7M Jul 15 16:08 3.dmp -rw-r--r--. 1 postgres postgres 1.3M Jul 15 16:08 9.dmp -rw-r--r--. 1 postgres postgres 1.4M Jul 15 16:08 N.dmp Schema backup (default database and default user): pg_dump -n schema1 -n schema2 > schema.dmp table backup (default database and default user): pg_dump -t schema1.table -t schema2.table > tables.dmp other options: -U username -h Hostname -p Port -d dbname -n schemaname -N ignore schema -t tablename -T ignore table -F Format =c|d|t|p (custom, directory, tar, plain) -f filename -W password -w nopassword -c includes drop statments -C creates database script -a dataonly (only copy command) -s schema only without database -x excludes privileges -X ignores ownership -h help -j parallel jobs(cpu based) -v shows TOC entries -V displays version -Z Compression algorithm [ gzip , lz4, zstd ] -a dataonly -b - lobs -B - ignore lobs -e, --extension=PATTERN dump the specified extension(s) only -O - ignore objtects owned by OWNER -s, --schema-only dump only the schema, no data -S, --superuser=NAME superuser user name to use in plain-text format -x, --no-privileges do not dump privileges (grant/revoke) --column-inserts dump data as INSERT commands with column names --exclude-table-data=PATTERN do NOT dump data for the specified table(s) --exclude-table-data-and-children=PATTERN do NOT dump data for the specified table(s), including child and partition tables --inserts dump data as INSERT commands, rather than COPY --no-tablespaces do not dump tablespace assignments