PG_BASEBACKUP(1)

πŸ“¦ version 14.24, in opensuse-leap 16.0

πŸŽ€ pg_basebackup(1) β€” PostgreSQL 14.24 Documentation πŸŽ€

ok so you're reading the manual page for pg_basebackup, bestie, and honestly? it's a good one. buckle up πŸ’–


✨ NAME ✨

pg_basebackup β€” take a base backup of a PostgreSQL cluster. that's it. that's the whole vibe. 🫢


🌸 SYNOPSIS 🌸

pg_basebackup [option...]

πŸ¦‹ DESCRIPTION πŸ¦‹

so pg_basebackup is your girl for taking a base backup of a running PostgreSQL database cluster. and the iconic part? the backup is taken without affecting other clients of the database β€” nobody even notices you're there πŸ’…. you can use the result for point-in-time recovery (peep Section 26.3) or as the starting point for a log-shipping or streaming-replication standby server (Section 27.2).

pg_basebackup makes an exact copy of the database cluster's files, and it handles putting the server into and out of backup mode automatically β€” no manual mode-switching era, we don't do that here ✨.

Backups are always taken of the entire database cluster. it is not possible to back up individual databases or database objects. for selective backups you gotta use another tool like pg_dump(1). no cap.

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 (Section 22.2) or is a superuser, and pg_hba.conf must permit the replication connection. the server must also be configured with max_wal_senders set high enough to provide at least one walsender for the backup plus one for WAL streaming (if used). plan accordingly, queen πŸ‘‘

there can be multiple pg_basebackups running at the same time, but ngl from a performance point of view it's usually better to take only one backup and then copy the result. work smarter, not harder 🧚

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 it can accept replication connections β€” that means set max_wal_senders and hot_standby, and configure its pg_hba.conf appropriately. you will also need to enable full_page_writes on the primary. yes, all of it. πŸ”₯

now, fair warning β€” there are some limitations in taking a backup from a standby, and lowkey you need to know all of them:

  • 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 to trigger an immediate WAL file switch.
  • if the standby is promoted to be primary during backup, the backup fails. full stop.
  • all WAL records required for the backup must contain sufficient full-page writes, which requires you to enable full_page_writes on the primary and not to use a tool like pg_compresslog as archive_command to remove full-page writes from WAL files. do not do that. πŸ’€

whenever pg_basebackup is taking a base backup, the server's pg_stat_progress_basebackup view will report the progress of the backup. see Section 28.4.5 for the details. progress bar era πŸ”₯


πŸ’œ OPTIONS πŸ’œ

