pg_dump is a logical backup tool provided by PolarDB for PostgreSQL (Compatible with Oracle). You can use it to back up a single database in a cluster as a script file or other archive files.
Introduction
pg_dump is used to back up a single database. It creates a consistent backup even while the database is being accessed, and does not block other users from accessing it (for reads or writes). For more information, see the official pg_dump website.
The pg_dump tool in PolarTools differs from the community version. The PolarTools version is adapted for PolarDB for PostgreSQL (Compatible with Oracle). Using the community version of pg_dump may result in unknown errors or incomplete data backups.
Backup file formats
Script file: A script file is a plain text file that contains the SQL commands to restore the database to its state at the time of the backup.
archive file: An archive file must be restored with pg_restore.
The output file formats include the custom format -Fc, the directory format -Fd, and the tar format archive file -Ft. The -Fc and -Fd formats allow you to select and reorder all archived items and are compressed by default. The -Ft format is not a compressed file, and it does not support reordering when you restore data.
NoteThe directory format is the only format that supports parallel backup.
When used with one of the archive formats, pg_dump can back up an entire database. You can use pg_restore to examine the archive or select which parts of the database to restore.
Syntax
pg_dump [connection-option...] [option...] [dbname]Table 1. Parameters
Parameter | Description |
connection-option | Command-line options that control database connection parameters. For details, see Connection options. |
option | Command-line options that control the output content and format. For details, see Dump options. |
dbname | Name of the database to back up. |
Table 2. Connection options
Command-line option | Description |
-d dbname or --dbname=dbname | Specifies the name of the database to connect to. |
-h host or --host=host | Specifies the hostname of the computer on which the server is running. If this value starts with a slash, it is used as the directory for the UNIX domain socket. The default value is the PGHOST environment variable. |
-p port or --port=port | Specifies the TCP port or the local UNIX domain socket file extension on which the server is listening for connections. The default value is taken from the PGPORT environment variable, or a compiled-in default is used if this variable is not set. |
-U username or --username=username | The username to connect as. |
-w is an alias for --no-password | Never issues a password prompt. |
-W is the short form of --password. | Forces pg_dump to prompt for a password before connecting to a database. Note This option is optional. |
--role=rolename | Specifies a role name to use to create the backup. |
Table 3. Dump options
Command-line option | Description |
dbname | The name of the database to be backed up. If not specified, the PGDATABASE environment variable is used. |
-a is an alias for --data-only | Backs up only the data, not the schema (data definitions). Note This option backs up table data, blobs, and sequence values. |
-b is an alias for --blobs | Blobs are included in backups by default. When the --schema, --table, or --schema only option is specified, blobs are not included in the backup. Important Because blobs are considered data, they are included in a backup if you use the --data-only option, but are excluded if you use the --schema-only option. |
-B is an alias for --no-blobs | Excludes blobs from the backup. Note If both -b and -B are specified, the backup will include blobs. |
-c is an alias for --clean | Delete the database object before you execute the command to create it. To avoid error messages when you restore the database, we recommend that you specify --if exists. Note This option applies only to script files. For archive files, you can specify this option when you call pg_restore. |
-C or --create | Creates a database and reconnects to the newly created database. If --clean is specified, the script deletes and recreates the target database, and then reconnects to it. If the --create option is specified and the --no-acl option is not specified, the backup data includes the comments, configuration information, and access privileges of the database. Note This option applies only to script files. For archive files, you can specify this option when you call pg_restore. |
-E encoding or --encoding=encoding | Creates a backup by using the specified character encoding. By default, the backup is created by using the character encoding of the database that you want to back up. You can also set the value of the PGCLIENTENCODING environment variable to the desired backup encoding. |
-F format or --format=format | Specifies the format of the output. The format can be one of the following:
|
-f file or --file=file | Sends output to the specified file.
|
-j njobs or --jobs=njobs | This option runs backups in parallel by simultaneously backing up Note Before you start a parallel backup, stop processes that modify the database, such as those involving DDL and DML. |
-n pattern is a shorthand for --schema=pattern. | Only backs up schemas that match the pattern. If this option is not specified, all non-system schemas in the target database are backed up. Note
|
-N pattern is an alias for --exclude-schema=pattern | Backs up schemas other than pattern. Note
|
-o or --oids | Backs up object identifiers (OIDs) as part of the data for every table. Use this option if your application references the OID columns, for example, in a foreign key constraint. Otherwise, do not use this option. |
-O is short for --no-owner | Does not include commands to set object ownership to match the original database. Note This option applies only to script files. For archive files, you can specify this option when you call pg_restore. |
-s is short for --schema-only | Backs up only the object definitions (schema), not data. |
-S username is the same as --superuser=username | The superuser username used when disabling triggers. This option is used only with --disable-triggers. |
-t pattern, or --table=pattern | Backs up only tables that match the pattern. You can select multiple tables by specifying the -t option multiple times or by using wildcards in the pattern. Note When you specify -t, pg_dump does not attempt to back up any other database objects that the selected table may depend on. Therefore, there is no guarantee that a backup of a specific table can be successfully restored to an empty database. |
-T pattern is an alias for --exclude-table=pattern | Does not back up tables that match the pattern. You can specify the -T option multiple times to exclude tables that match multiple patterns. Note
|
-v or --verbose | Specifies verbose mode. |
-V is an alias for --version | Prints the pg_dump version and exits. |
-x is an alias for --no-privileges or --no-acl | Prevents the backup of access privileges (grant/revoke commands). |
-Z 0..9 is the same as --compress=0..9 | Specifies the compression level to use. A value of 0 means no compression. Note
|
--column-inserts and --attribute-inserts | Back up the data as |
--disable-dollar-quoting | Disables the use of dollar quoting for function bodies. |
--disable-triggers | Temporarily disables triggers on the target table. This option is effective only when you create a data backup. When you use this option, you must specify a superuser by using -S. Note This option applies only to script files. For archive files, you can specify this option when you call pg_restore. |
--enable-row-security | Backs up only the parts of tables that you have permission to access. This option is effective only when backing up the content of a table that has row-level security enabled. Important If you use this option, you may also need to use INSERT to create backups because COPY FROM does not support row-level security during data recovery. |
--exclude-table-data=pattern | Does not back up the data of tables that match the pattern. You can specify --exclude-table-data multiple times to exclude tables that match multiple patterns. Note To exclude data from all tables in a database, refer to --schema only. |
--if-exists | Use conditional commands, such as adding an IF EXISTS clause, to clean up database objects. You must also specify the --clean option, otherwise this option has no effect. |
--inserts | Back up the data as INSERT commands. Important If you use this option, reordering data during restoration may cause the operation to fail. We recommend using --column-inserts . |
--load-via-partition-root | Use the COPY or INSERT command to back up data from a table partition. Note Use parallel restoration with caution when restoring an archive file created with this option. |
--lock-wait-timeout=timeout | Specifies the wait time for acquiring a shared lock. |
--no-comments | Does not back up comments. |
--no-publications | Does not back up publications. |
--no-security-labels | Does not back up security labels. |
--no-subscriptions | Does not back up subscriptions. |
--no-sync | Returns without waiting to safely write all files to disk. |
--no-synchronized-snapshots | Indicates that you can run pg_dump -j on the server. |
--no-tablespaces | Creates all objects in the default tablespace. Note This option applies only to script files. For archive files, you can specify this option when you call pg_restore. |
--no-unlogged-table-data | Does not back up the content of unlogged tables. |
--quote-all-identifiers | Forces quoting of all identifiers. |
--rows-per-insert=nrows | Controls the maximum number of rows per INSERT statement in a database backup. |
--section=sectionname | Specifies that only the specified section is backed up. The name of the section can be pre-data, data, or post-data. You can specify this option multiple times to select multiple sections. By default, all sections are backed up. Note
|
--serializable-deferrable | Uses a serializable transaction for the backup. Note
|
--snapshot=snapshotname | Uses the specified synchronized snapshot when backing up the database. |
--strict-names | Requires that each schema (-n or --schema) and table (-t or --table) pattern matches at least one schema or table in the source database. Note
|
--use-set-session-authorization | Outputs the SQL-standard SET SESSION AUTHORIZATION command instead of the ALTER OWNER command. |
-? or --help | Displays help for pg_dump command-line arguments and then exits. |
Usage notes
When you select a table that is used only for data backup and use the --disable triggers option,
pg_dumpissues a command to disable user table triggers before inserting data and a command to re-enable the triggers after the data is inserted. If a restore is interrupted, the system catalogs might be left in an incorrect state.After a backup file is restored, you can run ANALYZE to ensure optimal performance.
When you back up a logical replication subscription, pg_dump generates a CREATE SUBSCRIPTION command with the connect=false option. If the host has changed, you may need to change the connection information, and you must truncate the target table before starting a new full table copy.
Because pg_dump internally executes SELECT statements, if you have problems running pg_dump, you must ensure that you can query information from the database by using a tool such as psql. In addition, ensure that any default connection settings and environment variables used by the libpq frontend library are working correctly.
The statistics collector typically collects database activities for pg_dump, but if this is not required, you can use the PGOPTIONS or ALTER USER command to set the track_counts parameter to false.
Examples
Run the following command to back up the database named
mydbto an SQL script file:pg_dump mydb > db.sqlRun the following command to reload the SQL script into the newly created database named
newdb:psql -d newdb -f db.sqlRun the following command to back up a database to a custom format archive file:
pg_dump -Fc mydb > db.dumpRun the following command to back up a database to a directory format archive file:
pg_dump -Fd mydb -f dumpdirRun the following command to back up a database in parallel with 5 worker jobs to a directory format archive file:
pg_dump -Fd mydb -j 5 -f dumpdirRun the following command to restore the archive file to a new database named
newdb:pg_restore -d newdb db.dumpRun the following command to restore an archive file into the same database from which the backup was taken, clearing the current contents of that database:
pg_restore -d postgres --clean --create db.dumpRun the following command to back up a single table named
mytab:pg_dump -t mytab mydb > db.sqlRun the following command to back up all tables in the
detroitschema whose names start with emp, except for the table namedemployee_log:pg_dump -t 'detroit.emp*' -T detroit.employee_log mydb > db.sqlRun the following command to back up all schemas whose names start with east or west and end with gsm (excluding any schemas whose names contain test):
pg_dump -n 'east*gsm' -n 'west*gsm' -N '*test*' mydb > db.sqlThe following command uses a regular expression to achieve the same result more concisely:
pg_dump -n '(east|west)*gsm' -N '*test*' mydb > db.sqlRun the following command to back up all database objects except for tables whose names start with ts_:
pg_dump -T 'ts_*' mydb > db.sqlIf you need to specify an uppercase or mixed-case name in the -t and related switches, you must enclose the name in double quotes. Otherwise, it will be folded to lowercase. However, double quotes are special characters for shell commands, so you must also escape them. Therefore, to dump a single table with a mixed-case name, run the following command:
pg_dump -t "\"MixedCaseName\"" mydb > mytab.sql