table of contents
PG_BASEBACKUP(1) | PostgreSQL 9.2.24 Documentation | PG_BASEBACKUP(1) |
NAME¶
pg_basebackup - take a base backup of a PostgreSQL cluster
SYNOPSIS¶
pg_basebackup [option...]
DESCRIPTION¶
pg_basebackup is used to take base backups of a running PostgreSQL database cluster. These are taken without affecting other clients to the database, and can be used both for point-in-time recovery (see Section 24.3, “Continuous Archiving and Point-in-Time Recovery (PITR)”, in the documentation) and as the starting point for a log shipping or streaming replication standby servers (see Section 25.2, “Log-Shipping Standby Servers”, in the documentation).
pg_basebackup makes a binary copy of the database cluster files, while making sure the system is put in and out of backup mode automatically. Backups are always taken of the entire database cluster; it is not possible to back up individual databases or database objects. For individual database backups, a tool such as pg_dump(1) must be used.
The backup is made over a regular PostgreSQL connection, and uses the replication protocol. The connection must be made with a superuser or a user having REPLICATION permissions (see Section 20.2, “Role Attributes”, in the documentation), and pg_hba.conf must explicitly permit the replication connection. The server must also be configured with max_wal_senders set high enough to leave at least one session available for the backup.
There can be multiple pg_basebackups running at the same time, but it is better from a performance point of view to take only one backup, and copy the result.
pg_basebackup can make a base backup from not only the master but also the standby. To take a backup from the standby, set up the standby so that it can accept replication connections (that is, set max_wal_senders and hot_standby, and configure host-based authentication). You will also need to enable full_page_writes on the master.
Note that there are some limitations in an online backup from the standby:
OPTIONS¶
The following command-line options control the location and format of the output.
-D directory, --pgdata=directory
When the backup is in tar mode, and the directory is specified as - (dash), the tar file will be written to stdout.
This option is required.
-F format, --format=format
p, plain
This is the default format.
t, tar
If the value - (dash) is specified as target directory, the tar contents will be written to standard output, suitable for piping to for example gzip. This is only possible if the cluster has no additional tablespaces.
-x, --xlog
-X method, --xlog-method=method
The following methods for collecting the transaction logs are supported:
f, fetch
s, stream
-z, --gzip
-Z level, --compress=level
The following command-line options control the generation of the backup and the running of the program.
-c fast|spread, --checkpoint=fast|spread
-l label, --label=label
-P, --progress
When this is enabled, the backup will start by enumerating the size of the entire database, and then go back and send the actual contents. This may make the backup take slightly longer, and in particular it will take longer before the first data is sent.
-v, --verbose
The following command-line options control the database connection parameters.
-h host, --host=host
-p port, --port=port
-s interval, --status-interval=interval
-U username, --username=username
-w, --no-password
-W, --password
This option is never essential, since pg_basebackup will automatically prompt for a password if the server demands password authentication. However, pg_basebackup will waste a connection attempt finding out that the server wants a password. In some cases it is worth typing -W to avoid the extra connection attempt.
Other options are also available:
-V, --version
-?, --help
ENVIRONMENT¶
This utility, like most other PostgreSQL utilities, uses the environment variables supported by libpq (see Section 31.14, “Environment Variables”, in the documentation).
NOTES¶
The backup will include all files in the data directory and tablespaces, including the configuration files and any additional files placed in the directory by third parties. Only regular files and directories are allowed in the data directory, no symbolic links or special device files.
The way PostgreSQL manages tablespaces, the path for all additional tablespaces must be identical whenever a backup is restored. The main data directory, however, is relocatable to any location.
EXAMPLES¶
To create a base backup of the server at mydbserver and store it in the local directory /usr/local/pgsql/data:
$ pg_basebackup -h mydbserver -D /usr/local/pgsql/data
To create a backup of the local server with one compressed tar file for each tablespace, and store it in the directory backup, showing a progress report while running:
$ pg_basebackup -D backup -Ft -z -P
To create a backup of a single-tablespace local database and compress this with bzip2:
$ pg_basebackup -D - -Ft | bzip2 > backup.tar.bz2
(This command will fail if there are multiple tablespaces in the database.)
SEE ALSO¶
2017-11-06 | PostgreSQL 9.2.24 |