πŸŽ€ the ones that control the location and format of the output πŸŽ€

  • -D directory, --pgdata=directory β€” sets the target directory to write the output to. pg_basebackup will create this directory (and any missing parent directories) if it does not exist. if it already exists, it must be empty. also, when the backup is in tar format, the target directory may be specified as - (dash), causing the tar file to be written to stdout β€” pipe girl summer 🌈. this option is required. like, actually required, don't skip it 🫢
  • -F format, --format=format β€” selects the format for the output. format can be one of the following:
    • p, plain β€” write the output as plain files, with the same layout as the source server's data directory and tablespaces. when the cluster has no additional tablespaces, the whole database will be placed in the target directory. if the cluster contains additional tablespaces, the main data directory will be placed in the target directory, but all other tablespaces will be placed in the same absolute path as they have on the source server (see --tablespace-mapping to change that). this is the default format. ✨
    • t, tar β€” write the output as tar files in the target directory. the main data directory's contents will be written to a file named base.tar, and each other tablespace will be written to a separate tar file named after that tablespace's OID. if the target directory is specified as - (dash), the tar contents will be written to standard output, suitable for piping to (for example) gzip πŸ’…. this is only allowed if the cluster has no additional tablespaces and WAL streaming is not used. read that twice.
  • -R, --write-recovery-conf β€” creates a standby.signal file and appends connection settings to the postgresql.auto.conf file in the target directory (or within the base archive file when using tar format). this eases setting up a standby server using the results of the backup. the postgresql.auto.conf file will record the connection settings and, if specified, the replication slot that pg_basebackup is using, so that streaming replication will use the same settings later on. glow-up for your standby setup 🌟
  • -T olddir=newdir, --tablespace-mapping=olddir=newdir β€” relocates the tablespace in directory olddir to newdir during the backup. to be effective, olddir must exactly match the path specification of the tablespace as it is defined on the source server. (but it is not an error if there is no tablespace in olddir on the source server β€” no drama.) meanwhile newdir is a directory in the receiving host's filesystem. as with the main target directory, newdir need not exist already, but if it does exist it must be empty. both olddir and newdir must be absolute paths. if either path needs to contain an equal sign (=), precede that with a backslash. this option can be specified multiple times for multiple tablespaces πŸ” if a tablespace is relocated in this way, the symbolic links inside the main data directory are updated to point to the new location. so the new data directory is ready to be used for a new server instance with all tablespaces in the updated locations. it's giving organized πŸ’… currently, this option only works with plain output format; it is ignored if tar format is selected. ignored. ghosted. πŸ™ˆ
  • --waldir=waldir β€” sets the directory to write WAL (write-ahead log) files to. by default WAL files will be placed in the pg_wal subdirectory of the target directory, but this option can be used to place them elsewhere. waldir must be an absolute path. as with the main target directory, waldir need not exist already, but if it does exist it must be empty. this option can only be specified when the backup is in plain format. πŸŽ€
  • -X method, --wal-method=method β€” includes the required WAL (write-ahead log) files in the backup. this will include all write-ahead logs generated during the backup. unless the method none is specified, it is possible to start a postmaster in the target directory without the need to consult the log archive, thus making the output a completely standalone backup πŸ‘‘ the following methods for collecting the write-ahead logs are supported:
    • n, none β€” don't include write-ahead logs in the backup.
    • f, fetch β€” the write-ahead log files are collected at the end of the backup. therefore it is necessary for the source server's wal_keep_size parameter to be set high enough that the required log data is not removed before the end of the backup. if the required log data has been recycled before it's time to transfer it, the backup will fail and be unusable 😭. when tar format is used, the write-ahead log files will be included in the base.tar file.
    • s, stream β€” stream write-ahead log data while the backup is being taken. this method will open a second connection to the server and start streaming the write-ahead log in parallel while running the backup. therefore it will require two replication connections, not just one. as long as the client can keep up with the write-ahead log data, using this method requires no extra write-ahead logs to be saved on the source server ✨. when tar format is used, the write-ahead log files will be written to a separate file named pg_wal.tar (if the server is a version earlier than 10, the file will be named pg_xlog.tar). this value is the default. 🫢
  • -z, --gzip β€” enables gzip compression of tar file output, with the default compression level. compression is only available when using the tar format, and the suffix .gz will automatically be added to all tar filenames. cute and compressed πŸŽ€
  • -Z level, --compress=level β€” enables gzip compression of tar file output, and specifies the compression level (0 through 9, 0 being no compression and 9 being best compression). compression is only available when using the tar format, and the suffix .gz will automatically be added to all tar filenames. pick your fighter: 0 = chill, 9 = try hard πŸ’…

🌈 the ones that control the generation of the backup and the invocation of the program 🌈

  • -c fast|spread, --checkpoint=fast|spread β€” sets checkpoint mode to fast (immediate) or spread (the default) (see Section 26.3.3). ⚑
  • -C, --create-slot β€” specifies that the replication slot named by the --slot option should be created before starting the backup. an error is raised if the slot already exists. no duplicates allowed, periodt πŸ’€
  • -l label, --label=label β€” sets the label for the backup. if none is specified, a default value of pg_basebackup base backup will be used. name your child πŸ₯Ή
  • -n, --no-clean β€” by default, when pg_basebackup aborts with an error, it removes any directories it might have created before discovering that it cannot finish the job (for example, the target directory and write-ahead log directory). this option inhibits tidying-up and is thus useful for debugging πŸ›. note that tablespace directories are not cleaned up either way. πŸ™ˆ
  • -N, --no-sync β€” by default, pg_basebackup will wait for all files to be written safely to disk. this option causes pg_basebackup to return without waiting, which is faster, but means that a subsequent operating system crash can leave the base backup corrupt 😭. generally this option is useful for testing but should not be used when creating a production installation. take the warning seriously, bestie 🫢
  • -P, --progress β€” turns on progress reporting πŸŽ€. You'll get an approximate progress report while the backup runs. And like, key word: approximate β€” the database can literally change mid-backup, so this is only an estimation and it might not land at exactly 100%. Especially iconic edge case: when WAL is included in the backup, the total amount of data cannot be estimated in advance, and in that situation the estimated target size will just keep increasing once it passes the total estimate it made without WAL. Not a bug, just vibes πŸ’….
  • -r rate, --max-rate=rate β€” sets the max transfer rate at which data gets collected from the source server. Super useful to limit how much pg_basebackup bullies the server 🫢. Values are in kilobytes per second. Suffix M = megabytes per second. Suffix k is also accepted and honestly has no effect lol. Valid values: between 32 kilobytes per second and 1024 megabytes per second.

