$linuxjunkies
>

pg_dump(1)

Extract a PostgreSQL database into a script file or archive.

UbuntuDebianFedoraArch

Synopsis

pg_dump [OPTION]... [DBNAME]

Description

pg_dump is a utility for backing up a PostgreSQL database. It produces plain-text SQL scripts or custom-format archives that can be used to recreate the database from scratch or migrate it to another PostgreSQL installation.

By default, pg_dump outputs SQL commands to stdout. The output can be piped to psql to restore the database, or saved to a file for later use. Custom and directory formats allow parallel processing and selective restoration.

Common options

FlagWhat it does
-f, --file=FILENAMEWrite output to a file instead of stdout
-F, --format={plain|custom|directory|tar}Output format; plain is default SQL text, custom/directory/tar for archives
-j, --jobs=NUMUse parallel jobs for dumping (requires -F directory or -F tar)
-t, --table=PATTERNDump only matching tables (can be used multiple times)
-T, --exclude-table=PATTERNExclude matching tables from the dump
-s, --schema-onlyDump only the schema, not the data
-a, --data-onlyDump only the data, not the schema
-c, --cleanInclude DROP commands to clean objects before creating them
-C, --createInclude CREATE DATABASE command at start of dump
-U, --username=NAMEConnect as database user NAME
-h, --host=HOSTNAMESpecify database server host
-p, --port=PORTSpecify database server port (default 5432)

Examples

Dump the entire database 'mydb' to a plain SQL file

pg_dump mydb > mydb_backup.sql

Dump 'mydb' from one server and pipe directly to restore into 'targetdb' on another

pg_dump -U postgres -h 192.168.1.10 mydb | psql -U postgres targetdb

Create a custom-format archive suitable for selective restore with pg_restore

pg_dump -F custom -f mydb.dump mydb

Dump database using 4 parallel jobs into directory format for faster backup

pg_dump -F directory -j 4 -f mydb_backup mydb

Dump only the schema of the 'users' table

pg_dump -s -t 'users' mydb > users_schema.sql

Dump only data from all tables matching 'logs*' pattern

pg_dump -a -t 'logs*' mydb > logs_data.sql

Create a complete backup with DROP and CREATE database commands

pg_dump -c -C mydb > mydb_full_backup.sql

Dump entire database excluding temporary and cache tables

pg_dump -T 'temp*' -T 'cache*' mydb > mydb_production.sql

Related commands