2 doc/src/sgml/ref/pg_basebackup.sgml
3 PostgreSQL documentation
6 <refentry id="app-pgbasebackup">
8 <refentrytitle>pg_basebackup</refentrytitle>
9 <manvolnum>1</manvolnum>
10 <refmiscinfo>Application</refmiscinfo>
14 <refname>pg_basebackup</refname>
15 <refpurpose>take a base backup of a <productname>PostgreSQL</productname> cluster</refpurpose>
18 <indexterm zone="app-pgbasebackup">
19 <primary>pg_basebackup</primary>
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 automatically 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</option></term>
193 <term><option>--write-recovery-conf</option></term>
197 Write a minimal <filename>recovery.conf</filename> in the output directory (or into
198 the base archive file when using tar format) to ease setting
206 <term><option>-x</option></term>
207 <term><option>--xlog</option></term>
210 Using this option is equivalent of using <literal>-X</literal> with
211 method <literal>fetch</literal>.
217 <term><option>-X <replaceable class="parameter">method</replaceable></option></term>
218 <term><option>--xlog-method=<replaceable class="parameter">method</replaceable></option></term>
221 Includes the required transaction log files (WAL files) in the
222 backup. This will include all transaction logs generated during
223 the backup. If this option is specified, it is possible to start
224 a postmaster directly in the extracted directory without the need
225 to consult the log archive, thus making this a completely standalone
229 The following methods for collecting the transaction logs are
234 <term><literal>f</literal></term>
235 <term><literal>fetch</literal></term>
238 The transaction log files are collected at the end of the backup.
239 Therefore, it is necessary for the
240 <xref linkend="guc-wal-keep-segments"> parameter to be set high
241 enough that the log is not removed before the end of the backup.
242 If the log has been rotated when it's time to transfer it, the
243 backup will fail and be unusable.
249 <term><literal>s</literal></term>
250 <term><literal>stream</literal></term>
253 Stream the transaction log while the backup is created. This will
254 open a second connection to the server and start streaming the
255 transaction log in parallel while running the backup. Therefore,
256 it will use up two slots configured by the
257 <xref linkend="guc-max-wal-senders"> parameter. As long as the
258 client can keep up with transaction log received, using this mode
259 requires no extra transaction logs to be saved on the master.
269 <term><option>-z</option></term>
270 <term><option>--gzip</option></term>
273 Enables gzip compression of tar file output, with the default
274 compression level. Compression is only available when using
281 <term><option>-Z <replaceable class="parameter">level</replaceable></option></term>
282 <term><option>--compress=<replaceable class="parameter">level</replaceable></option></term>
285 Enables gzip compression of tar file output, and specifies the
286 compression level (1 through 9, 9 being best
287 compression). Compression is only available when using the tar
295 The following command-line options control the generation of the
296 backup and the running of the program.
300 <term><option>-c <replaceable class="parameter">fast|spread</replaceable></option></term>
301 <term><option>--checkpoint=<replaceable class="parameter">fast|spread</replaceable></option></term>
304 Sets checkpoint mode to fast or spread (default).
310 <term><option>-l <replaceable class="parameter">label</replaceable></option></term>
311 <term><option>--label=<replaceable class="parameter">label</replaceable></option></term>
314 Sets the label for the backup. If none is specified, a default value of
315 <quote><literal>pg_basebackup base backup</literal></quote> will be used.
321 <term><option>-P</option></term>
322 <term><option>--progress</option></term>
325 Enables progress reporting. Turning this on will deliver an approximate
326 progress report during the backup. Since the database may change during
327 the backup, this is only an approximation and may not end at exactly
328 <literal>100%</literal>. In particular, when WAL log is included in the
329 backup, the total amount of data cannot be estimated in advance, and
330 in this case the estimated target size will increase once it passes the
331 total estimate without WAL.
334 When this is enabled, the backup will start by enumerating the size of
335 the entire database, and then go back and send the actual contents.
336 This may make the backup take slightly longer, and in particular it
337 will take longer before the first data is sent.
343 <term><option>-v</option></term>
344 <term><option>--verbose</option></term>
347 Enables verbose mode. Will output some extra steps during startup and
348 shutdown, as well as show the exact file name that is currently being
349 processed if progress reporting is also enabled.
358 The following command-line options control the database connection parameters.
362 <term><option>-d <replaceable class="parameter">connstr</replaceable></option></term>
363 <term><option>--dbname=<replaceable class="parameter">connstr</replaceable></option></term>
366 Specifies parameters used to connect to the server, as a connection
367 string. See <xref linkend="libpq-connstring"> for more information.
370 The option is called <literal>--dbname</> for consistency with other
371 client applications, but because <application>pg_basebackup</application>
372 doesn't connect to any particular database in the cluster, database
373 name in the connection string will be ignored.
379 <term><option>-h <replaceable class="parameter">host</replaceable></option></term>
380 <term><option>--host=<replaceable class="parameter">host</replaceable></option></term>
383 Specifies the host name of the machine on which the server is
384 running. If the value begins with a slash, it is used as the
385 directory for the Unix domain socket. The default is taken
386 from the <envar>PGHOST</envar> environment variable, if set,
387 else a Unix domain socket connection is attempted.
393 <term><option>-p <replaceable class="parameter">port</replaceable></option></term>
394 <term><option>--port=<replaceable class="parameter">port</replaceable></option></term>
397 Specifies the TCP port or local Unix domain socket file
398 extension on which the server is listening for connections.
399 Defaults to the <envar>PGPORT</envar> environment variable, if
400 set, or a compiled-in default.
406 <term><option>-s <replaceable class="parameter">interval</replaceable></option></term>
407 <term><option>--status-interval=<replaceable class="parameter">interval</replaceable></option></term>
410 Specifies the number of seconds between status packets sent back to the
411 server. This allows for easier monitoring of the progress from server.
412 A value of zero disables the periodic status updates completely,
413 although an update will still be sent when requested by the server, to
414 avoid timeout disconnect. The default value is 10 seconds.
420 <term><option>-U <replaceable>username</replaceable></option></term>
421 <term><option>--username=<replaceable class="parameter">username</replaceable></option></term>
424 User name to connect as.
430 <term><option>-w</></term>
431 <term><option>--no-password</></term>
434 Never issue a password prompt. If the server requires
435 password authentication and a password is not available by
436 other means such as a <filename>.pgpass</filename> file, the
437 connection attempt will fail. This option can be useful in
438 batch jobs and scripts where no user is present to enter a
445 <term><option>-W</option></term>
446 <term><option>--password</option></term>
449 Force <application>pg_basebackup</application> to prompt for a
450 password before connecting to a database.
454 This option is never essential, since
455 <application>pg_basebackup</application> will automatically prompt
456 for a password if the server demands password authentication.
457 However, <application>pg_basebackup</application> will waste a
458 connection attempt finding out that the server wants a password.
459 In some cases it is worth typing <option>-W</> to avoid the extra
468 Other options are also available:
472 <term><option>-V</></term>
473 <term><option>--version</></term>
476 Print the <application>pg_basebackup</application> version and exit.
482 <term><option>-?</></term>
483 <term><option>--help</></term>
486 Show help about <application>pg_basebackup</application> command line
498 <title>Environment</title>
501 This utility, like most other <productname>PostgreSQL</> utilities,
502 uses the environment variables supported by <application>libpq</>
503 (see <xref linkend="libpq-envars">).
512 The backup will include all files in the data directory and tablespaces,
513 including the configuration files and any additional files placed in the
514 directory by third parties. Only regular files and directories are allowed
515 in the data directory, no symbolic links or special device files.
519 The way <productname>PostgreSQL</productname> manages tablespaces, the path
520 for all additional tablespaces must be identical whenever a backup is
521 restored. The main data directory, however, is relocatable to any location.
525 <application>pg_basebackup</application> works with servers of the same
526 or an older major version, down to 9.1. However, WAL streaming mode (-X
527 stream) only works with server version 9.3.
532 <title>Examples</title>
535 To create a base backup of the server at <literal>mydbserver</literal>
536 and store it in the local directory
537 <filename>/usr/local/pgsql/data</filename>:
539 <prompt>$</prompt> <userinput>pg_basebackup -h mydbserver -D /usr/local/pgsql/data</userinput>
544 To create a backup of the local server with one compressed
545 tar file for each tablespace, and store it in the directory
546 <filename>backup</filename>, showing a progress report while running:
548 <prompt>$</prompt> <userinput>pg_basebackup -D backup -Ft -z -P</userinput>
553 To create a backup of a single-tablespace local database and compress
554 this with <productname>bzip2</productname>:
556 <prompt>$</prompt> <userinput>pg_basebackup -D - -Ft | bzip2 > backup.tar.bz2</userinput>
558 (This command will fail if there are multiple tablespaces in the
564 <title>See Also</title>
566 <simplelist type="inline">
567 <member><xref linkend="APP-PGDUMP"></member>