Debian Codegraph @

doc/src/sgml/ref/pg_basebackup.sgml

raw
1 <!--
2 doc/src/sgml/ref/pg_basebackup.sgml
3 PostgreSQL documentation
4 -->
5
6 <refentry id="app-pgbasebackup">
7 <indexterm zone="app-pgbasebackup">
8 <primary>pg_basebackup</primary>
9 </indexterm>
10
11 <refmeta>
12 <refentrytitle><application>pg_basebackup</application></refentrytitle>
13 <manvolnum>1</manvolnum>
14 <refmiscinfo>Application</refmiscinfo>
15 </refmeta>
16
17 <refnamediv>
18 <refname>pg_basebackup</refname>
19 <refpurpose>take a base backup of a <productname>PostgreSQL</productname> cluster</refpurpose>
20 </refnamediv>
21
22 <refsynopsisdiv>
23 <cmdsynopsis>
24 <command>pg_basebackup</command>
25 <arg rep="repeat"><replaceable>option</replaceable></arg>
26 </cmdsynopsis>
27 </refsynopsisdiv>
28
29 <refsect1>
30 <title>Description</title>
31 <para>
32 <application>pg_basebackup</application> is used to take a base backup of
33 a running <productname>PostgreSQL</productname> database cluster. The backup
34 is taken without affecting other clients of the database, and can be used
35 both for point-in-time recovery (see <xref linkend="continuous-archiving"/>)
36 and as the starting point for a log-shipping or streaming-replication standby
37 server (see <xref linkend="warm-standby"/>).
38 </para>
39
40 <para>
41 <application>pg_basebackup</application> can take a full or incremental
42 base backup of the database. When used to take a full backup, it makes an
43 exact copy of the database cluster's files. When used to take an incremental
44 backup, some files that would have been part of a full backup may be
45 replaced with incremental versions of the same files, containing only those
46 blocks that have been modified since the reference backup. An incremental
47 backup cannot be used directly; instead,
48 <xref linkend="app-pgcombinebackup"/> must first
49 be used to combine it with the previous backups upon which it depends.
50 See <xref linkend="backup-incremental-backup" /> for more information
51 about incremental backups, and <xref linkend="backup-pitr-recovery" />
52 for steps to recover from a backup.
53 </para>
54
55 <para>
56 In any mode, <application>pg_basebackup</application> makes sure the server
57 is put into and out of backup mode automatically. Backups are always taken of
58 the entire database cluster; it is not possible to back up individual
59 databases or database objects. For selective backups, another tool such as
60 <xref linkend="app-pgdump"/> must be used.
61 </para>
62
63 <para>
64 The backup is made over a regular <productname>PostgreSQL</productname>
65 connection that uses the replication protocol. The connection must be made
66 with a user ID that has <literal>REPLICATION</literal> permissions
67 (see <xref linkend="role-attributes"/>) or is a superuser,
68 and <link linkend="auth-pg-hba-conf"><filename>pg_hba.conf</filename></link>
69 must permit the replication connection. The server must also be configured
70 with <xref linkend="guc-max-wal-senders"/> set high enough to provide at
71 least one walsender for the backup plus one for WAL streaming (if used).
72 </para>
73
74 <para>
75 There can be multiple <command>pg_basebackup</command>s running at the same time, but it is usually
76 better from a performance point of view to take only one backup, and copy
77 the result.
78 </para>
79
80 <para>
81 <application>pg_basebackup</application> can make a base backup from
82 not only a primary server but also a standby. To take a backup from a standby,
83 set up the standby so that it can accept replication connections (that is, set
84 <varname>max_wal_senders</varname> and <xref linkend="guc-hot-standby"/>,
85 and configure its <filename>pg_hba.conf</filename> appropriately).
86 You will also need to enable <xref linkend="guc-full-page-writes"/> on the primary.
87 </para>
88
89 <para>
90 Note that there are some limitations in taking a backup from a standby:
91
92 <itemizedlist>
93 <listitem>
94 <para>
95 The backup history file is not created in the database cluster backed up.
96 </para>
97 </listitem>
98 <listitem>
99 <para>
100 <application>pg_basebackup</application> cannot force the standby
101 to switch to a new WAL file at the end of backup.
102 When you are using <literal>-X none</literal>, if write activity on
103 the primary is low, <application>pg_basebackup</application> may
104 need to wait a long time for the last WAL file required for the backup
105 to be switched and archived. In this case, it may be useful to run
106 <function>pg_switch_wal</function> on the primary in order to
107 trigger an immediate WAL file switch.
108 </para>
109 </listitem>
110 <listitem>
111 <para>
112 If the standby is promoted to be primary during backup, the backup fails.
113 </para>
114 </listitem>
115 <listitem>
116 <para>
117 All WAL records required for the backup must contain sufficient full-page writes,
118 which requires you to enable <varname>full_page_writes</varname> on the primary.
119 </para>
120 </listitem>
121 </itemizedlist>
122 </para>
123
124 <para>
125 Whenever <application>pg_basebackup</application> is taking a base
126 backup, the server's <structname>pg_stat_progress_basebackup</structname>
127 view will report the progress of the backup.
128 See <xref linkend="basebackup-progress-reporting"/> for details.
129 </para>
130 </refsect1>
131
132 <refsect1>
133 <title>Options</title>
134
135 <para>
136 The following command-line options control the location and format of the
137 output:
138
139 <variablelist>
140 <varlistentry>
141 <term><option>-D <replaceable class="parameter">directory</replaceable></option></term>
142 <term><option>--pgdata=<replaceable class="parameter">directory</replaceable></option></term>
143 <listitem>
144 <para>
145 Sets the target directory to write the output to.
146 <application>pg_basebackup</application> will create this directory
147 (and any missing parent directories) if it does not exist. If it
148 already exists, it must be empty.
149 </para>
150 <para>
151 When the backup is in tar format, the target directory may be
152 specified as <literal>-</literal> (dash), causing the tar file to be
153 written to <literal>stdout</literal>.
154 </para>
155 <para>
156 This option is required.
157 </para>
158 </listitem>
159 </varlistentry>
160
161 <varlistentry>
162 <term><option>-F <replaceable class="parameter">format</replaceable></option></term>
163 <term><option>--format=<replaceable class="parameter">format</replaceable></option></term>
164 <listitem>
165 <para>
166 Selects the format for the output. <replaceable>format</replaceable>
167 can be one of the following:
168
169 <variablelist>
170 <varlistentry>
171 <term><literal>p</literal></term>
172 <term><literal>plain</literal></term>
173 <listitem>
174 <para>
175 Write the output as plain files, with the same layout as the
176 source server's data directory and tablespaces. When the cluster has
177 no additional tablespaces, the whole database will be placed in
178 the target directory. If the cluster contains additional
179 tablespaces, the main data directory will be placed in the
180 target directory, but all other tablespaces will be placed
181 in the same absolute path as they have on the source server.
182 (See <option>--tablespace-mapping</option> to change that.)
183 </para>
184 <para>
185 This is the default format.
186 </para>
187 </listitem>
188 </varlistentry>
189
190 <varlistentry>
191 <term><literal>t</literal></term>
192 <term><literal>tar</literal></term>
193 <listitem>
194 <para>
195 Write the output as tar files in the target directory. The main
196 data directory's contents will be written to a file named
197 <filename>base.tar</filename>, and each other tablespace will be
198 written to a separate tar file named after that tablespace's OID.
199 </para>
200 <para>
201 If the target directory is specified as <literal>-</literal>
202 (dash), the tar contents will be written to
203 standard output, suitable for piping to (for example)
204 <productname>gzip</productname>. This is only allowed if
205 the cluster has no additional tablespaces and WAL
206 streaming is not used.
207 </para>
208 </listitem>
209 </varlistentry>
210 </variablelist></para>
211 </listitem>
212 </varlistentry>
213
214 <varlistentry>
215 <term><option>-i <replaceable class="parameter">old_manifest_file</replaceable></option></term>
216 <term><option>--incremental=<replaceable class="parameter">old_manifest_file</replaceable></option></term>
217 <listitem>
218 <para>
219 Performs an <link linkend="backup-incremental-backup">incremental
220 backup</link>. The backup manifest for the reference
221 backup must be provided, and will be uploaded to the server, which will
222 respond by sending the requested incremental backup.
223 </para>
224 </listitem>
225 </varlistentry>
226
227 <varlistentry>
228 <term><option>-R</option></term>
229 <term><option>--write-recovery-conf</option></term>
230 <listitem>
231
232 <para>
233 Creates a
234 <link linkend="file-standby-signal"><filename>standby.signal</filename></link>
235 <indexterm><primary><filename>standby.signal</filename></primary><secondary>pg_basebackup --write-recovery-conf</secondary></indexterm>
236 file and appends
237 connection settings to the <filename>postgresql.auto.conf</filename>
238 file in the target directory (or within the base archive file when
239 using tar format). This eases setting up a standby server using the
240 results of the backup.
241 </para>
242 <para>
243 The <filename>postgresql.auto.conf</filename> file will record the connection
244 settings and, if specified, the replication slot
245 that <application>pg_basebackup</application> is using, so that
246 streaming replication and <link linkend="logicaldecoding-replication-slots-synchronization">
247 logical replication slot synchronization</link> will use the same
248 settings later on. The dbname will be recorded only if the dbname was
249 specified explicitly in the connection string or <link linkend="libpq-envars">
250 environment variable</link>.
251 </para>
252
253 </listitem>
254 </varlistentry>
255
256 <varlistentry>
257 <term><option>-t <replaceable class="parameter">target</replaceable></option></term>
258 <term><option>--target=<replaceable class="parameter">target</replaceable></option></term>
259 <listitem>
260
261 <para>
262 Instructs the server where to place the base backup. The default target
263 is <literal>client</literal>, which specifies that the backup should
264 be sent to the machine where <application>pg_basebackup</application>
265 is running. If the target is instead set to
266 <literal>server:/some/path</literal>, the backup will be stored on
267 the machine where the server is running in the
268 <literal>/some/path</literal> directory. Storing a backup on the
269 server requires superuser privileges or having privileges of the
270 <literal>pg_write_server_files</literal> role. If the target is set to
271 <literal>blackhole</literal>, the contents are discarded and not
272 stored anywhere. This should only be used for testing purposes, as you
273 will not end up with an actual backup.
274 </para>
275
276 <para>
277 Since WAL streaming is implemented by
278 <application>pg_basebackup</application> rather than by the server,
279 this option cannot be used together with <literal>-Xstream</literal>.
280 Since that is the default, when this option is specified, you must also
281 specify either <literal>-Xfetch</literal> or <literal>-Xnone</literal>.
282 </para>
283
284 </listitem>
285 </varlistentry>
286
287 <varlistentry>
288 <term><option>-T <replaceable class="parameter">olddir</replaceable>=<replaceable class="parameter">newdir</replaceable></option></term>
289 <term><option>--tablespace-mapping=<replaceable class="parameter">olddir</replaceable>=<replaceable class="parameter">newdir</replaceable></option></term>
290 <listitem>
291 <para>
292 Relocates the tablespace in directory <replaceable>olddir</replaceable>
293 to <replaceable>newdir</replaceable> during the backup. To be
294 effective, <replaceable>olddir</replaceable> must exactly match the
295 path specification of the tablespace as it is defined on the source
296 server. (But it is not an error if there is no tablespace
297 in <replaceable>olddir</replaceable> on the source server.)
298 Meanwhile <replaceable>newdir</replaceable> is a directory in the
299 receiving host's filesystem. As with the main target directory,
300 <replaceable>newdir</replaceable> need not exist already, but if
301 it does exist it must be empty.
302 Both <replaceable>olddir</replaceable>
303 and <replaceable>newdir</replaceable> must be absolute paths. If
304 either path needs to contain an equal sign (<literal>=</literal>),
305 precede that with a backslash. This option can be specified multiple
306 times for multiple tablespaces.
307 </para>
308
309 <para>
310 If a tablespace is relocated in this way, the symbolic links inside
311 the main data directory are updated to point to the new location. So
312 the new data directory is ready to be used for a new server instance
313 with all tablespaces in the updated locations.
314 </para>
315
316 <para>
317 Currently, this option only works with plain output format; it is
318 ignored if tar format is selected.
319 </para>
320 </listitem>
321 </varlistentry>
322
323 <varlistentry>
324 <term><option>--waldir=<replaceable class="parameter">waldir</replaceable></option></term>
325 <listitem>
326 <para>
327 Sets the directory to write WAL (write-ahead log) files to.
328 By default WAL files will be placed in
329 the <filename>pg_wal</filename> subdirectory of the target
330 directory, but this option can be used to place them elsewhere.
331 <replaceable>waldir</replaceable> must be an absolute path.
332 As with the main target directory,
333 <replaceable>waldir</replaceable> need not exist already, but if
334 it does exist it must be empty.
335 This option can only be specified when
336 the backup is in plain format.
337 </para>
338 </listitem>
339 </varlistentry>
340
341 <varlistentry>
342 <term><option>-X <replaceable class="parameter">method</replaceable></option></term>
343 <term><option>--wal-method=<replaceable class="parameter">method</replaceable></option></term>
344 <listitem>
345 <para>
346 Includes the required WAL (write-ahead log) files in the
347 backup. This will include all write-ahead logs generated during
348 the backup. Unless the method <literal>none</literal> is specified,
349 it is possible to start a postmaster in the target
350 directory without the need to consult the WAL archive, thus
351 making the output a completely standalone backup.
352 </para>
353 <para>
354 The following <replaceable>method</replaceable>s for collecting the
355 write-ahead logs are supported:
356
357 <variablelist>
358 <varlistentry>
359 <term><literal>n</literal></term>
360 <term><literal>none</literal></term>
361 <listitem>
362 <para>
363 Don't include write-ahead logs in the backup.
364 </para>
365 </listitem>
366 </varlistentry>
367
368 <varlistentry>
369 <term><literal>f</literal></term>
370 <term><literal>fetch</literal></term>
371 <listitem>
372 <para>
373 The write-ahead log files are collected at the end of the backup.
374 Therefore, it is necessary for the source server's
375 <xref linkend="guc-wal-keep-size"/> parameter to be set high
376 enough that the required log data is not removed before the end
377 of the backup. If the required log data has been recycled
378 before it's time to transfer it, the backup will fail and be
379 unusable.
380 </para>
381 <para>
382 When tar format is used, the write-ahead log files will be
383 included in the <filename>base.tar</filename> file.
384 </para>
385 </listitem>
386 </varlistentry>
387
388 <varlistentry>
389 <term><literal>s</literal></term>
390 <term><literal>stream</literal></term>
391 <listitem>
392 <para>
393 Stream write-ahead log data while the backup is being taken.
394 This method will open a second connection to the server and
395 start streaming the write-ahead log in parallel while running
396 the backup. Therefore, it will require two replication
397 connections not just one. As long as the client can keep up
398 with the write-ahead log data, using this method requires no
399 extra write-ahead logs to be saved on the source server.
400 </para>
401 <para>
402 When tar format is used, the write-ahead log files will be
403 written to a separate file named <filename>pg_wal.tar</filename>
404 (if the server is a version earlier than 10, the file will be named
405 <filename>pg_xlog.tar</filename>).
406 </para>
407 <para>
408 This value is the default.
409 </para>
410 </listitem>
411 </varlistentry>
412 </variablelist></para>
413 </listitem>
414 </varlistentry>
415
416 <varlistentry>
417 <term><option>-z</option></term>
418 <term><option>--gzip</option></term>
419 <listitem>
420 <para>
421 Enables gzip compression of tar file output, with the default
422 compression level. Compression is only available when using
423 the tar format, and the suffix <filename>.gz</filename> will
424 automatically be added to all tar filenames.
425 </para>
426 </listitem>
427 </varlistentry>
428
429 <varlistentry>
430 <term><option>-Z <replaceable class="parameter">level</replaceable></option></term>
431 <term><option>-Z [{client|server}-]<replaceable class="parameter">method</replaceable>[:<replaceable>detail</replaceable>]</option></term>
432 <term><option>--compress=<replaceable class="parameter">level</replaceable></option></term>
433 <term><option>--compress=[{client|server}-]<replaceable class="parameter">method</replaceable>[:<replaceable>detail</replaceable>]</option></term>
434 <listitem>
435 <para>
436 Requests compression of the backup. If <literal>client</literal> or
437 <literal>server</literal> is included, it specifies where the
438 compression is to be performed. Compressing on the server will reduce
439 transfer bandwidth but will increase server CPU consumption. The
440 default is <literal>client</literal> except when
441 <literal>--target</literal> is used. In that case, the backup is not
442 being sent to the client, so only server compression is sensible.
443 When <literal>-Xstream</literal>, which is the default, is used,
444 server-side compression will not be applied to the WAL. To compress
445 the WAL, use client-side compression, or
446 specify <literal>-Xfetch</literal>.
447 </para>
448 <para>
449 The compression method can be set to <literal>gzip</literal>,
450 <literal>lz4</literal>, <literal>zstd</literal>,
451 <literal>none</literal> for no compression or an integer (no
452 compression if 0, <literal>gzip</literal> if greater than 0).
453 A compression detail string can optionally be specified.
454 If the detail string is an integer, it specifies the compression
455 level. Otherwise, it should be a comma-separated list of items,
456 each of the form
457 <replaceable>keyword</replaceable> or
458 <replaceable>keyword=value</replaceable>.
459 Currently, the supported keywords are <literal>level</literal>,
460 <literal>long</literal>, and <literal>workers</literal>.
461 The detail string cannot be used when the compression method
462 is specified as a plain integer.
463 </para>
464 <para>
465 If no compression level is specified, the default compression level
466 will be used. If only a level is specified without mentioning an
467 algorithm, <literal>gzip</literal> compression will be used if the
468 level is greater than 0, and no compression will be used if the level
469 is 0.
470 </para>
471 <para>
472 When the tar format is used with <literal>gzip</literal>,
473 <literal>lz4</literal>, or <literal>zstd</literal>, the suffix
474 <filename>.gz</filename>, <filename>.lz4</filename>, or
475 <filename>.zst</filename>, respectively, will be automatically added to
476 all tar filenames. When the plain format is used, client-side
477 compression may not be specified, but it is still possible to request
478 server-side compression. If this is done, the server will compress the
479 backup for transmission, and the client will decompress and extract it.
480 </para>
481 <para>
482 When this option is used in combination with
483 <literal>-Xstream</literal>, <literal>pg_wal.tar</literal> will
484 be compressed using <literal>gzip</literal> if client-side gzip
485 compression is selected, but will not be compressed if any other
486 compression algorithm is selected, or if server-side compression
487 is selected.
488 </para>
489 </listitem>
490 </varlistentry>
491 </variablelist>
492 </para>
493 <para>
494 The following command-line options control the generation of the
495 backup and the invocation of the program:
496
497 <variablelist>
498 <varlistentry>
499 <term><option>-c {fast|spread}</option></term>
500 <term><option>--checkpoint={fast|spread}</option></term>
501 <listitem>
502 <para>
503 Sets checkpoint mode to fast (immediate) or spread (the default)
504 (see <xref linkend="backup-lowlevel-base-backup"/>).
505 </para>
506 </listitem>
507 </varlistentry>
508
509 <varlistentry>
510 <term><option>-C</option></term>
511 <term><option>--create-slot</option></term>
512 <listitem>
513 <para>
514 Specifies that the replication slot named by the
515 <literal>--slot</literal> option should be created before starting
516 the backup. An error is raised if the slot already exists.
517 </para>
518 </listitem>
519 </varlistentry>
520
521 <varlistentry>
522 <term><option>-l <replaceable class="parameter">label</replaceable></option></term>
523 <term><option>--label=<replaceable class="parameter">label</replaceable></option></term>
524 <listitem>
525 <para>
526 Sets the label for the backup. If none is specified, a default value of
527 <quote><literal>pg_basebackup base backup</literal></quote> will be used.
528 </para>
529 </listitem>
530 </varlistentry>
531
532 <varlistentry>
533 <term><option>-n</option></term>
534 <term><option>--no-clean</option></term>
535 <listitem>
536 <para>
537 By default, when <command>pg_basebackup</command> aborts with an
538 error, it removes any directories it might have created before
539 discovering that it cannot finish the job (for example, the target
540 directory and write-ahead log directory). This option inhibits
541 tidying-up and is thus useful for debugging.
542 </para>
543
544 <para>
545 Note that tablespace directories are not cleaned up either way.
546 </para>
547 </listitem>
548 </varlistentry>
549
550 <varlistentry>
551 <term><option>-N</option></term>
552 <term><option>--no-sync</option></term>
553 <listitem>
554 <para>
555 By default, <command>pg_basebackup</command> will wait for all files
556 to be written safely to disk. This option causes
557 <command>pg_basebackup</command> to return without waiting, which is
558 faster, but means that a subsequent operating system crash can leave
559 the base backup corrupt. Generally, this option is useful for testing
560 but should not be used when creating a production installation.
561 </para>
562 </listitem>
563 </varlistentry>
564
565 <varlistentry>
566 <term><option>-P</option></term>
567 <term><option>--progress</option></term>
568 <listitem>
569 <para>
570 Enables progress reporting. Turning this on will deliver an approximate
571 progress report during the backup. Since the database may change during
572 the backup, this is only an approximation and may not end at exactly
573 <literal>100%</literal>. In particular, when WAL log is included in the
574 backup, the total amount of data cannot be estimated in advance, and
575 in this case the estimated target size will increase once it passes the
576 total estimate without WAL.
577 </para>
578 </listitem>
579 </varlistentry>
580
581 <varlistentry>
582 <term><option>-r <replaceable class="parameter">rate</replaceable></option></term>
583 <term><option>--max-rate=<replaceable class="parameter">rate</replaceable></option></term>
584 <listitem>
585 <para>
586 Sets the maximum transfer rate at which data is collected from the
587 source server. This can be useful to limit the impact
588 of <application>pg_basebackup</application> on the server. Values
589 are in kilobytes per second. Use a suffix of <literal>M</literal>
590 to indicate megabytes per second. A suffix of <literal>k</literal>
591 is also accepted, and has no effect. Valid values are between 32
592 kilobytes per second and 1024 megabytes per second.
593 </para>
594 <para>
595 This option always affects transfer of the data directory. Transfer of
596 WAL files is only affected if the collection method
597 is <literal>fetch</literal>.
598 </para>
599 </listitem>
600 </varlistentry>
601
602 <varlistentry>
603 <term><option>-S <replaceable>slotname</replaceable></option></term>
604 <term><option>--slot=<replaceable class="parameter">slotname</replaceable></option></term>
605 <listitem>
606 <para>
607 This option can only be used together with <literal>-X
608 stream</literal>. It causes WAL streaming to use the specified
609 replication slot. If the base backup is intended to be used as a
610 streaming-replication standby using a replication slot, the standby
611 should then use the same replication slot name as
612 <xref linkend="guc-primary-slot-name"/>. This ensures that the
613 primary server does not remove any necessary WAL data in the time
614 between the end of the base backup and the start of streaming
615 replication on the new standby.
616 </para>
617 <para>
618 The specified replication slot has to exist unless the
619 option <option>-C</option> is also used.
620 </para>
621 <para>
622 If this option is not specified and the server supports temporary
623 replication slots (version 10 and later), then a temporary replication
624 slot is automatically used for WAL streaming.
625 </para>
626 </listitem>
627 </varlistentry>
628
629 <varlistentry>
630 <term><option>--sync-method=<replaceable class="parameter">method</replaceable></option></term>
631 <listitem>
632 <para>
633 When set to <literal>fsync</literal>, which is the default,
634 <command>pg_basebackup</command> will recursively open and synchronize
635 all files in the backup directory. When the plain format is used, the
636 search for files will follow symbolic links for the WAL directory and
637 each configured tablespace.
638 </para>
639 <para>
640 On Linux, <literal>syncfs</literal> may be used instead to ask the
641 operating system to synchronize the whole file system that contains the
642 backup directory. When the plain format is used,
643 <command>pg_basebackup</command> will also synchronize the file systems
644 that contain the WAL files and each tablespace. See
645 <xref linkend="guc-recovery-init-sync-method"/> for information about
646 the caveats to be aware of when using <literal>syncfs</literal>.
647 </para>
648 <para>
649 This option has no effect when <option>--no-sync</option> is used.
650 </para>
651 </listitem>
652 </varlistentry>
653
654 <varlistentry>
655 <term><option>-v</option></term>
656 <term><option>--verbose</option></term>
657 <listitem>
658 <para>
659 Enables verbose mode. Will output some extra steps during startup and
660 shutdown, as well as show the exact file name that is currently being
661 processed if progress reporting is also enabled.
662 </para>
663 </listitem>
664 </varlistentry>
665
666 <varlistentry>
667 <term><option>--manifest-checksums=<replaceable class="parameter">algorithm</replaceable></option></term>
668 <listitem>
669 <para>
670 Specifies the checksum algorithm that should be applied to each file
671 included in the backup manifest. Currently, the available
672 algorithms are <literal>NONE</literal>, <literal>CRC32C</literal>,
673 <literal>SHA224</literal>, <literal>SHA256</literal>,
674 <literal>SHA384</literal>, and <literal>SHA512</literal>.
675 The default is <literal>CRC32C</literal>.
676 </para>
677 <para>
678 If <literal>NONE</literal> is selected, the backup manifest will
679 not contain any checksums. Otherwise, it will contain a checksum
680 of each file in the backup using the specified algorithm. In addition,
681 the manifest will always contain a <literal>SHA256</literal>
682 checksum of its own contents. The <literal>SHA</literal> algorithms
683 are significantly more CPU-intensive than <literal>CRC32C</literal>,
684 so selecting one of them may increase the time required to complete
685 the backup.
686 </para>
687 <para>
688 Using a SHA hash function provides a cryptographically secure digest
689 of each file for users who wish to verify that the backup has not been
690 tampered with, while the CRC-32C algorithm provides a checksum that is
691 much faster to calculate; it is good at catching errors due to accidental
692 changes but is not resistant to malicious modifications. Note that, to
693 be useful against an adversary who has access to the backup, the backup
694 manifest would need to be stored securely elsewhere or otherwise
695 verified not to have been modified since the backup was taken.
696 </para>
697 <para>
698 <xref linkend="app-pgverifybackup"/> can be used to check the
699 integrity of a backup against the backup manifest.
700 </para>
701 </listitem>
702 </varlistentry>
703
704 <varlistentry>
705 <term><option>--manifest-force-encode</option></term>
706 <listitem>
707 <para>
708 Forces all filenames in the backup manifest to be hex-encoded.
709 If this option is not specified, only non-UTF8 filenames are
710 hex-encoded. This option is mostly intended to test that tools which
711 read a backup manifest file properly handle this case.
712 </para>
713 </listitem>
714 </varlistentry>
715
716 <varlistentry>
717 <term><option>--no-estimate-size</option></term>
718 <listitem>
719 <para>
720 Prevents the server from estimating the total
721 amount of backup data that will be streamed, resulting in the
722 <structfield>backup_total</structfield> column in the
723 <structname>pg_stat_progress_basebackup</structname> view
724 always being <literal>NULL</literal>.
725 </para>
726 <para>
727 Without this option, the backup will start by enumerating
728 the size of the entire database, and then go back and send
729 the actual contents. This may make the backup take slightly
730 longer, and in particular it will take longer before the first
731 data is sent. This option is useful to avoid such estimation
732 time if it's too long.
733 </para>
734 <para>
735 This option is not allowed when using <option>--progress</option>.
736 </para>
737 </listitem>
738 </varlistentry>
739
740 <varlistentry>
741 <term><option>--no-manifest</option></term>
742 <listitem>
743 <para>
744 Disables generation of a backup manifest. If this option is not
745 specified, the server will generate and send a backup manifest
746 which can be verified using <xref linkend="app-pgverifybackup"/>.
747 The manifest is a list of every file present in the backup with the
748 exception of any WAL files that may be included. It also stores the
749 size, last modification time, and an optional checksum for each file.
750 </para>
751 </listitem>
752 </varlistentry>
753
754 <varlistentry>
755 <term><option>--no-slot</option></term>
756 <listitem>
757 <para>
758 Prevents the creation of a temporary replication slot
759 for the backup.
760 </para>
761 <para>
762 By default, if log streaming is selected but no slot name is given
763 with the <option>-S</option> option, then a temporary replication
764 slot is created (if supported by the source server).
765 </para>
766 <para>
767 The main purpose of this option is to allow taking a base backup when
768 the server has no free replication slots. Using a replication slot
769 is almost always preferred, because it prevents needed WAL from being
770 removed by the server during the backup.
771 </para>
772 </listitem>
773 </varlistentry>
774
775 <varlistentry>
776 <term><option>--no-verify-checksums</option></term>
777 <listitem>
778 <para>
779 Disables verification of checksums, if they are enabled on the server
780 the base backup is taken from.
781 </para>
782 <para>
783 By default, checksums are verified and checksum failures will result
784 in a non-zero exit status. However, the base backup will not be
785 removed in such a case, as if the <option>--no-clean</option> option
786 had been used. Checksum verification failures will also be reported
787 in the <link linkend="monitoring-pg-stat-database-view">
788 <structname>pg_stat_database</structname></link> view.
789 </para>
790 </listitem>
791 </varlistentry>
792 </variablelist>
793 </para>
794
795 <para>
796 The following command-line options control the connection to the source
797 server:
798
799 <variablelist>
800 <varlistentry>
801 <term><option>-d <replaceable class="parameter">connstr</replaceable></option></term>
802 <term><option>--dbname=<replaceable class="parameter">connstr</replaceable></option></term>
803 <listitem>
804 <para>
805 Specifies parameters used to connect to the server, as a <link
806 linkend="libpq-connstring">connection string</link>; these
807 will override any conflicting command line options.
808 </para>
809 <para>
810 This option is called <literal>--dbname</literal> for consistency with other
811 client applications, but because <application>pg_basebackup</application>
812 doesn't connect to any particular database in the cluster, any database
813 name included in the connection string will be ignored by the server.
814 However, a database name supplied that way overrides the default
815 database name (<literal>replication</literal>) for purposes of
816 looking up the replication connection's password
817 in <filename>~/.pgpass</filename>. Similarly, middleware or proxies
818 used in connecting to <productname>PostgreSQL</productname> might
819 utilize the name for purposes such as connection routing. The
820 database name can also be used
821 by <link linkend="logicaldecoding-replication-slots-synchronization">
822 logical replication slot synchronization</link>.
823 </para>
824 </listitem>
825 </varlistentry>
826
827 <varlistentry>
828 <term><option>-h <replaceable class="parameter">host</replaceable></option></term>
829 <term><option>--host=<replaceable class="parameter">host</replaceable></option></term>
830 <listitem>
831 <para>
832 Specifies the host name of the machine on which the server is
833 running. If the value begins with a slash, it is used as the
834 directory for a Unix domain socket. The default is taken
835 from the <envar>PGHOST</envar> environment variable, if set,
836 else a Unix domain socket connection is attempted.
837 </para>
838 </listitem>
839 </varlistentry>
840
841 <varlistentry>
842 <term><option>-p <replaceable class="parameter">port</replaceable></option></term>
843 <term><option>--port=<replaceable class="parameter">port</replaceable></option></term>
844 <listitem>
845 <para>
846 Specifies the TCP port or local Unix domain socket file
847 extension on which the server is listening for connections.
848 Defaults to the <envar>PGPORT</envar> environment variable, if
849 set, or a compiled-in default.
850 </para>
851 </listitem>
852 </varlistentry>
853
854 <varlistentry>
855 <term><option>-s <replaceable class="parameter">interval</replaceable></option></term>
856 <term><option>--status-interval=<replaceable class="parameter">interval</replaceable></option></term>
857 <listitem>
858 <para>
859 Specifies the number of seconds between status packets sent back to
860 the source server. Smaller values allow more accurate monitoring of
861 backup progress from the server.
862 A value of zero disables periodic status updates completely,
863 although an update will still be sent when requested by the server, to
864 avoid timeout-based disconnects. The default value is 10 seconds.
865 </para>
866 </listitem>
867 </varlistentry>
868
869 <varlistentry>
870 <term><option>-U <replaceable>username</replaceable></option></term>
871 <term><option>--username=<replaceable class="parameter">username</replaceable></option></term>
872 <listitem>
873 <para>
874 Specifies the user name to connect as.
875 </para>
876 </listitem>
877 </varlistentry>
878
879 <varlistentry>
880 <term><option>-w</option></term>
881 <term><option>--no-password</option></term>
882 <listitem>
883 <para>
884 Prevents issuing a password prompt. If the server requires
885 password authentication and a password is not available by
886 other means such as a <filename>.pgpass</filename> file, the
887 connection attempt will fail. This option can be useful in
888 batch jobs and scripts where no user is present to enter a
889 password.
890 </para>
891 </listitem>
892 </varlistentry>
893
894 <varlistentry>
895 <term><option>-W</option></term>
896 <term><option>--password</option></term>
897 <listitem>
898 <para>
899 Forces <application>pg_basebackup</application> to prompt for a
900 password before connecting to the source server.
901 </para>
902
903 <para>
904 This option is never essential, since
905 <application>pg_basebackup</application> will automatically prompt
906 for a password if the server demands password authentication.
907 However, <application>pg_basebackup</application> will waste a
908 connection attempt finding out that the server wants a password.
909 In some cases it is worth typing <option>-W</option> to avoid the extra
910 connection attempt.
911 </para>
912 </listitem>
913 </varlistentry>
914 </variablelist>
915 </para>
916
917 <para>
918 Other options are also available:
919
920 <variablelist>
921 <varlistentry>
922 <term><option>-V</option></term>
923 <term><option>--version</option></term>
924 <listitem>
925 <para>
926 Prints the <application>pg_basebackup</application> version and exits.
927 </para>
928 </listitem>
929 </varlistentry>
930
931 <varlistentry>
932 <term><option>-?</option></term>
933 <term><option>--help</option></term>
934 <listitem>
935 <para>
936 Shows help about <application>pg_basebackup</application> command line
937 arguments, and exits.
938 </para>
939 </listitem>
940 </varlistentry>
941
942 </variablelist>
943 </para>
944
945 </refsect1>
946
947 <refsect1>
948 <title>Environment</title>
949
950 <para>
951 This utility, like most other <productname>PostgreSQL</productname> utilities,
952 uses the environment variables supported by <application>libpq</application>
953 (see <xref linkend="libpq-envars"/>).
954 </para>
955
956 <para>
957 The environment variable <envar>PG_COLOR</envar> specifies whether to use
958 color in diagnostic messages. Possible values are
959 <literal>always</literal>, <literal>auto</literal> and
960 <literal>never</literal>.
961 </para>
962 </refsect1>
963
964 <refsect1>
965 <title>Notes</title>
966
967 <para>
968 At the beginning of the backup, a checkpoint needs to be performed on the
969 source server. This can take some time (especially if the option
970 <literal>--checkpoint=fast</literal> is not used), during
971 which <application>pg_basebackup</application> will appear to be idle.
972 </para>
973
974 <para>
975 The backup will include all files in the data directory and tablespaces,
976 including the configuration files and any additional files placed in the
977 directory by third parties, except certain temporary files managed by
978 PostgreSQL and operating system files. But only regular files and
979 directories are copied, except that
980 symbolic links used for tablespaces are preserved. Symbolic links pointing
981 to certain directories known to PostgreSQL are copied as empty directories.
982 Other symbolic links and special device files are skipped.
983 See <xref linkend="protocol-replication"/> for the precise details.
984 </para>
985
986 <para>
987 In plain format, tablespaces will be backed up to the same path
988 they have on the source server, unless the
989 option <literal>--tablespace-mapping</literal> is used. Without
990 this option, running a plain format base backup on the same host as the
991 server will not work if tablespaces are in use, because the backup would
992 have to be written to the same directory locations as the original
993 tablespaces.
994 </para>
995
996 <para>
997 When tar format is used, it is the user's responsibility to unpack each
998 tar file before starting a PostgreSQL server that uses the data. If there
999 are additional tablespaces, the
1000 tar files for them need to be unpacked in the correct locations. In this
1001 case the symbolic links for those tablespaces will be created by the server
1002 according to the contents of the <filename>tablespace_map</filename> file that is
1003 included in the <filename>base.tar</filename> file.
1004 </para>
1005
1006 <para>
1007 <application>pg_basebackup</application> works with servers of the same
1008 or older major version, down to 9.1. However, WAL streaming mode (<literal>-X
1009 stream</literal>) only works with server version 9.3 and later, the tar format
1010 (<literal>--format=tar</literal>) only works with server version 9.5
1011 and later, and incremental backup (<literal>--incremental</literal>) only works
1012 with server version 17 and later.
1013 </para>
1014
1015 <para>
1016 <application>pg_basebackup</application> will preserve group permissions
1017 for data files if group permissions are enabled on the source cluster.
1018 </para>
1019
1020 </refsect1>
1021
1022 <refsect1>
1023 <title>Examples</title>
1024
1025 <para>
1026 To create a base backup of the server at <literal>mydbserver</literal>
1027 and store it in the local directory
1028 <filename>/usr/local/pgsql/data</filename>:
1029 <screen>
1030 <prompt>$</prompt> <userinput>pg_basebackup -h mydbserver -D /usr/local/pgsql/data</userinput>
1031 </screen>
1032 </para>
1033
1034 <para>
1035 To create a backup of the local server with one compressed
1036 tar file for each tablespace, and store it in the directory
1037 <filename>backup</filename>, showing a progress report while running:
1038 <screen>
1039 <prompt>$</prompt> <userinput>pg_basebackup -D backup -Ft -z -P</userinput>
1040 </screen>
1041 </para>
1042
1043 <para>
1044 To create a backup of a single-tablespace local database and compress
1045 this with <productname>bzip2</productname>:
1046 <screen>
1047 <prompt>$</prompt> <userinput>pg_basebackup -D - -Ft -X fetch | bzip2 &gt; backup.tar.bz2</userinput>
1048 </screen>
1049 (This command will fail if there are multiple tablespaces in the
1050 database.)
1051 </para>
1052
1053 <para>
1054 To create a backup of a local database where the tablespace in
1055 <filename>/opt/ts</filename> is relocated
1056 to <filename>./backup/ts</filename>:
1057 <screen>
1058 <prompt>$</prompt> <userinput>pg_basebackup -D backup/data -T /opt/ts=$(pwd)/backup/ts</userinput>
1059 </screen></para>
1060
1061 <para>
1062 To create a backup of the local server with one tar file for each tablespace
1063 compressed with <application>gzip</application> at level 9, stored in the
1064 directory <filename>backup</filename>:
1065 <screen>
1066 <prompt>$</prompt> <userinput>pg_basebackup -D backup -Ft --compress=gzip:9</userinput>
1067 </screen></para>
1068
1069 </refsect1>
1070
1071 <refsect1>
1072 <title>See Also</title>
1073
1074 <simplelist type="inline">
1075 <member><xref linkend="app-pgdump"/></member>
1076 <member><xref linkend="basebackup-progress-reporting"/></member>
1077 </simplelist>
1078 </refsect1>
1079
1080 </refentry>