Heads up: this always affects transfer of the data directory. Transfer of WAL files is only affected if the collection method is fetch.

  • -S slotname, --slot=slotname β€” only usable together with -X stream. It makes WAL streaming use the replication slot you name. If the base backup is meant to be used as a streaming-replication standby using a replication slot, then the standby should use the same replication slot name as primary_slot_name β€” that's how you ensure the primary server doesn't yeet any necessary WAL data in the gap between the end of the base backup and the start of streaming replication on the new standby ✨.

That replication slot has to, like, actually exist, unless you're also using option -C.

If you don't specify this option and the server supports temporary replication slots (version 10 and later), then a temporary replication slot is automatically used for WAL streaming. Slay.

  • -v, --verbose β€” verbose mode, bestie πŸ“£. Outputs some extra steps during startup and shutdown, and also shows the exact file name currently being processed if progress reporting is enabled too.
  • --manifest-checksums=algorithm β€” picks the checksum algorithm applied to each file included in the backup manifest. Currently available: NONE, CRC32C, SHA224, SHA256, SHA384, and SHA512. Default is CRC32C.

If NONE is selected, the backup manifest just… contains no checksums 🫠. Otherwise it contains a checksum of each file in the backup using the algorithm you picked. Also β€” in every case β€” the manifest always contains a SHA256 checksum of its own contents. The SHA algorithms are significantly more CPU-intensive than CRC32C, so picking one of them may increase the time required to complete the backup.

Using a SHA hash function gives you a cryptographically secure digest of each file, for those who wanna verify the backup hasn't been tampered with ✨. CRC32C gives a checksum that's much faster to calculate; it's good at catching errors from accidental changes but is not resistant to malicious modifications. Also note: to actually be useful against an adversary who has access to the backup, the backup manifest would need to be stored securely elsewhere or otherwise verified not to have been modified since the backup was taken πŸ‘€.

pg_verifybackup(1) can be used to check the integrity of a backup against the backup manifest.

  • --manifest-force-encode β€” forces all filenames in the backup manifest to be hex-encoded. If you don't pass this, only non-UTF8 filenames get hex-encoded. Mostly intended to test that tools which read a backup manifest file properly handle this case 🧚.
  • --no-estimate-size β€” stops the server from estimating the total amount of backup data that will be streamed, which means the backup_total column in the pg_stat_progress_basebackup view is always NULL. Bye bestie πŸ‘‹.

Without this option, the backup starts by enumerating the size of the entire database, and then goes back and sends the actual contents. That can make the backup take slightly longer β€” and in particular it'll take longer before the first data is sent. This option is useful for skipping that estimation time if it's too long.

⚠️ This option is not allowed when using --progress.
  • --no-manifest β€” disables generation of a backup manifest. If you don't pass this, the server will generate and send a backup manifest which can be verified using pg_verifybackup(1) πŸ’–. The manifest is a list of every file present in the backup β€” with the exception of any WAL files that may be included. It also stores the size, last modification time, and an optional checksum for each file.
  • --no-slot β€” prevents the creation of a temporary replication slot for the backup.

By default, if log streaming is selected but no slot name is given with -S, then a temporary replication slot is created (if supported by the source server).

The main purpose of this option is to allow taking a base backup when the server has no free replication slots. That said, using a replication slot is almost always preferred, because it prevents needed WAL from being removed by the server during the backup πŸ”₯.

  • --no-verify-checksums β€” disables verification of checksums, if they're enabled on the server the base backup is taken from.

By default, checksums are verified and checksum failures result in a non-zero exit status. However πŸ’€, the base backup will not be removed in such a case β€” exactly as if the --no-clean option had been used. Checksum verification failures will also be reported in the pg_stat_database view.


okay so this next batch is all about the connection to the source server πŸ”Œ:

  • -d connstr, --dbname=connstr β€” specifies parameters used to connect to the server, as a connection string. These will override any conflicting command line options πŸ‘‘.

