1PG_BASEBACKUP(1)         PostgreSQL 12.6 Documentation        PG_BASEBACKUP(1)
2
3
4

NAME

6       pg_basebackup - take a base backup of a PostgreSQL cluster
7

SYNOPSIS

9       pg_basebackup [option...]
10

DESCRIPTION

12       pg_basebackup is used to take base backups of a running PostgreSQL
13       database cluster. These are taken without affecting other clients to
14       the database, and can be used both for point-in-time recovery (see
15       Section 25.3) and as the starting point for a log shipping or streaming
16       replication standby servers (see Section 26.2).
17
18       pg_basebackup makes a binary copy of the database cluster files, while
19       making sure the system is put in and out of backup mode automatically.
20       Backups are always taken of the entire database cluster; it is not
21       possible to back up individual databases or database objects. For
22       individual database backups, a tool such as pg_dump(1) must be used.
23
24       The backup is made over a regular PostgreSQL connection, and uses the
25       replication protocol. The connection must be made with a superuser or a
26       user having REPLICATION permissions (see Section 21.2), and pg_hba.conf
27       must explicitly permit the replication connection. The server must also
28       be configured with max_wal_senders set high enough to leave at least
29       one session available for the backup and one for WAL streaming (if
30       used).
31
32       There can be multiple pg_basebackups running at the same time, but it
33       is better from a performance point of view to take only one backup, and
34       copy the result.
35
36       pg_basebackup can make a base backup from not only the master but also
37       the standby. To take a backup from the standby, set up the standby so
38       that it can accept replication connections (that is, set
39       max_wal_senders and hot_standby, and configure host-based
40       authentication). You will also need to enable full_page_writes on the
41       master.
42
43       Note that there are some limitations in an online backup from the
44       standby:
45
46       ·   The backup history file is not created in the database cluster
47           backed up.
48
49       ·   If you are using -X none, there is no guarantee that all WAL files
50           required for the backup are archived at the end of backup.
51
52       ·   If the standby is promoted to the master during online backup, the
53           backup fails.
54
55       ·   All WAL records required for the backup must contain sufficient
56           full-page writes, which requires you to enable full_page_writes on
57           the master and not to use a tool like pg_compresslog as
58           archive_command to remove full-page writes from WAL files.
59
60

OPTIONS

