RefLib

Reference library for AI

Back

postgresql_16:7.2.8.1 Description

PostgreSQL 16 > Reference > PostgreSQL Client Applications > pg_basebackup > Description

pg_basebackup is used to take a base backup of a running PostgreSQL database cluster. The backup is taken without affecting other clients of the database, and can be used both for point-in-time recovery (see (see postgresql_16:4.11.3)) and as the starting point for a log-shipping or streaming-replication standby server (see (see postgresql_16:4.12.2)).

pg_basebackup makes an exact copy of the database cluster's files, while making sure the server is put into 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 selective backups, another tool such as (see postgresql_16:7.2.11) must be used.

The backup is made over a regular PostgreSQL connection that uses the replication protocol. The connection must be made with a user ID that has REPLICATION permissions (see (see postgresql_16:4.7.2)) or is a superuser, and pg_hba.conf (see postgresql_16:4.6.1) must permit the replication connection. The server must also be configured with (see guc-max-wal-senders) set high enough to provide at least one walsender for the backup plus one for WAL streaming (if used).

There can be multiple pg_basebackups running at the same time, but it is usually 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 a primary server but also a standby. To take a backup from a standby, set up the standby so that it can accept replication connections (that is, set max_wal_senders and (see guc-hot-standby), and configure its pg_hba.conf appropriately). You will also need to enable (see guc-full-page-writes) on the primary.

Note that there are some limitations in taking a backup from a standby: The backup history file is not created in the database cluster backed up. pg_basebackup cannot force the standby to switch to a new WAL file at the end of backup. When you are using -X none, if write activity on the primary is low, pg_basebackup may need to wait a long time for the last WAL file required for the backup to be switched and archived. In this case, it may be useful to run pg_switch_wal on the primary in order to trigger an immediate WAL file switch. If the standby is promoted to be primary during backup, the backup fails. All WAL records required for the backup must contain sufficient full-page writes, which requires you to enable full_page_writes on the primary.

Whenever pg_basebackup is taking a base backup, the server's pg_stat_progress_basebackup view will report the progress of the backup. See (see postgresql_16:4.13.4.6) for details.

Back