2 doc/src/sgml/ref/pg_basebackup.sgml
3 PostgreSQL documentation
6 <refentry id="app-pgbasebackup">
7 <indexterm zone="app-pgbasebackup">
8 <primary>pg_basebackup</primary>
12 <refentrytitle>pg_basebackup</refentrytitle>
13 <manvolnum>1</manvolnum>
14 <refmiscinfo>Application</refmiscinfo>
18 <refname>pg_basebackup</refname>
19 <refpurpose>take a base backup of a <productname>PostgreSQL</productname> cluster</refpurpose>
24 <command>pg_basebackup</command>
25 <arg rep="repeat"><replaceable>option</></arg>
34 <application>pg_basebackup</application> is used to take base backups of
35 a running <productname>PostgreSQL</productname> database cluster. These
36 are taken without affecting other clients to the database, and can be used
37 both for point-in-time recovery (see <xref linkend="continuous-archiving">)
38 and as the starting point for a log shipping or streaming replication standby
39 servers (see <xref linkend="warm-standby">).
43 <application>pg_basebackup</application> makes a binary copy of the database
44 cluster files, while making sure the system is put in and
45 out of backup mode automatically. Backups are always taken of the entire
46 database cluster; it is not possible to back up individual databases or
47 database objects. For individual database backups, a tool such as
48 <xref linkend="APP-PGDUMP"> must be used.
52 The backup is made over a regular <productname>PostgreSQL</productname>
53 connection, and uses the replication protocol. The connection must be made
54 with a superuser or a user having <literal>REPLICATION</literal>
55 permissions (see <xref linkend="role-attributes">),
56 and <filename>pg_hba.conf</filename> must explicitly permit the replication
57 connection. The server must also be configured
58 with <xref linkend="guc-max-wal-senders"> set high enough to leave at least
59 one session available for the backup.
63 There can be multiple <command>pg_basebackup</command>s running at the same time, but it is
64 better from a performance point of view to take only one backup, and copy
69 <application>pg_basebackup</application> can make a base backup from
70 not only the master but also the standby. To take a backup from the standby,
71 set up the standby so that it can accept replication connections (that is, set
72 <varname>max_wal_senders</> and <xref linkend="guc-hot-standby">,
73 and configure <link linkend="auth-pg-hba-conf">host-based authentication</link>).
74 You will also need to enable <xref linkend="guc-full-page-writes"> on the master.
78 Note that there are some limitations in an online backup from the standby:
83 The backup history file is not created in the database cluster backed up.
88 There is no guarantee that all WAL files required for the backup are archived
89 at the end of backup. If you are planning to use the backup for an archive
90 recovery and want to ensure that all required files are available at that moment,
91 you need to include them into the backup by using <literal>-x</> option.
96 If the standby is promoted to the master during online backup, the backup fails.
101 All WAL records required for the backup must contain sufficient full-page writes,
102 which requires you to enable <varname>full_page_writes</> on the master and
103 not to use a tool like <application>pg_compresslog</> as
104 <varname>archive_command</> to remove full-page writes from WAL files.
112 <title>Options</title>
115 The following command-line options control the location and format of the
120 <term><option>-D <replaceable class="parameter">directory</replaceable></option></term>
121 <term><option>--pgdata=<replaceable class="parameter">directory</replaceable></option></term>
124 Directory to write the output to.
125 <application>pg_basebackup</application> will create the directory and
126 any parent directories if necessary. The directory may already exist,
127 but it is an error if the directory already exists and is not empty.
130 When the backup is in tar mode, and the directory is specified as
131 <literal>-</literal> (dash), the tar file will be written to
132 <literal>stdout</literal>.
135 This option is required.
141 <term><option>-F <replaceable class="parameter">format</replaceable></option></term>
142 <term><option>--format=<replaceable class="parameter">format</replaceable></option></term>
145 Selects the format for the output. <replaceable>format</replaceable>
146 can be one of the following:
150 <term><literal>p</literal></term>
151 <term><literal>plain</literal></term>
154 Write the output as plain files, with the same layout as the
155 current data directory and tablespaces. When the cluster has
156 no additional tablespaces, the whole database will be placed in
157 the target directory. If the cluster contains additional
158 tablespaces, the main data directory will be placed in the
159 target directory, but all other tablespaces will be placed
160 in the same absolute path as they have on the server.
163 This is the default format.
169 <term><literal>t</literal></term>
170 <term><literal>tar</literal></term>
173 Write the output as tar files in the target directory. The main
174 data directory will be written to a file named
175 <filename>base.tar</filename>, and all other tablespaces will
176 be named after the tablespace OID.
179 If the value <literal>-</literal> (dash) is specified as
180 target directory, the tar contents will be written to
181 standard output, suitable for piping to for example
182 <productname>gzip</productname>. This is only possible if
183 the cluster has no additional tablespaces.
187 </variablelist></para>
192 <term><option>-r <replaceable class="parameter">rate</replaceable></option></term>
193 <term><option>--max-rate=<replaceable class="parameter">rate</replaceable></option></term>
196 The maximum transfer rate of data transferred from the server. Values are
197 in kilobytes per second. Use a suffix of <literal>M</> to indicate megabytes
198 per second. A suffix of <literal>k</> is also accepted, and has no effect.
199 Valid values are between 32 kilobytes per second and 1024 megabytes per second.
202 The purpose is to limit the impact of <application>pg_basebackup</application>
203 on the running server.
206 This option always affects transfer of the data directory. Transfer of
207 WAL files is only affected if the collection method is <literal>fetch</literal>.
213 <term><option>-R</option></term>
214 <term><option>--write-recovery-conf</option></term>
218 Write a minimal <filename>recovery.conf</filename> in the output directory (or into
219 the base archive file when using tar format) to ease setting
227 <term><option>-T <replaceable class="parameter">olddir</replaceable>=<replaceable class="parameter">newdir</replaceable></option></term>
228 <term><option>--tablespace-mapping=<replaceable class="parameter">olddir</replaceable>=<replaceable class="parameter">newdir</replaceable></option></term>
231 Relocate the tablespace in directory <replaceable>olddir</replaceable>
232 to <replaceable>newdir</replaceable> during the backup. To be
233 effective, <replaceable>olddir</replaceable> must exactly match the
234 path specification of the tablespace as it is currently defined. (But
235 it is not an error if there is no tablespace
236 in <replaceable>olddir</replaceable> contained in the backup.)
237 Both <replaceable>olddir</replaceable>
238 and <replaceable>newdir</replaceable> must be absolute paths. If a
239 path happens to contain a <literal>=</literal> sign, escape it with a
240 backslash. This option can be specified multiple times for multiple
241 tablespaces. See examples below.
245 If a tablespace is relocated in this way, the symbolic links inside
246 the main data directory are updated to point to the new location. So
247 the new data directory is ready to be used for a new server instance
248 with all tablespaces in the updated locations.
254 <term><option>--xlogdir=<replaceable class="parameter">xlogdir</replaceable></option></term>
257 Specifies the location for the transaction log directory.
258 <replaceable>xlogdir</replaceable> must be an absolute path.
259 The transaction log directory can only be specified when
260 the backup is in plain mode.
266 <term><option>-x</option></term>
267 <term><option>--xlog</option></term>
270 Using this option is equivalent of using <literal>-X</literal> with
271 method <literal>fetch</literal>.
277 <term><option>-X <replaceable class="parameter">method</replaceable></option></term>
278 <term><option>--xlog-method=<replaceable class="parameter">method</replaceable></option></term>
281 Includes the required transaction log files (WAL files) in the
282 backup. This will include all transaction logs generated during
283 the backup. If this option is specified, it is possible to start
284 a postmaster directly in the extracted directory without the need
285 to consult the log archive, thus making this a completely standalone
289 The following methods for collecting the transaction logs are
294 <term><literal>f</literal></term>
295 <term><literal>fetch</literal></term>
298 The transaction log files are collected at the end of the backup.
299 Therefore, it is necessary for the
300 <xref linkend="guc-wal-keep-segments"> parameter to be set high
301 enough that the log is not removed before the end of the backup.
302 If the log has been rotated when it's time to transfer it, the
303 backup will fail and be unusable.
309 <term><literal>s</literal></term>
310 <term><literal>stream</literal></term>
313 Stream the transaction log while the backup is created. This will
314 open a second connection to the server and start streaming the
315 transaction log in parallel while running the backup. Therefore,
316 it will use up two slots configured by the
317 <xref linkend="guc-max-wal-senders"> parameter. As long as the
318 client can keep up with transaction log received, using this mode
319 requires no extra transaction logs to be saved on the master.
329 <term><option>-z</option></term>
330 <term><option>--gzip</option></term>
333 Enables gzip compression of tar file output, with the default
334 compression level. Compression is only available when using
341 <term><option>-Z <replaceable class="parameter">level</replaceable></option></term>
342 <term><option>--compress=<replaceable class="parameter">level</replaceable></option></term>
345 Enables gzip compression of tar file output, and specifies the
346 compression level (1 through 9, 9 being best
347 compression). Compression is only available when using the tar
355 The following command-line options control the generation of the
356 backup and the running of the program.
360 <term><option>-c <replaceable class="parameter">fast|spread</replaceable></option></term>
361 <term><option>--checkpoint=<replaceable class="parameter">fast|spread</replaceable></option></term>
364 Sets checkpoint mode to fast or spread (default) (see <xref linkend="backup-lowlevel-base-backup">).
370 <term><option>-l <replaceable class="parameter">label</replaceable></option></term>
371 <term><option>--label=<replaceable class="parameter">label</replaceable></option></term>
374 Sets the label for the backup. If none is specified, a default value of
375 <quote><literal>pg_basebackup base backup</literal></quote> will be used.
381 <term><option>-P</option></term>
382 <term><option>--progress</option></term>
385 Enables progress reporting. Turning this on will deliver an approximate
386 progress report during the backup. Since the database may change during
387 the backup, this is only an approximation and may not end at exactly
388 <literal>100%</literal>. In particular, when WAL log is included in the
389 backup, the total amount of data cannot be estimated in advance, and
390 in this case the estimated target size will increase once it passes the
391 total estimate without WAL.
394 When this is enabled, the backup will start by enumerating the size of
395 the entire database, and then go back and send the actual contents.
396 This may make the backup take slightly longer, and in particular it
397 will take longer before the first data is sent.
403 <term><option>-v</option></term>
404 <term><option>--verbose</option></term>
407 Enables verbose mode. Will output some extra steps during startup and
408 shutdown, as well as show the exact file name that is currently being
409 processed if progress reporting is also enabled.
418 The following command-line options control the database connection parameters.
422 <term><option>-d <replaceable class="parameter">connstr</replaceable></option></term>
423 <term><option>--dbname=<replaceable class="parameter">connstr</replaceable></option></term>
426 Specifies parameters used to connect to the server, as a connection
427 string. See <xref linkend="libpq-connstring"> for more information.
430 The option is called <literal>--dbname</> for consistency with other
431 client applications, but because <application>pg_basebackup</application>
432 doesn't connect to any particular database in the cluster, database
433 name in the connection string will be ignored.
439 <term><option>-h <replaceable class="parameter">host</replaceable></option></term>
440 <term><option>--host=<replaceable class="parameter">host</replaceable></option></term>
443 Specifies the host name of the machine on which the server is
444 running. If the value begins with a slash, it is used as the
445 directory for the Unix domain socket. The default is taken
446 from the <envar>PGHOST</envar> environment variable, if set,
447 else a Unix domain socket connection is attempted.
453 <term><option>-p <replaceable class="parameter">port</replaceable></option></term>
454 <term><option>--port=<replaceable class="parameter">port</replaceable></option></term>
457 Specifies the TCP port or local Unix domain socket file
458 extension on which the server is listening for connections.
459 Defaults to the <envar>PGPORT</envar> environment variable, if
460 set, or a compiled-in default.
466 <term><option>-s <replaceable class="parameter">interval</replaceable></option></term>
467 <term><option>--status-interval=<replaceable class="parameter">interval</replaceable></option></term>
470 Specifies the number of seconds between status packets sent back to the
471 server. This allows for easier monitoring of the progress from server.
472 A value of zero disables the periodic status updates completely,
473 although an update will still be sent when requested by the server, to
474 avoid timeout disconnect. The default value is 10 seconds.
480 <term><option>-U <replaceable>username</replaceable></option></term>
481 <term><option>--username=<replaceable class="parameter">username</replaceable></option></term>
484 User name to connect as.
490 <term><option>-w</></term>
491 <term><option>--no-password</></term>
494 Never issue a password prompt. If the server requires
495 password authentication and a password is not available by
496 other means such as a <filename>.pgpass</filename> file, the
497 connection attempt will fail. This option can be useful in
498 batch jobs and scripts where no user is present to enter a
505 <term><option>-W</option></term>
506 <term><option>--password</option></term>
509 Force <application>pg_basebackup</application> to prompt for a
510 password before connecting to a database.
514 This option is never essential, since
515 <application>pg_basebackup</application> will automatically prompt
516 for a password if the server demands password authentication.
517 However, <application>pg_basebackup</application> will waste a
518 connection attempt finding out that the server wants a password.
519 In some cases it is worth typing <option>-W</> to avoid the extra
528 Other options are also available:
532 <term><option>-V</></term>
533 <term><option>--version</></term>
536 Print the <application>pg_basebackup</application> version and exit.
542 <term><option>-?</></term>
543 <term><option>--help</></term>
546 Show help about <application>pg_basebackup</application> command line
558 <title>Environment</title>
561 This utility, like most other <productname>PostgreSQL</> utilities,
562 uses the environment variables supported by <application>libpq</>
563 (see <xref linkend="libpq-envars">).
572 The backup will include all files in the data directory and tablespaces,
573 including the configuration files and any additional files placed in the
574 directory by third parties. Only regular files and directories are allowed
575 in the data directory, no symbolic links or special device files.
579 Tablespaces will in plain format by default be backed up to the same path
580 they have on the server, unless the
581 option <replaceable>--tablespace-mapping</replaceable> is used. Without
582 this option, running a plain format base backup on the same host as the
583 server will not work if tablespaces are in use, because the backup would
584 have to be written to the same directory locations as the original
589 <application>pg_basebackup</application> works with servers of the same
590 or an older major version, down to 9.1. However, WAL streaming mode (-X
591 stream) only works with server version 9.3.
596 <title>Examples</title>
599 To create a base backup of the server at <literal>mydbserver</literal>
600 and store it in the local directory
601 <filename>/usr/local/pgsql/data</filename>:
603 <prompt>$</prompt> <userinput>pg_basebackup -h mydbserver -D /usr/local/pgsql/data</userinput>
608 To create a backup of the local server with one compressed
609 tar file for each tablespace, and store it in the directory
610 <filename>backup</filename>, showing a progress report while running:
612 <prompt>$</prompt> <userinput>pg_basebackup -D backup -Ft -z -P</userinput>
617 To create a backup of a single-tablespace local database and compress
618 this with <productname>bzip2</productname>:
620 <prompt>$</prompt> <userinput>pg_basebackup -D - -Ft | bzip2 > backup.tar.bz2</userinput>
622 (This command will fail if there are multiple tablespaces in the
627 To create a backup of a local database where the tablespace in
628 <filename>/opt/ts</filename> is relocated
629 to <filename>./backup/ts</filename>:
631 <prompt>$</prompt> <userinput>pg_basebackup -D backup/data -T /opt/ts=$(pwd)/backup/ts</userinput>
637 <title>See Also</title>
639 <simplelist type="inline">
640 <member><xref linkend="APP-PGDUMP"></member>