62       The following command-line options control the location and format of
63       the output.
64
65       -D directory
66       --pgdata=directory
67           Directory to write the output to.  pg_basebackup will create the
68           directory and any parent directories if necessary. The directory
69           may already exist, but it is an error if the directory already
70           exists and is not empty.
71
72           When the backup is in tar mode, and the directory is specified as -
73           (dash), the tar file will be written to stdout.
74
75           This option is required.
76
77       -F format
78       --format=format
79           Selects the format for the output.  format can be one of the
80           following:
81
82           p
83           plain
84               Write the output as plain files, with the same layout as the
85               current data directory and tablespaces. When the cluster has no
86               additional tablespaces, the whole database will be placed in
87               the target directory. If the cluster contains additional
88               tablespaces, the main data directory will be placed in the
89               target directory, but all other tablespaces will be placed in
90               the same absolute path as they have on the server.
91
92               This is the default format.
93
94           t
95           tar
96               Write the output as tar files in the target directory. The main
97               data directory will be written to a file named base.tar, and
98               all other tablespaces will be named after the tablespace OID.
99
100               If the value - (dash) is specified as target directory, the tar
101               contents will be written to standard output, suitable for
102               piping to for example gzip. This is only possible if the
103               cluster has no additional tablespaces and WAL streaming is not
104               used.
105
106       -r rate
107       --max-rate=rate
108           The maximum transfer rate of data transferred from the server.
109           Values are in kilobytes per second. Use a suffix of M to indicate
110           megabytes per second. A suffix of k is also accepted, and has no
111           effect. Valid values are between 32 kilobytes per second and 1024
112           megabytes per second.
113
114           The purpose is to limit the impact of pg_basebackup on the running
115           server.
116
117           This option always affects transfer of the data directory. Transfer
118           of WAL files is only affected if the collection method is fetch.
119
120       -R
121       --write-recovery-conf
122           Create standby.signal and append connection settings to
123           postgresql.auto.conf in the output directory (or into the base
124           archive file when using tar format) to ease setting up a standby
125           server. The postgresql.auto.conf file will record the connection
126           settings and, if specified, the replication slot that pg_basebackup
127           is using, so that the streaming replication will use the same
128           settings later on.
129
130       -T olddir=newdir
131       --tablespace-mapping=olddir=newdir
132           Relocate the tablespace in directory olddir to newdir during the
133           backup. To be effective, olddir must exactly match the path
134           specification of the tablespace as it is currently defined. (But it
135           is not an error if there is no tablespace in olddir contained in
136           the backup.) Both olddir and newdir must be absolute paths. If a
137           path happens to contain a = sign, escape it with a backslash. This
138           option can be specified multiple times for multiple tablespaces.
139           See examples below.
140
141           If a tablespace is relocated in this way, the symbolic links inside
142           the main data directory are updated to point to the new location.
143           So the new data directory is ready to be used for a new server
144           instance with all tablespaces in the updated locations.
145
146       --waldir=waldir
147           Specifies the location for the write-ahead log directory.  waldir
148           must be an absolute path. The write-ahead log directory can only be
149           specified when the backup is in plain mode.
150
151       -X method
152       --wal-method=method
153           Includes the required write-ahead log files (WAL files) in the
154           backup. This will include all write-ahead logs generated during the
155           backup. Unless the method none is specified, it is possible to
156           start a postmaster directly in the extracted directory without the
157           need to consult the log archive, thus making this a completely
158           standalone backup.
159
160           The following methods for collecting the write-ahead logs are
161           supported:
162
163           n
164           none
165               Don't include write-ahead log in the backup.
166
167           f
168           fetch
169               The write-ahead log files are collected at the end of the
170               backup. Therefore, it is necessary for the wal_keep_segments
171               parameter to be set high enough that the log is not removed
172               before the end of the backup. If the log has been rotated when
173               it's time to transfer it, the backup will fail and be unusable.
174
175               When tar format mode is used, the write-ahead log files will be
176               written to the base.tar file.
177
178           s
179           stream
180               Stream the write-ahead log while the backup is created. This
181               will open a second connection to the server and start streaming
182               the write-ahead log in parallel while running the backup.
183               Therefore, it will use up two connections configured by the
184               max_wal_senders parameter. As long as the client can keep up
185               with write-ahead log received, using this mode requires no
186               extra write-ahead logs to be saved on the master.
187
188               When tar format mode is used, the write-ahead log files will be
189               written to a separate file named pg_wal.tar (if the server is a
190               version earlier than 10, the file will be named pg_xlog.tar).
191
192               This value is the default.
193
194
195       -z
196       --gzip
197           Enables gzip compression of tar file output, with the default
198           compression level. Compression is only available when using the tar
199           format, and the suffix .gz will automatically be added to all tar
200           filenames.
201
202       -Z level
203       --compress=level
204           Enables gzip compression of tar file output, and specifies the
205           compression level (0 through 9, 0 being no compression and 9 being
206           best compression). Compression is only available when using the tar
207           format, and the suffix .gz will automatically be added to all tar
208           filenames.
209
210       The following command-line options control the generation of the backup
211       and the running of the program.
212
213       -c fast|spread
214       --checkpoint=fast|spread
215           Sets checkpoint mode to fast (immediate) or spread (default) (see
216           Section 25.3.3).
217
218       -C
219       --create-slot
220           This option causes creation of a replication slot named by the
221           --slot option before starting the backup. An error is raised if the
222           slot already exists.
223
224       -l label
225       --label=label
226           Sets the label for the backup. If none is specified, a default
227           value of “pg_basebackup base backup” will be used.
228
229       -n
230       --no-clean
231           By default, when pg_basebackup aborts with an error, it removes any
232           directories it might have created before discovering that it cannot
233           finish the job (for example, data directory and write-ahead log
234           directory). This option inhibits tidying-up and is thus useful for
235           debugging.
236
237           Note that tablespace directories are not cleaned up either way.
238
239       -N
240       --no-sync
241           By default, pg_basebackup will wait for all files to be written
242           safely to disk. This option causes pg_basebackup to return without
243           waiting, which is faster, but means that a subsequent operating
244           system crash can leave the base backup corrupt. Generally, this
245           option is useful for testing but should not be used when creating a
246           production installation.
247
248       -P
249       --progress
250           Enables progress reporting. Turning this on will deliver an
251           approximate progress report during the backup. Since the database
252           may change during the backup, this is only an approximation and may
253           not end at exactly 100%. In particular, when WAL log is included in
254           the backup, the total amount of data cannot be estimated in
255           advance, and in this case the estimated target size will increase
256           once it passes the total estimate without WAL.
257
258           When this is enabled, the backup will start by enumerating the size
259           of the entire database, and then go back and send the actual
260           contents. This may make the backup take slightly longer, and in
261           particular it will take longer before the first data is sent.
262
263       -S slotname
264       --slot=slotname
265           This option can only be used together with -X stream. It causes the
266           WAL streaming to use the specified replication slot. If the base
267           backup is intended to be used as a streaming replication standby
268           using replication slots, it should then use the same replication
269           slot name in primary_slot_name. That way, it is ensured that the
270           server does not remove any necessary WAL data in the time between
271           the end of the base backup and the start of streaming replication.
272
273           The specified replication slot has to exist unless the option -C is
274           also used.
275
276           If this option is not specified and the server supports temporary
277           replication slots (version 10 and later), then a temporary
278           replication slot is automatically used for WAL streaming.
279
280       -v
281       --verbose
282           Enables verbose mode. Will output some extra steps during startup
283           and shutdown, as well as show the exact file name that is currently
284           being processed if progress reporting is also enabled.
285
286       --no-slot
287           This option prevents the creation of a temporary replication slot
288           during the backup even if it's supported by the server.
289
290           Temporary replication slots are created by default if no slot name
291           is given with the option -S when using log streaming.
292
293           The main purpose of this option is to allow taking a base backup
294           when the server is out of free replication slots. Using replication
295           slots is almost always preferred, because it prevents needed WAL
296           from being removed by the server during the backup.
297
298       --no-verify-checksums
299           Disables verification of checksums, if they are enabled on the
300           server the base backup is taken from.
301
302           By default, checksums are verified and checksum failures will
303           result in a non-zero exit status. However, the base backup will not
304           be removed in such a case, as if the --no-clean option had been
305           used. Checksum verifications failures will also be reported in the
306           pg_stat_database view.
307
308       The following command-line options control the database connection
309       parameters.
310
311       -d connstr
312       --dbname=connstr
313           Specifies parameters used to connect to the server, as a connection
314           string; these will override any conflicting command line options.
315
316           The option is called --dbname for consistency with other client
317           applications, but because pg_basebackup doesn't connect to any
318           particular database in the cluster, database name in the connection
319           string will be ignored.
320
321       -h host
322       --host=host
323           Specifies the host name of the machine on which the server is
324           running. If the value begins with a slash, it is used as the
325           directory for the Unix domain socket. The default is taken from the
326           PGHOST environment variable, if set, else a Unix domain socket
327           connection is attempted.
328
329       -p port
330       --port=port
331           Specifies the TCP port or local Unix domain socket file extension
332           on which the server is listening for connections. Defaults to the
333           PGPORT environment variable, if set, or a compiled-in default.
334
335       -s interval
336       --status-interval=interval
337           Specifies the number of seconds between status packets sent back to
338           the server. This allows for easier monitoring of the progress from
339           server. A value of zero disables the periodic status updates
340           completely, although an update will still be sent when requested by
341           the server, to avoid timeout disconnect. The default value is 10
342           seconds.
343
344       -U username
345       --username=username
346           User name to connect as.
347
348       -w
349       --no-password
350           Never issue a password prompt. If the server requires password
351           authentication and a password is not available by other means such
352           as a .pgpass file, the connection attempt will fail. This option
353           can be useful in batch jobs and scripts where no user is present to
354           enter a password.
355
356       -W
357       --password
358           Force pg_basebackup to prompt for a password before connecting to a
359           database.
360
361           This option is never essential, since pg_basebackup will
362           automatically prompt for a password if the server demands password
363           authentication. However, pg_basebackup will waste a connection
364           attempt finding out that the server wants a password. In some cases
365           it is worth typing -W to avoid the extra connection attempt.
366
367       Other options are also available:
368
369       -V
370       --version
371           Print the pg_basebackup version and exit.
372
373       -?
374       --help
375           Show help about pg_basebackup command line arguments, and exit.
376

