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>--xlogdir=<replaceable class="parameter">xlogdir</replaceable></option></term>
209 Specifies the location for the transaction log directory.
210 <replaceable>xlogdir</replaceable> must be an absolute path.
211 The transaction log directory can only be specified when
212 the backup is in plain mode.
218 <term><option>-x</option></term>
219 <term><option>--xlog</option></term>
222 Using this option is equivalent of using <literal>-X</literal> with
223 method <literal>fetch</literal>.
229 <term><option>-X <replaceable class="parameter">method</replaceable></option></term>
230 <term><option>--xlog-method=<replaceable class="parameter">method</replaceable></option></term>
233 Includes the required transaction log files (WAL files) in the
234 backup. This will include all transaction logs generated during
235 the backup. If this option is specified, it is possible to start
236 a postmaster directly in the extracted directory without the need
237 to consult the log archive, thus making this a completely standalone
241 The following methods for collecting the transaction logs are
246 <term><literal>f</literal></term>
247 <term><literal>fetch</literal></term>
250 The transaction log files are collected at the end of the backup.
251 Therefore, it is necessary for the
252 <xref linkend="guc-wal-keep-segments"> parameter to be set high
253 enough that the log is not removed before the end of the backup.
254 If the log has been rotated when it's time to transfer it, the
255 backup will fail and be unusable.
261 <term><literal>s</literal></term>
262 <term><literal>stream</literal></term>
265 Stream the transaction log while the backup is created. This will
266 open a second connection to the server and start streaming the
267 transaction log in parallel while running the backup. Therefore,
268 it will use up two slots configured by the
269 <xref linkend="guc-max-wal-senders"> parameter. As long as the
270 client can keep up with transaction log received, using this mode
271 requires no extra transaction logs to be saved on the master.
281 <term><option>-z</option></term>
282 <term><option>--gzip</option></term>
285 Enables gzip compression of tar file output, with the default
286 compression level. Compression is only available when using
293 <term><option>-Z <replaceable class="parameter">level</replaceable></option></term>
294 <term><option>--compress=<replaceable class="parameter">level</replaceable></option></term>
297 Enables gzip compression of tar file output, and specifies the
298 compression level (1 through 9, 9 being best
299 compression). Compression is only available when using the tar
307 The following command-line options control the generation of the
308 backup and the running of the program.
312 <term><option>-c <replaceable class="parameter">fast|spread</replaceable></option></term>
313 <term><option>--checkpoint=<replaceable class="parameter">fast|spread</replaceable></option></term>
316 Sets checkpoint mode to fast or spread (default).
322 <term><option>-l <replaceable class="parameter">label</replaceable></option></term>
323 <term><option>--label=<replaceable class="parameter">label</replaceable></option></term>
326 Sets the label for the backup. If none is specified, a default value of
327 <quote><literal>pg_basebackup base backup</literal></quote> will be used.
333 <term><option>-P</option></term>
334 <term><option>--progress</option></term>
337 Enables progress reporting. Turning this on will deliver an approximate
338 progress report during the backup. Since the database may change during
339 the backup, this is only an approximation and may not end at exactly
340 <literal>100%</literal>. In particular, when WAL log is included in the
341 backup, the total amount of data cannot be estimated in advance, and
342 in this case the estimated target size will increase once it passes the
343 total estimate without WAL.
346 When this is enabled, the backup will start by enumerating the size of
347 the entire database, and then go back and send the actual contents.
348 This may make the backup take slightly longer, and in particular it
349 will take longer before the first data is sent.
355 <term><option>-v</option></term>
356 <term><option>--verbose</option></term>
359 Enables verbose mode. Will output some extra steps during startup and
360 shutdown, as well as show the exact file name that is currently being
361 processed if progress reporting is also enabled.
370 The following command-line options control the database connection parameters.
374 <term><option>-d <replaceable class="parameter">connstr</replaceable></option></term>
375 <term><option>--dbname=<replaceable class="parameter">connstr</replaceable></option></term>
378 Specifies parameters used to connect to the server, as a connection
379 string. See <xref linkend="libpq-connstring"> for more information.
382 The option is called <literal>--dbname</> for consistency with other
383 client applications, but because <application>pg_basebackup</application>
384 doesn't connect to any particular database in the cluster, database
385 name in the connection string will be ignored.
391 <term><option>-h <replaceable class="parameter">host</replaceable></option></term>
392 <term><option>--host=<replaceable class="parameter">host</replaceable></option></term>
395 Specifies the host name of the machine on which the server is
396 running. If the value begins with a slash, it is used as the
397 directory for the Unix domain socket. The default is taken
398 from the <envar>PGHOST</envar> environment variable, if set,
399 else a Unix domain socket connection is attempted.
405 <term><option>-p <replaceable class="parameter">port</replaceable></option></term>
406 <term><option>--port=<replaceable class="parameter">port</replaceable></option></term>
409 Specifies the TCP port or local Unix domain socket file
410 extension on which the server is listening for connections.
411 Defaults to the <envar>PGPORT</envar> environment variable, if
412 set, or a compiled-in default.
418 <term><option>-s <replaceable class="parameter">interval</replaceable></option></term>
419 <term><option>--status-interval=<replaceable class="parameter">interval</replaceable></option></term>
422 Specifies the number of seconds between status packets sent back to the
423 server. This allows for easier monitoring of the progress from server.
424 A value of zero disables the periodic status updates completely,
425 although an update will still be sent when requested by the server, to
426 avoid timeout disconnect. The default value is 10 seconds.
432 <term><option>-U <replaceable>username</replaceable></option></term>
433 <term><option>--username=<replaceable class="parameter">username</replaceable></option></term>
436 User name to connect as.
442 <term><option>-w</></term>
443 <term><option>--no-password</></term>
446 Never issue a password prompt. If the server requires
447 password authentication and a password is not available by
448 other means such as a <filename>.pgpass</filename> file, the
449 connection attempt will fail. This option can be useful in
450 batch jobs and scripts where no user is present to enter a
457 <term><option>-W</option></term>
458 <term><option>--password</option></term>
461 Force <application>pg_basebackup</application> to prompt for a
462 password before connecting to a database.
466 This option is never essential, since
467 <application>pg_basebackup</application> will automatically prompt
468 for a password if the server demands password authentication.
469 However, <application>pg_basebackup</application> will waste a
470 connection attempt finding out that the server wants a password.
471 In some cases it is worth typing <option>-W</> to avoid the extra
480 Other options are also available:
484 <term><option>-V</></term>
485 <term><option>--version</></term>
488 Print the <application>pg_basebackup</application> version and exit.
494 <term><option>-?</></term>
495 <term><option>--help</></term>
498 Show help about <application>pg_basebackup</application> command line
510 <title>Environment</title>
513 This utility, like most other <productname>PostgreSQL</> utilities,
514 uses the environment variables supported by <application>libpq</>
515 (see <xref linkend="libpq-envars">).
524 The backup will include all files in the data directory and tablespaces,
525 including the configuration files and any additional files placed in the
526 directory by third parties. Only regular files and directories are allowed
527 in the data directory, no symbolic links or special device files.
531 The way <productname>PostgreSQL</productname> manages tablespaces, the path
532 for all additional tablespaces must be identical whenever a backup is
533 restored. The main data directory, however, is relocatable to any location.
537 <application>pg_basebackup</application> works with servers of the same
538 or an older major version, down to 9.1. However, WAL streaming mode (-X
539 stream) only works with server version 9.3.
544 <title>Examples</title>
547 To create a base backup of the server at <literal>mydbserver</literal>
548 and store it in the local directory
549 <filename>/usr/local/pgsql/data</filename>:
551 <prompt>$</prompt> <userinput>pg_basebackup -h mydbserver -D /usr/local/pgsql/data</userinput>
556 To create a backup of the local server with one compressed
557 tar file for each tablespace, and store it in the directory
558 <filename>backup</filename>, showing a progress report while running:
560 <prompt>$</prompt> <userinput>pg_basebackup -D backup -Ft -z -P</userinput>
565 To create a backup of a single-tablespace local database and compress
566 this with <productname>bzip2</productname>:
568 <prompt>$</prompt> <userinput>pg_basebackup -D - -Ft | bzip2 > backup.tar.bz2</userinput>
570 (This command will fail if there are multiple tablespaces in the
576 <title>See Also</title>
578 <simplelist type="inline">
579 <member><xref linkend="APP-PGDUMP"></member>