It's called --dbname for consistency with other client applications, but β€” fun fact β€” because pg_basebackup doesn't connect to any particular database in the cluster, any database name in the connection string will be ignored πŸ™ˆ.

  • -h host, --host=host β€” specifies the host name of the machine where the server is running. If the value begins with a slash, it's used as the directory for a Unix domain socket πŸ¦‹. The default is taken from the PGHOST environment variable, if set β€” else a Unix domain socket connection is attempted.
  • -p port, --port=port β€” specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections. Defaults to the PGPORT environment variable, if set, or a compiled-in default ⭐.
  • -s interval, --status-interval=interval β€” the number of seconds between status packets sent back to the source server. Smaller values let the server monitor backup progress more accurately πŸ’–. A value of 0 disables periodic status updates completely β€” although an update will still be sent when requested by the server, to avoid timeout-based disconnects. Default is 10 seconds.
  • -U username, --username=username β€” the user name to connect as πŸŽ€.
  • -w, --no-password β€” never issues a password prompt. If the server requires password authentication and a password isn't available by other means such as a .pgpass file, the connection attempt will fail 😭. Useful in batch jobs and scripts where no user is around to type a password.
  • -W, --password β€” forces pg_basebackup to prompt for a password before connecting to the source server.

Honestly this option is never essential, since pg_basebackup automatically prompts for a password if the server demands password authentication. But it'll waste a whole connection attempt finding out the server wants a password 🫠. In some cases it's worth typing -W just to avoid that extra connection attempt.


and there's more! other options are also available, bestie:

  • -V, --version β€” prints the pg_basebackup version and exits ✨.
  • -?, --help β€” shows help about pg_basebackup command line arguments, and exits 🌸.

🌈 ENVIRONMENT 🌈

This utility, like most other PostgreSQL utilities, uses the environment variables supported by libpq (see Section 34.15).

The environment variable PG_COLOR specifies whether to use color in diagnostic messages πŸ’…. Possible values are always, auto and never.

πŸ’« NOTES πŸ’«

At the beginning of the backup, a checkpoint needs to be performed on the source server. This can take some time (especially if the option --checkpoint=fast is not used) β€” and during that whole stretch pg_basebackup will just appear to be idle 😴. It's not stuck, it's manifesting.

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 β€” except certain temporary files managed by PostgreSQL πŸŽ€. But only regular files and directories are copied, except that symbolic links used for tablespaces are preserved. Symbolic links pointing to certain directories known to PostgreSQL are copied as empty directories. Other symbolic links and special device files are skipped πŸ™ˆ. See Section 53.4 for the precise details.

In plain format, tablespaces will be backed up to the same path they have on the source server, unless the option --tablespace-mapping is used. Without this option, running a plain format base backup on the same host as the server will not work if tablespaces are in use πŸ’€ β€” because the backup would have to be written to the same directory locations as the original tablespaces.

When tar format is used, it is the user's responsibility to unpack each tar file before starting a PostgreSQL server that uses the data 🫢. If there are additional tablespaces, the tar files for them need to be unpacked in the correct locations. In this case the symbolic links for those tablespaces will be created by the server according to the contents of the tablespace_map file that is included in the base.tar file.

pg_basebackup works with servers of the same or an older major version, down to 9.1 ✨. However, WAL streaming mode (-X stream) only works with server version 9.3 and later, and tar format (--format=tar) only works with server version 9.5 and later.

pg_basebackup will preserve group permissions for data files if group permissions are enabled on the source cluster πŸ‘‘.

✨ EXAMPLES ✨

okay bestie, this is the part where it all clicks πŸ’– here are the exact glow-up moves, no cap:

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 -X fetch | bzip2 > backup.tar.bz2
🫢 heads up bestie: this command will fail if there are multiple tablespaces in the database. it's giving "one tablespace only" energy, so don't try to make it carry more than it can 😭

To create a backup of a local database where the tablespace in /opt/ts is relocated to ./backup/ts:

$ pg_basebackup -D backup/data -T /opt/ts=$(pwd)/backup/ts

🌸 SEE ALSO 🌸


πŸ’… and that's the whole iconic manual page, bestie. sourced from PostgreSQL 14.24, dated 2026, and officially filed under PG_BASEBACKUP(1) β€” periodt. πŸŽ€βœ¨

πŸ“¦ where this page comes from