ENVIRONMENT

378       This utility, like most other PostgreSQL utilities, uses the
379       environment variables supported by libpq (see Section 33.14).
380
381       The environment variable PG_COLOR specifies whether to use color in
382       diagnostic messages. Possible values are always, auto and never.
383

NOTES

385       At the beginning of the backup, a checkpoint needs to be written on the
386       server the backup is taken from. Especially if the option
387       --checkpoint=fast is not used, this can take some time during which
388       pg_basebackup will be appear to be idle.
389
390       The backup will include all files in the data directory and
391       tablespaces, including the configuration files and any additional files
392       placed in the directory by third parties, except certain temporary
393       files managed by PostgreSQL. But only regular files and directories are
394       copied, except that symbolic links used for tablespaces are preserved.
395       Symbolic links pointing to certain directories known to PostgreSQL are
396       copied as empty directories. Other symbolic links and special device
397       files are skipped. See Section 52.4 for the precise details.
398
399       Tablespaces will in plain format by default be backed up to the same
400       path they have on the server, unless the option --tablespace-mapping is
401       used. Without this option, running a plain format base backup on the
402       same host as the server will not work if tablespaces are in use,
403       because the backup would have to be written to the same directory
404       locations as the original tablespaces.
405
406       When tar format mode is used, it is the user's responsibility to unpack
407       each tar file before starting the PostgreSQL server. If there are
408       additional tablespaces, the tar files for them need to be unpacked in
409       the correct locations. In this case the symbolic links for those
410       tablespaces will be created by the server according to the contents of
411       the tablespace_map file that is included in the base.tar file.
412
413       pg_basebackup works with servers of the same or an older major version,
414       down to 9.1. However, WAL streaming mode (-X stream) only works with
415       server version 9.3 and later, and tar format mode (--format=tar) of the
416       current version only works with server version 9.5 or later.
417
418       pg_basebackup will preserve group permissions in both the plain and tar
419       formats if group permissions are enabled on the source cluster.
420

EXAMPLES

422       To create a base backup of the server at mydbserver and store it in the
423       local directory /usr/local/pgsql/data:
424
425           $ pg_basebackup -h mydbserver -D /usr/local/pgsql/data
426
427       To create a backup of the local server with one compressed tar file for
428       each tablespace, and store it in the directory backup, showing a
429       progress report while running:
430
431           $ pg_basebackup -D backup -Ft -z -P
432
433       To create a backup of a single-tablespace local database and compress
434       this with bzip2:
435
436           $ pg_basebackup -D - -Ft -X fetch | bzip2 > backup.tar.bz2
437
438       (This command will fail if there are multiple tablespaces in the
439       database.)
440
441       To create a backup of a local database where the tablespace in /opt/ts
442       is relocated to ./backup/ts:
443
444           $ pg_basebackup -D backup/data -T /opt/ts=$(pwd)/backup/ts
445
446

SEE ALSO

448       pg_dump(1)
449
450
451
452PostgreSQL 12.6                      2021                     PG_BASEBACKUP(1)
Impressum