PG_BASEBACKUP(1)
π 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_basebackupcannot 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_basebackupmay 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 runpg_switch_walon 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_writeson the primary and not to use a tool likepg_compresslogasarchive_commandto 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 π
-Ddirectory,--pgdata=directory β sets the target directory to write the output to.pg_basebackupwill 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 tostdoutβ pipe girl summer π. this option is required. like, actually required, don't skip it π«Ά-Fformat,--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-mappingto 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 namedbase.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 astandby.signalfile and appends connection settings to thepostgresql.auto.conffile 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. thepostgresql.auto.conffile will record the connection settings and, if specified, the replication slot thatpg_basebackupis using, so that streaming replication will use the same settings later on. glow-up for your standby setup π-Tolddir=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 thepg_walsubdirectory 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. π-Xmethod,--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 methodnoneis 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'swal_keep_sizeparameter 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 thebase.tarfile.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 namedpg_wal.tar(if the server is a version earlier than 10, the file will be namedpg_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.gzwill automatically be added to all tar filenames. cute and compressed π-Zlevel,--compress=level β enables gzip compression of tar file output, and specifies the compression level (0through9,0being no compression and9being best compression). compression is only available when using the tar format, and the suffix.gzwill 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 tofast(immediate) orspread(the default) (see Section 26.3.3). β‘-C,--create-slotβ specifies that the replication slot named by the--slotoption should be created before starting the backup. an error is raised if the slot already exists. no duplicates allowed, periodt π-llabel,--label=label β sets the label for the backup. if none is specified, a default value ofpg_basebackup base backupwill be used. name your child π₯Ή-n,--no-cleanβ by default, whenpg_basebackupaborts 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_basebackupwill wait for all files to be written safely to disk. this option causespg_basebackupto 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 π .-rrate,--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. SuffixM= megabytes per second. Suffixkis 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.
-Sslotname,--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 asprimary_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, andSHA512. Default isCRC32C.
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 thebackup_totalcolumn in thepg_stat_progress_basebackupview is alwaysNULL. 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 usingpg_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 π:
-dconnstr,--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 π.
-hhost,--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 thePGHOSTenvironment variable, if set β else a Unix domain socket connection is attempted.-pport,--port=portβ specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections. Defaults to thePGPORTenvironment variable, if set, or a compiled-in default β.-sinterval,--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 of0disables periodic status updates completely β although an update will still be sent when requested by the server, to avoid timeout-based disconnects. Default is10seconds.-Uusername,--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.pgpassfile, 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 πΈ
pg_dump(1)- Section 28.4.5
π
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
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms