PG_BASEBACKUP(1)
β¨ PG_BASEBACKUP(1) β¨
PostgreSQL 16.15 Documentation β bestie, we are in the manual-page era and this one is literally the base-backup queen ππ
πΈ NAME πΈ
pg_basebackup β take a base backup of a PostgreSQL cluster. Yes, the whole cluster. Not a little slice. The whole vibe. π«Ά
π¦ SYNOPSIS π¦
pg_basebackup [option...]
π DESCRIPTION π
Okay so pg_basebackup is the tool for taking a base backup of a running PostgreSQL database cluster. The backup is taken without affecting other clients of the database, and it can be used both for point-in-time recovery (see Section 26.3) and as the starting point for a log-shipping or streaming-replication standby server (see Section 27.2). Itβs giving βmain character backup utilityβ energy, no cap. β¨
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 pg_dump(1) must be used. Thatβs the rule, bestie β full cluster or call someone else. π
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 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). Permission check, config check, slay. β
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. Like, sure, you can make it a group chat, but maybe donβt. π«
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 hot_standby, and configure its pg_hba.conf appropriately). You will also need to enable full_page_writes on the primary. Standby backup era! π
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_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 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_writeson the primary.
π¨ Warning bestie: those standby limitations are real. Especially the βbackup fails if promotedβ one β that is not a drill, and it is not a cute surprise. π
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.6 for details. Progress bar in the database? Obsessed. π₯
π OPTIONS π
The command-line options below control where the output goes and what format it takes. Location and format, location and format. Say it with me. π£οΈπ
-D directory,--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. When the backup is in tar format, the target directory may be specified as-(dash), causing the tar file to be written tostdout. This option is required. Yes, required required. Donβt skip it. π¨-F format,--format=formatβ Selects the format for the output.formatcan 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. Plain and simple, but make it iconic. π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. Tar mode has rules, bestie. Respect the rules. π
-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. Itβs basically prepping your standbyβs little outfit for later. πβ¨-t target,--target=targetβ Instructs the server where to place the base backup. The default target isclient, which specifies that the backup should be sent to the machine wherepg_basebackupis running. If the target is instead set toserver:/some/path, the backup will be stored on the machine where the server is running in the/some/pathdirectory. Storing a backup on the server requires superuser privileges or having privileges of thepg_write_server_filesrole. If the target is set toblackhole, the contents are discarded and not stored anywhere. This should only be used for testing purposes, as you will not end up with an actual backup. Since WAL streaming is implemented bypg_basebackuprather than by the server, this option cannot be used together with-Xstream. Since that is the default, when this option is specified, you must also specify either-Xfetchor-Xnone.
π«Ά Friendly reminder: blackhole is for testing, not for βoops I forgot to store my backupβ energy. You literally will not end up with an actual backup. π
-T olddir=newdir,--tablespace-mapping=olddir=newdirβ Relocates the tablespace in directoryolddirtonewdirduring the backup. To be effective,olddirmust 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 inolddiron the source server.) Meanwhilenewdiris a directory in the receiving hostβs filesystem. As with the main target directory,newdirneed not exist already, but if it does exist it must be empty. Botholddirandnewdirmust 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. Currently, this option only works with plain output format; it is ignored if tar format is selected. Mapping queen behavior. π--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.waldirmust be an absolute path. As with the main target directory,waldirneed 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. WAL files need a home too. π-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 methodnoneis specified, it is possible to start a postmaster in the target directory without the need to consult the WAL 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. Minimalist. Maybe too minimalist. π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. Streaming, bestie. Two connections. Main character energy. π₯
-z,--gzipβ Enablesgzipcompression 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. Tiny files, big slay. π-Z level,-Z [{client|server}-]method[:detail],--compress=level,--compress=[{client|server}-]method[:detail]β Requests compression of the backup. Ifclientorserveris included, it specifies where the compression is to be performed. Compressing on the server will reduce transfer bandwidth but will increase server CPU consumption. The default isclientexcept when--targetis used. In that case, the backup is not being sent to the client, so only server compression is sensible. When-Xstream, which is the default, is used, server-side compression will not be applied to the WAL. To compress the WAL, use client-side compression, or specify-Xfetch. Compression choices are a whole personality, and this option has range. π
So like, the compression method? You can set it to gzip, lz4, zstd, none for literally no compression at all, or an integer (which means no compression if it's 0, and gzip if it's greater than 0) π
You can optionally tack on a compression detail string too β¨ If that detail string is an integer, it specifies the compression level. Otherwise, it should be a comma-separated list of items, each of the form keyword or keyword=value. Currently the supported keywords are level, long, and workers. And important: the detail string cannot be used when the compression method is specified as a plain integer β that combo is a no-go, bestie π
If no compression level is specified, the default compression level will be used. And if you only specify a level without mentioning an algorithm? Then gzip compression will be used if the level is greater than 0, and no compression will be used if the level is 0. Slay, sensible defaults π
When the tar format is used with gzip, lz4, or zstd, the suffix .gz, .lz4, or .zst, respectively, will be automatically added to all tar filenames β¨ When the plain format is used, client-side compression may not be specified, but it is still possible to request server-side compression. If that happens, the server will compress the backup for transmission, and the client will decompress and extract it. Teamwork, honestly π«Ά
When this option is used in combination with -Xstream, pg_wal.tar will be compressed using gzip if client-side gzip compression is selected, but will not be compressed if any other compression algorithm is selected, or if server-side compression is selected. Ik, ik, very specific β but that's the lore π₯
The following command-line options 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). Pick your fighter π«-C,--create-slotβ Says "yes please make the replication slot" before starting the backup β specifically the slot named by the--slotoption. If the slot already exists? An error is raised, no cap π-llabel,--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 baby π-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. So it's giving "leave the crime scene alone" π
Note that tablespace directories are not cleaned up either way. Just so you know π«Ά
-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. Like, please don't do this in prod, bestie π-P,--progressβ Enables progress reporting β¨ Turning this on will deliver an approximate progress report during the backup. Since the database may change during the backup, this is only an approximation and may not end at exactly 100%. In particular, when WAL log is included in the backup, the total amount of data cannot be estimated in advance, and in this case the estimated target size will increase once it passes the total estimate without WAL π-rrate,--max-rate=rateβ Sets the maximum transfer rate at which data is collected from the source server. This can be useful to limit the impact ofpg_basebackupon the server. Values are in kilobytes per second. Use a suffix ofMto indicate megabytes per second. A suffix ofkis also accepted, and has no effect. Valid values are between 32 kilobytes per second and 1024 megabytes per second. Chill on the bandwidth, king πΈ
This option always affects transfer of the data directory. Transfer of WAL files is only affected if the collection method is fetch.
-Sslotname,--slot=slotnameβ This option can only be used together with-X stream, ok? It causes WAL streaming to use the specified replication slot. If the base backup is intended to be used as a streaming-replication standby using a replication slot, the standby should then use the same replication slot name asprimary_slot_name. This ensures that the primary server does not remove any necessary WAL data in the time between the end of the base backup and the start of streaming replication on the new standby π¦
The specified replication slot has to exist unless the option -C is also used.
If this option is not specified and the server supports temporary replication slots (version 10 and later), then a temporary replication slot is automatically used for WAL streaming. Free slot, no fuss β¨
-v,--verboseβ Enables verbose mode. Will output some extra steps during startup and shutdown, as well as show the exact file name that is currently being processed if progress reporting is also enabled. She's chatty and I love her π¬--manifest-checksums=algorithmβ Specifies the checksum algorithm that should be applied to each file included in the backup manifest. Currently, the available algorithms areNONE,CRC32C,SHA224,SHA256,SHA384, andSHA512. The default isCRC32Cπ
If NONE is selected, the backup manifest will not contain any checksums. Otherwise, it will contain a checksum of each file in the backup using the specified algorithm. In addition, the manifest will always contain a SHA256 checksum of its own contents. The SHA algorithms are significantly more CPU-intensive than CRC32C, so selecting one of them may increase the time required to complete the backup. Trade-offs, bestie, trade-offs βοΈ
Using a SHA hash function provides a cryptographically secure digest of each file for users who wish to verify that the backup has not been tampered with, while the CRC32C algorithm provides a checksum that is much faster to calculate; it is good at catching errors due to accidental changes but is not resistant to malicious modifications. Note that, to 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 π Not me reminding you that a checksum sitting next to the thing it's checking is lowkey useless against a hater π
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 this option is not specified, only non-UTF8 filenames are hex-encoded. This option is mostly intended to test that tools which read a backup manifest file properly handle this case. It's a little freak-in-the-lab option, we love it π§--no-estimate-sizeβ Prevents the server from estimating the total amount of backup data that will be streamed, resulting in thebackup_totalcolumn in thepg_stat_progress_basebackupview always beingNULL. No spoilers, no total π
Without this option, 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. This option is useful to avoid such estimation time if it's too long.
This option is not allowed when using --progress. Pick one, girl π
--no-manifestβ Disables generation of a backup manifest. If this option is not specified, 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 the -S option, 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. Using a replication slot is almost always preferred, because it prevents needed WAL from being removed by the server during the backup π«Ά Like, it works, but it's the "no slots left, guess we're doing this" route.
--no-verify-checksumsβ Disables verification of checksums, if they are enabled on the server the base backup is taken from. Turning off the vibe check, bold move π₯
By default, checksums are verified and checksum failures will result in a non-zero exit status. However, the base backup will not be removed in such a case, as if the --no-clean option had been used. Checksum verification failures will also be reported in the pg_stat_database view.
The following command-line options control 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 π
The option is called --dbname for consistency with other client applications, but because pg_basebackup doesn't connect to any particular database in the cluster, any database name in the connection string will be ignored. Yes, you can put a dbname in there. No, it will not do anything. She's just matching her friends' outfits π
-hhost,--host=hostβ Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is 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β Specifies the number of seconds between status packets sent back to the source server. Smaller values allow more accurate monitoring of backup progress from the server. A value of zero disables periodic status updates completely, although an update will still be sent when requested by the server, to avoid timeout-based disconnects. The default value is10seconds. So even at zero it still checks in when asked β she's not gonna ghost and get disconnected π-Uusername,--username=usernameβ Specifies the user name to connect as. Hi, it's me π-w,--no-passwordβ Prevents issuing a password prompt. If the server requires password authentication and a password is not available by other means such as a.pgpassfile, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password. No keyboard, no problem πΈ-W,--passwordβ forcespg_basebackupto ask you for a password before it connects to the source server. Bestie, this one is never essential, becausepg_basebackupwill automatically prompt you if the server demands password authentication. But it will waste a connection attempt finding out that the server wants a password, so sometimes itβs worth typing-Wto dodge that extra attempt. π
Other options are also available:
-V,--versionβ prints thepg_basebackupversion and exits. Cute and done. β¨-?,--helpβ shows help aboutpg_basebackupcommand line arguments, and exits. Your little map for the flags, bestie. π
πΏ ENVIRONMENT πΏ
This utility, like most other PostgreSQL utilities, uses the environment variables supported by libpq (see Section 34.15). iykyk π
The environment variable PG_COLOR specifies whether to use color in diagnostic messages. Possible values are always, auto and never. color vibes, literally π
π§ 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), during which pg_basebackup will appear to be idle. not it looking lazy when itβs actually working π
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 and operating system files. 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 55.4 for the precise details. the rules are giving very specific, and we respect them π¦
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. this one will straight-up not work in that setup π
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. unpack first, server later β order matters π«Ά
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. version check, bestie, no cap β
pg_basebackup will preserve group permissions for data files if group permissions are enabled on the source cluster. group perms stay slaying π
π 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 -X fetch | bzip2 > backup.tar.bz2
(This command will fail if there are multiple tablespaces in the database.) one tablespace only, bestie π
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
To create a backup of a local server with one tar file for each tablespace compressed with gzip at level 9, stored in the directory backup:
$ pg_basebackup -D backup -Ft --compress=gzip:9
π SEE ALSO π
pg_dump(1), Section 28.4.6 β go read these too, theyβre iconic π
xoxo, your PG_BASEBACKUP(1) bestie β served fresh from PostgreSQL 16.15 in 2026 π
β¨
π¦ where this page comes from
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-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