]> begriffs open source - ai-pg/blob - full-docs/html/app-pgbasebackup.html
Include links to all subsection html pages, with shorter paths too
[ai-pg] / full-docs / html / app-pgbasebackup.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>pg_basebackup</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="app-pgamcheck.html" title="pg_amcheck" /><link rel="next" href="pgbench.html" title="pgbench" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center"><span class="application">pg_basebackup</span></th></tr><tr><td width="10%" align="left"><a accesskey="p" href="app-pgamcheck.html" title="pg_amcheck">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="reference-client.html" title="PostgreSQL Client Applications">Up</a></td><th width="60%" align="center">PostgreSQL Client Applications</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="pgbench.html" title="pgbench">Next</a></td></tr></table><hr /></div><div class="refentry" id="APP-PGBASEBACKUP"><div class="titlepage"></div><a id="id-1.9.4.10.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle"><span class="application">pg_basebackup</span></span></h2><p>pg_basebackup — take a base backup of a <span class="productname">PostgreSQL</span> cluster</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><div class="cmdsynopsis"><p id="id-1.9.4.10.4.1"><code class="command">pg_basebackup</code> [<em class="replaceable"><code>option</code></em>...]</p></div></div><div class="refsect1" id="id-1.9.4.10.5"><h2>Description</h2><p>
3    <span class="application">pg_basebackup</span> is used to take a base backup of
4    a running <span class="productname">PostgreSQL</span> database cluster. The backup
5    is taken without affecting other clients of the database, and can be used
6    both for point-in-time recovery (see <a class="xref" href="continuous-archiving.html" title="25.3. Continuous Archiving and Point-in-Time Recovery (PITR)">Section 25.3</a>)
7    and as the starting point for a log-shipping or streaming-replication standby
8    server (see <a class="xref" href="warm-standby.html" title="26.2. Log-Shipping Standby Servers">Section 26.2</a>).
9   </p><p>
10    <span class="application">pg_basebackup</span> can take a full or incremental
11    base backup of the database. When used to take a full backup, it makes an
12    exact copy of the database cluster's files. When used to take an incremental
13    backup, some files that would have been part of a full backup may be
14    replaced with incremental versions of the same files, containing only those
15    blocks that have been modified since the reference backup. An incremental
16    backup cannot be used directly; instead,
17    <a class="xref" href="app-pgcombinebackup.html" title="pg_combinebackup"><span class="refentrytitle"><span class="application">pg_combinebackup</span></span></a> must first
18    be used to combine it with the previous backups upon which it depends.
19    See <a class="xref" href="continuous-archiving.html#BACKUP-INCREMENTAL-BACKUP" title="25.3.3. Making an Incremental Backup">Section 25.3.3</a> for more information
20    about incremental backups, and <a class="xref" href="continuous-archiving.html#BACKUP-PITR-RECOVERY" title="25.3.5. Recovering Using a Continuous Archive Backup">Section 25.3.5</a>
21    for steps to recover from a backup.
22   </p><p>
23    In any mode, <span class="application">pg_basebackup</span> makes sure the server
24    is put into and out of backup mode automatically. Backups are always taken of
25    the entire database cluster; it is not possible to back up individual
26    databases or database objects. For selective backups, another tool such as
27    <a class="xref" href="app-pgdump.html" title="pg_dump"><span class="refentrytitle"><span class="application">pg_dump</span></span></a> must be used.
28   </p><p>
29    The backup is made over a regular <span class="productname">PostgreSQL</span>
30    connection that uses the replication protocol. The connection must be made
31    with a user ID that has <code class="literal">REPLICATION</code> permissions
32    (see <a class="xref" href="role-attributes.html" title="21.2. Role Attributes">Section 21.2</a>) or is a superuser,
33    and <a class="link" href="auth-pg-hba-conf.html" title="20.1. The pg_hba.conf File"><code class="filename">pg_hba.conf</code></a>
34    must permit the replication connection. The server must also be configured
35    with <a class="xref" href="runtime-config-replication.html#GUC-MAX-WAL-SENDERS">max_wal_senders</a> set high enough to provide at
36    least one walsender for the backup plus one for WAL streaming (if used).
37   </p><p>
38    There can be multiple <code class="command">pg_basebackup</code>s running at the same time, but it is usually
39    better from a performance point of view to take only one backup, and copy
40    the result.
41   </p><p>
42    <span class="application">pg_basebackup</span> can make a base backup from
43    not only a primary server but also a standby. To take a backup from a standby,
44    set up the standby so that it can accept replication connections (that is, set
45    <code class="varname">max_wal_senders</code> and <a class="xref" href="runtime-config-replication.html#GUC-HOT-STANDBY">hot_standby</a>,
46    and configure its <code class="filename">pg_hba.conf</code> appropriately).
47    You will also need to enable <a class="xref" href="runtime-config-wal.html#GUC-FULL-PAGE-WRITES">full_page_writes</a> on the primary.
48   </p><p>
49    Note that there are some limitations in taking a backup from a standby:
50
51    </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
52       The backup history file is not created in the database cluster backed up.
53      </p></li><li class="listitem"><p>
54       <span class="application">pg_basebackup</span> cannot force the standby
55       to switch to a new WAL file at the end of backup.
56       When you are using <code class="literal">-X none</code>, if write activity on
57       the primary is low, <span class="application">pg_basebackup</span> may
58       need to wait a long time for the last WAL file required for the backup
59       to be switched and archived.  In this case, it may be useful to run
60       <code class="function">pg_switch_wal</code> on the primary in order to
61       trigger an immediate WAL file switch.
62      </p></li><li class="listitem"><p>
63       If the standby is promoted to be primary during backup, the backup fails.
64      </p></li><li class="listitem"><p>
65       All WAL records required for the backup must contain sufficient full-page writes,
66       which requires you to enable <code class="varname">full_page_writes</code> on the primary.
67      </p></li></ul></div><p>
68   </p><p>
69    Whenever <span class="application">pg_basebackup</span> is taking a base
70    backup, the server's <code class="structname">pg_stat_progress_basebackup</code>
71    view will report the progress of the backup.
72    See <a class="xref" href="progress-reporting.html#BASEBACKUP-PROGRESS-REPORTING" title="27.4.6. Base Backup Progress Reporting">Section 27.4.6</a> for details.
73   </p></div><div class="refsect1" id="id-1.9.4.10.6"><h2>Options</h2><p>
74     The following command-line options control the location and format of the
75     output:
76
77     </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="option">-D <em class="replaceable"><code>directory</code></em></code><br /></span><span class="term"><code class="option">--pgdata=<em class="replaceable"><code>directory</code></em></code></span></dt><dd><p>
78         Sets the target directory to write the output to.
79         <span class="application">pg_basebackup</span> will create this directory
80         (and any missing parent directories) if it does not exist.  If it
81         already exists, it must be empty.
82        </p><p>
83         When the backup is in tar format, the target directory may be
84         specified as <code class="literal">-</code> (dash), causing the tar file to be
85         written to <code class="literal">stdout</code>.
86        </p><p>
87         This option is required.
88        </p></dd><dt><span class="term"><code class="option">-F <em class="replaceable"><code>format</code></em></code><br /></span><span class="term"><code class="option">--format=<em class="replaceable"><code>format</code></em></code></span></dt><dd><p>
89         Selects the format for the output. <em class="replaceable"><code>format</code></em>
90         can be one of the following:
91
92         </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">p</code><br /></span><span class="term"><code class="literal">plain</code></span></dt><dd><p>
93             Write the output as plain files, with the same layout as the
94             source server's data directory and tablespaces. When the cluster has
95             no additional tablespaces, the whole database will be placed in
96             the target directory. If the cluster contains additional
97             tablespaces, the main data directory will be placed in the
98             target directory, but all other tablespaces will be placed
99             in the same absolute path as they have on the source server.
100             (See <code class="option">--tablespace-mapping</code> to change that.)
101            </p><p>
102             This is the default format.
103            </p></dd><dt><span class="term"><code class="literal">t</code><br /></span><span class="term"><code class="literal">tar</code></span></dt><dd><p>
104             Write the output as tar files in the target directory. The main
105             data directory's contents will be written to a file named
106             <code class="filename">base.tar</code>, and each other tablespace will be
107             written to a separate tar file named after that tablespace's OID.
108            </p><p>
109             If the target directory is specified as <code class="literal">-</code>
110             (dash), the tar contents will be written to
111             standard output, suitable for piping to (for example)
112             <span class="productname">gzip</span>. This is only allowed if
113             the cluster has no additional tablespaces and WAL
114             streaming is not used.
115            </p></dd></dl></div></dd><dt><span class="term"><code class="option">-i <em class="replaceable"><code>old_manifest_file</code></em></code><br /></span><span class="term"><code class="option">--incremental=<em class="replaceable"><code>old_manifest_file</code></em></code></span></dt><dd><p>
116         Performs an <a class="link" href="continuous-archiving.html#BACKUP-INCREMENTAL-BACKUP" title="25.3.3. Making an Incremental Backup">incremental
117         backup</a>. The backup manifest for the reference
118         backup must be provided, and will be uploaded to the server, which will
119         respond by sending the requested incremental backup.
120        </p></dd><dt><span class="term"><code class="option">-R</code><br /></span><span class="term"><code class="option">--write-recovery-conf</code></span></dt><dd><p>
121         Creates a
122         <a class="link" href="warm-standby.html#FILE-STANDBY-SIGNAL"><code class="filename">standby.signal</code></a>
123         <a id="id-1.9.4.10.6.2.1.4.3.1.2" class="indexterm"></a>
124         file and appends
125         connection settings to the <code class="filename">postgresql.auto.conf</code>
126         file in the target directory (or within the base archive file when
127         using tar format).  This eases setting up a standby server using the
128         results of the backup.
129        </p><p>
130         The <code class="filename">postgresql.auto.conf</code> file will record the connection
131         settings and, if specified, the replication slot
132         that <span class="application">pg_basebackup</span> is using, so that
133         streaming replication and <a class="link" href="logicaldecoding-explanation.html#LOGICALDECODING-REPLICATION-SLOTS-SYNCHRONIZATION" title="47.2.3. Replication Slot Synchronization">
134         logical replication slot synchronization</a> will use the same
135         settings later on. The dbname will be recorded only if the dbname was
136         specified explicitly in the connection string or <a class="link" href="libpq-envars.html" title="32.15. Environment Variables">
137         environment variable</a>.
138        </p></dd><dt><span class="term"><code class="option">-t <em class="replaceable"><code>target</code></em></code><br /></span><span class="term"><code class="option">--target=<em class="replaceable"><code>target</code></em></code></span></dt><dd><p>
139         Instructs the server where to place the base backup. The default target
140         is <code class="literal">client</code>, which specifies that the backup should
141         be sent to the machine where <span class="application">pg_basebackup</span>
142         is running. If the target is instead set to
143         <code class="literal">server:/some/path</code>, the backup will be stored on
144         the machine where the server is running in the
145         <code class="literal">/some/path</code> directory. Storing a backup on the
146         server requires superuser privileges or having privileges of the
147         <code class="literal">pg_write_server_files</code> role. If the target is set to
148         <code class="literal">blackhole</code>, the contents are discarded and not
149         stored anywhere. This should only be used for testing purposes, as you
150         will not end up with an actual backup.
151        </p><p>
152         Since WAL streaming is implemented by
153         <span class="application">pg_basebackup</span> rather than by the server,
154         this option cannot be used together with <code class="literal">-Xstream</code>.
155         Since that is the default, when this option is specified, you must also
156         specify either <code class="literal">-Xfetch</code> or <code class="literal">-Xnone</code>.
157        </p></dd><dt><span class="term"><code class="option">-T <em class="replaceable"><code>olddir</code></em>=<em class="replaceable"><code>newdir</code></em></code><br /></span><span class="term"><code class="option">--tablespace-mapping=<em class="replaceable"><code>olddir</code></em>=<em class="replaceable"><code>newdir</code></em></code></span></dt><dd><p>
158         Relocates the tablespace in directory <em class="replaceable"><code>olddir</code></em>
159         to <em class="replaceable"><code>newdir</code></em> during the backup.  To be
160         effective, <em class="replaceable"><code>olddir</code></em> must exactly match the
161         path specification of the tablespace as it is defined on the source
162         server.  (But it is not an error if there is no tablespace
163         in <em class="replaceable"><code>olddir</code></em> on the source server.)
164         Meanwhile <em class="replaceable"><code>newdir</code></em> is a directory in the
165         receiving host's filesystem.  As with the main target directory,
166         <em class="replaceable"><code>newdir</code></em> need not exist already, but if
167         it does exist it must be empty.
168         Both <em class="replaceable"><code>olddir</code></em>
169         and <em class="replaceable"><code>newdir</code></em> must be absolute paths.  If
170         either path needs to contain an equal sign (<code class="literal">=</code>),
171         precede that with a backslash.  This option can be specified multiple
172         times for multiple tablespaces.
173        </p><p>
174         If a tablespace is relocated in this way, the symbolic links inside
175         the main data directory are updated to point to the new location.  So
176         the new data directory is ready to be used for a new server instance
177         with all tablespaces in the updated locations.
178        </p><p>
179         Currently, this option only works with plain output format; it is
180         ignored if tar format is selected.
181        </p></dd><dt><span class="term"><code class="option">--waldir=<em class="replaceable"><code>waldir</code></em></code></span></dt><dd><p>
182         Sets the directory to write WAL (write-ahead log) files to.
183         By default WAL files will be placed in
184         the <code class="filename">pg_wal</code> subdirectory of the target
185         directory, but this option can be used to place them elsewhere.
186         <em class="replaceable"><code>waldir</code></em> must be an absolute path.
187         As with the main target directory,
188         <em class="replaceable"><code>waldir</code></em> need not exist already, but if
189         it does exist it must be empty.
190         This option can only be specified when
191         the backup is in plain format.
192        </p></dd><dt><span class="term"><code class="option">-X <em class="replaceable"><code>method</code></em></code><br /></span><span class="term"><code class="option">--wal-method=<em class="replaceable"><code>method</code></em></code></span></dt><dd><p>
193         Includes the required WAL (write-ahead log) files in the
194         backup. This will include all write-ahead logs generated during
195         the backup. Unless the method <code class="literal">none</code> is specified,
196         it is possible to start a postmaster in the target
197         directory without the need to consult the WAL archive, thus
198         making the output a completely standalone backup.
199        </p><p>
200         The following <em class="replaceable"><code>method</code></em>s for collecting the
201         write-ahead logs are supported:
202
203         </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">n</code><br /></span><span class="term"><code class="literal">none</code></span></dt><dd><p>
204             Don't include write-ahead logs in the backup.
205            </p></dd><dt><span class="term"><code class="literal">f</code><br /></span><span class="term"><code class="literal">fetch</code></span></dt><dd><p>
206             The write-ahead log files are collected at the end of the backup.
207             Therefore, it is necessary for the source server's
208             <a class="xref" href="runtime-config-replication.html#GUC-WAL-KEEP-SIZE">wal_keep_size</a> parameter to be set high
209             enough that the required log data is not removed before the end
210             of the backup.  If the required log data has been recycled
211             before it's time to transfer it, the backup will fail and be
212             unusable.
213            </p><p>
214             When tar format is used, the write-ahead log files will be
215             included in the <code class="filename">base.tar</code> file.
216            </p></dd><dt><span class="term"><code class="literal">s</code><br /></span><span class="term"><code class="literal">stream</code></span></dt><dd><p>
217             Stream write-ahead log data while the backup is being taken.
218             This method will open a second connection to the server and
219             start streaming the write-ahead log in parallel while running
220             the backup.  Therefore, it will require two replication
221             connections not just one.  As long as the client can keep up
222             with the write-ahead log data, using this method requires no
223             extra write-ahead logs to be saved on the source server.
224            </p><p>
225             When tar format is used, the write-ahead log files will be
226             written to a separate file named <code class="filename">pg_wal.tar</code>
227             (if the server is a version earlier than 10, the file will be named
228             <code class="filename">pg_xlog.tar</code>).
229            </p><p>
230             This value is the default.
231            </p></dd></dl></div></dd><dt><span class="term"><code class="option">-z</code><br /></span><span class="term"><code class="option">--gzip</code></span></dt><dd><p>
232         Enables gzip compression of tar file output, with the default
233         compression level. Compression is only available when using
234         the tar format, and the suffix <code class="filename">.gz</code> will
235         automatically be added to all tar filenames.
236        </p></dd><dt><span class="term"><code class="option">-Z <em class="replaceable"><code>level</code></em></code><br /></span><span class="term"><code class="option">-Z [{client|server}-]<em class="replaceable"><code>method</code></em>[:<em class="replaceable"><code>detail</code></em>]</code><br /></span><span class="term"><code class="option">--compress=<em class="replaceable"><code>level</code></em></code><br /></span><span class="term"><code class="option">--compress=[{client|server}-]<em class="replaceable"><code>method</code></em>[:<em class="replaceable"><code>detail</code></em>]</code></span></dt><dd><p>
237         Requests compression of the backup. If <code class="literal">client</code> or
238         <code class="literal">server</code> is included, it specifies where the
239         compression is to be performed. Compressing on the server will reduce
240         transfer bandwidth but will increase server CPU consumption.  The
241         default is <code class="literal">client</code> except when
242         <code class="literal">--target</code> is used. In that case, the backup is not
243         being sent to the client, so only server compression is sensible.
244         When <code class="literal">-Xstream</code>, which is the default, is used,
245         server-side compression will not be applied to the WAL. To compress
246         the WAL, use client-side compression, or
247         specify <code class="literal">-Xfetch</code>.
248        </p><p>
249         The compression method can be set to <code class="literal">gzip</code>,
250         <code class="literal">lz4</code>, <code class="literal">zstd</code>,
251         <code class="literal">none</code> for no compression or an integer (no
252         compression if 0, <code class="literal">gzip</code> if greater than 0).
253         A compression detail string can optionally be specified.
254         If the detail string is an integer, it specifies the compression
255         level.  Otherwise, it should be a comma-separated list of items,
256         each of the form
257         <em class="replaceable"><code>keyword</code></em> or
258         <em class="replaceable"><code>keyword=value</code></em>.
259         Currently, the supported keywords are <code class="literal">level</code>,
260         <code class="literal">long</code>, and <code class="literal">workers</code>.
261         The detail string cannot be used when the compression method
262         is specified as a plain integer.
263        </p><p>
264         If no compression level is specified, the default compression level
265         will be used. If only a level is specified without mentioning an
266         algorithm, <code class="literal">gzip</code> compression will be used if the
267         level is greater than 0, and no compression will be used if the level
268         is 0.
269        </p><p>
270         When the tar format is used with <code class="literal">gzip</code>,
271         <code class="literal">lz4</code>, or <code class="literal">zstd</code>, the suffix
272         <code class="filename">.gz</code>, <code class="filename">.lz4</code>, or
273         <code class="filename">.zst</code>, respectively, will be automatically added to
274         all tar filenames. When the plain format is used, client-side
275         compression may not be specified, but it is still possible to request
276         server-side compression. If this is done, the server will compress the
277         backup for transmission, and the client will decompress and extract it.
278        </p><p>
279         When this option is used in combination with
280         <code class="literal">-Xstream</code>, <code class="literal">pg_wal.tar</code> will
281         be compressed using <code class="literal">gzip</code> if client-side gzip
282         compression is selected, but will not be compressed if any other
283         compression algorithm is selected, or if server-side compression
284         is selected.
285        </p></dd></dl></div><p>
286    </p><p>
287     The following command-line options control the generation of the
288     backup and the invocation of the program:
289
290     </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="option">-c {fast|spread}</code><br /></span><span class="term"><code class="option">--checkpoint={fast|spread}</code></span></dt><dd><p>
291         Sets checkpoint mode to fast (immediate) or spread (the default)
292         (see <a class="xref" href="continuous-archiving.html#BACKUP-LOWLEVEL-BASE-BACKUP" title="25.3.4. Making a Base Backup Using the Low Level API">Section 25.3.4</a>).
293        </p></dd><dt><span class="term"><code class="option">-C</code><br /></span><span class="term"><code class="option">--create-slot</code></span></dt><dd><p>
294         Specifies that the replication slot named by the
295         <code class="literal">--slot</code> option should be created before starting
296         the backup.  An error is raised if the slot already exists.
297        </p></dd><dt><span class="term"><code class="option">-l <em class="replaceable"><code>label</code></em></code><br /></span><span class="term"><code class="option">--label=<em class="replaceable"><code>label</code></em></code></span></dt><dd><p>
298         Sets the label for the backup. If none is specified, a default value of
299         <span class="quote">“<span class="quote"><code class="literal">pg_basebackup base backup</code></span>”</span> will be used.
300        </p></dd><dt><span class="term"><code class="option">-n</code><br /></span><span class="term"><code class="option">--no-clean</code></span></dt><dd><p>
301         By default, when <code class="command">pg_basebackup</code> aborts with an
302         error, it removes any directories it might have created before
303         discovering that it cannot finish the job (for example, the target
304         directory and write-ahead log directory). This option inhibits
305         tidying-up and is thus useful for debugging.
306        </p><p>
307         Note that tablespace directories are not cleaned up either way.
308        </p></dd><dt><span class="term"><code class="option">-N</code><br /></span><span class="term"><code class="option">--no-sync</code></span></dt><dd><p>
309         By default, <code class="command">pg_basebackup</code> will wait for all files
310         to be written safely to disk.  This option causes
311         <code class="command">pg_basebackup</code> to return without waiting, which is
312         faster, but means that a subsequent operating system crash can leave
313         the base backup corrupt.  Generally, this option is useful for testing
314         but should not be used when creating a production installation.
315        </p></dd><dt><span class="term"><code class="option">-P</code><br /></span><span class="term"><code class="option">--progress</code></span></dt><dd><p>
316         Enables progress reporting. Turning this on will deliver an approximate
317         progress report during the backup. Since the database may change during
318         the backup, this is only an approximation and may not end at exactly
319         <code class="literal">100%</code>. In particular, when WAL log is included in the
320         backup, the total amount of data cannot be estimated in advance, and
321         in this case the estimated target size will increase once it passes the
322         total estimate without WAL.
323        </p></dd><dt><span class="term"><code class="option">-r <em class="replaceable"><code>rate</code></em></code><br /></span><span class="term"><code class="option">--max-rate=<em class="replaceable"><code>rate</code></em></code></span></dt><dd><p>
324         Sets the maximum transfer rate at which data is collected from the
325         source server.  This can be useful to limit the impact
326         of <span class="application">pg_basebackup</span> on the server.  Values
327         are in kilobytes per second.  Use a suffix of <code class="literal">M</code>
328         to indicate megabytes per second.  A suffix of <code class="literal">k</code>
329         is also accepted, and has no effect.  Valid values are between 32
330         kilobytes per second and 1024 megabytes per second.
331        </p><p>
332         This option always affects transfer of the data directory. Transfer of
333         WAL files is only affected if the collection method
334         is <code class="literal">fetch</code>.
335        </p></dd><dt><span class="term"><code class="option">-S <em class="replaceable"><code>slotname</code></em></code><br /></span><span class="term"><code class="option">--slot=<em class="replaceable"><code>slotname</code></em></code></span></dt><dd><p>
336         This option can only be used together with <code class="literal">-X
337         stream</code>.  It causes WAL streaming to use the specified
338         replication slot.  If the base backup is intended to be used as a
339         streaming-replication standby using a replication slot, the standby
340         should then use the same replication slot name as
341         <a class="xref" href="runtime-config-replication.html#GUC-PRIMARY-SLOT-NAME">primary_slot_name</a>.  This ensures that the
342         primary server does not remove any necessary WAL data in the time
343         between the end of the base backup and the start of streaming
344         replication on the new standby.
345        </p><p>
346         The specified replication slot has to exist unless the
347         option <code class="option">-C</code> is also used.
348        </p><p>
349         If this option is not specified and the server supports temporary
350         replication slots (version 10 and later), then a temporary replication
351         slot is automatically used for WAL streaming.
352        </p></dd><dt><span class="term"><code class="option">--sync-method=<em class="replaceable"><code>method</code></em></code></span></dt><dd><p>
353         When set to <code class="literal">fsync</code>, which is the default,
354         <code class="command">pg_basebackup</code> will recursively open and synchronize
355         all files in the backup directory.  When the plain format is used, the
356         search for files will follow symbolic links for the WAL directory and
357         each configured tablespace.
358        </p><p>
359         On Linux, <code class="literal">syncfs</code> may be used instead to ask the
360         operating system to synchronize the whole file system that contains the
361         backup directory.  When the plain format is used,
362         <code class="command">pg_basebackup</code> will also synchronize the file systems
363         that contain the WAL files and each tablespace.  See
364         <a class="xref" href="runtime-config-error-handling.html#GUC-RECOVERY-INIT-SYNC-METHOD">recovery_init_sync_method</a> for information about
365         the caveats to be aware of when using <code class="literal">syncfs</code>.
366        </p><p>
367         This option has no effect when <code class="option">--no-sync</code> is used.
368        </p></dd><dt><span class="term"><code class="option">-v</code><br /></span><span class="term"><code class="option">--verbose</code></span></dt><dd><p>
369         Enables verbose mode. Will output some extra steps during startup and
370         shutdown, as well as show the exact file name that is currently being
371         processed if progress reporting is also enabled.
372        </p></dd><dt><span class="term"><code class="option">--manifest-checksums=<em class="replaceable"><code>algorithm</code></em></code></span></dt><dd><p>
373         Specifies the checksum algorithm that should be applied to each file
374         included in the backup manifest. Currently, the available
375         algorithms are <code class="literal">NONE</code>, <code class="literal">CRC32C</code>,
376         <code class="literal">SHA224</code>, <code class="literal">SHA256</code>,
377         <code class="literal">SHA384</code>, and <code class="literal">SHA512</code>.
378         The default is <code class="literal">CRC32C</code>.
379        </p><p>
380         If <code class="literal">NONE</code> is selected, the backup manifest will
381         not contain any checksums. Otherwise, it will contain a checksum
382         of each file in the backup using the specified algorithm. In addition,
383         the manifest will always contain a <code class="literal">SHA256</code>
384         checksum of its own contents. The <code class="literal">SHA</code> algorithms
385         are significantly more CPU-intensive than <code class="literal">CRC32C</code>,
386         so selecting one of them may increase the time required to complete
387         the backup.
388        </p><p>
389         Using a SHA hash function provides a cryptographically secure digest
390         of each file for users who wish to verify that the backup has not been
391         tampered with, while the CRC-32C algorithm provides a checksum that is
392         much faster to calculate; it is good at catching errors due to accidental
393         changes but is not resistant to malicious modifications.  Note that, to
394         be useful against an adversary who has access to the backup, the backup
395         manifest would need to be stored securely elsewhere or otherwise
396         verified not to have been modified since the backup was taken.
397        </p><p>
398         <a class="xref" href="app-pgverifybackup.html" title="pg_verifybackup"><span class="refentrytitle"><span class="application">pg_verifybackup</span></span></a> can be used to check the
399         integrity of a backup against the backup manifest.
400        </p></dd><dt><span class="term"><code class="option">--manifest-force-encode</code></span></dt><dd><p>
401         Forces all filenames in the backup manifest to be hex-encoded.
402         If this option is not specified, only non-UTF8 filenames are
403         hex-encoded. This option is mostly intended to test that tools which
404         read a backup manifest file properly handle this case.
405        </p></dd><dt><span class="term"><code class="option">--no-estimate-size</code></span></dt><dd><p>
406         Prevents the server from estimating the total
407         amount of backup data that will be streamed, resulting in the
408         <code class="structfield">backup_total</code> column in the
409         <code class="structname">pg_stat_progress_basebackup</code> view
410         always being <code class="literal">NULL</code>.
411        </p><p>
412         Without this option, the backup will start by enumerating
413         the size of the entire database, and then go back and send
414         the actual contents. This may make the backup take slightly
415         longer, and in particular it will take longer before the first
416         data is sent. This option is useful to avoid such estimation
417         time if it's too long.
418        </p><p>
419         This option is not allowed when using <code class="option">--progress</code>.
420        </p></dd><dt><span class="term"><code class="option">--no-manifest</code></span></dt><dd><p>
421         Disables generation of a backup manifest. If this option is not
422         specified, the server will generate and send a backup manifest
423         which can be verified using <a class="xref" href="app-pgverifybackup.html" title="pg_verifybackup"><span class="refentrytitle"><span class="application">pg_verifybackup</span></span></a>.
424         The manifest is a list of every file present in the backup with the
425         exception of any WAL files that may be included. It also stores the
426         size, last modification time, and an optional checksum for each file.
427        </p></dd><dt><span class="term"><code class="option">--no-slot</code></span></dt><dd><p>
428         Prevents the creation of a temporary replication slot
429         for the backup.
430        </p><p>
431         By default, if log streaming is selected but no slot name is given
432         with the <code class="option">-S</code> option, then a temporary replication
433         slot is created (if supported by the source server).
434        </p><p>
435         The main purpose of this option is to allow taking a base backup when
436         the server has no free replication slots.  Using a replication slot
437         is almost always preferred, because it prevents needed WAL from being
438         removed by the server during the backup.
439        </p></dd><dt><span class="term"><code class="option">--no-verify-checksums</code></span></dt><dd><p>
440         Disables verification of checksums, if they are enabled on the server
441         the base backup is taken from.
442        </p><p>
443         By default, checksums are verified and checksum failures will result
444         in a non-zero exit status. However, the base backup will not be
445         removed in such a case, as if the <code class="option">--no-clean</code> option
446         had been used.  Checksum verification failures will also be reported
447         in the <a class="link" href="monitoring-stats.html#MONITORING-PG-STAT-DATABASE-VIEW" title="27.2.17. pg_stat_database">
448         <code class="structname">pg_stat_database</code></a> view.
449        </p></dd></dl></div><p>
450    </p><p>
451     The following command-line options control the connection to the source
452     server:
453
454     </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="option">-d <em class="replaceable"><code>connstr</code></em></code><br /></span><span class="term"><code class="option">--dbname=<em class="replaceable"><code>connstr</code></em></code></span></dt><dd><p>
455         Specifies parameters used to connect to the server, as a <a class="link" href="libpq-connect.html#LIBPQ-CONNSTRING" title="32.1.1. Connection Strings">connection string</a>;  these
456         will override any conflicting command line options.
457        </p><p>
458         This option is called <code class="literal">--dbname</code> for consistency with other
459         client applications, but because <span class="application">pg_basebackup</span>
460         doesn't connect to any particular database in the cluster, any database
461         name included in the connection string will be ignored by the server.
462         However, a database name supplied that way overrides the default
463         database name (<code class="literal">replication</code>) for purposes of
464         looking up the replication connection's password
465         in <code class="filename">~/.pgpass</code>.  Similarly, middleware or proxies
466         used in connecting to <span class="productname">PostgreSQL</span> might
467         utilize the name for purposes such as connection routing.  The
468         database name can also be used
469         by <a class="link" href="logicaldecoding-explanation.html#LOGICALDECODING-REPLICATION-SLOTS-SYNCHRONIZATION" title="47.2.3. Replication Slot Synchronization">
470         logical replication slot synchronization</a>.
471        </p></dd><dt><span class="term"><code class="option">-h <em class="replaceable"><code>host</code></em></code><br /></span><span class="term"><code class="option">--host=<em class="replaceable"><code>host</code></em></code></span></dt><dd><p>
472         Specifies the host name of the machine on which the server is
473         running.  If the value begins with a slash, it is used as the
474         directory for a Unix domain socket. The default is taken
475         from the <code class="envar">PGHOST</code> environment variable, if set,
476         else a Unix domain socket connection is attempted.
477        </p></dd><dt><span class="term"><code class="option">-p <em class="replaceable"><code>port</code></em></code><br /></span><span class="term"><code class="option">--port=<em class="replaceable"><code>port</code></em></code></span></dt><dd><p>
478         Specifies the TCP port or local Unix domain socket file
479         extension on which the server is listening for connections.
480         Defaults to the <code class="envar">PGPORT</code> environment variable, if
481         set, or a compiled-in default.
482        </p></dd><dt><span class="term"><code class="option">-s <em class="replaceable"><code>interval</code></em></code><br /></span><span class="term"><code class="option">--status-interval=<em class="replaceable"><code>interval</code></em></code></span></dt><dd><p>
483         Specifies the number of seconds between status packets sent back to
484         the source server. Smaller values allow more accurate monitoring of
485         backup progress from the server.
486         A value of zero disables periodic status updates completely,
487         although an update will still be sent when requested by the server, to
488         avoid timeout-based disconnects. The default value is 10 seconds.
489        </p></dd><dt><span class="term"><code class="option">-U <em class="replaceable"><code>username</code></em></code><br /></span><span class="term"><code class="option">--username=<em class="replaceable"><code>username</code></em></code></span></dt><dd><p>
490         Specifies the user name to connect as.
491        </p></dd><dt><span class="term"><code class="option">-w</code><br /></span><span class="term"><code class="option">--no-password</code></span></dt><dd><p>
492         Prevents issuing a password prompt.  If the server requires
493         password authentication and a password is not available by
494         other means such as a <code class="filename">.pgpass</code> file, the
495         connection attempt will fail.  This option can be useful in
496         batch jobs and scripts where no user is present to enter a
497         password.
498        </p></dd><dt><span class="term"><code class="option">-W</code><br /></span><span class="term"><code class="option">--password</code></span></dt><dd><p>
499         Forces <span class="application">pg_basebackup</span> to prompt for a
500         password before connecting to the source server.
501        </p><p>
502         This option is never essential, since
503         <span class="application">pg_basebackup</span> will automatically prompt
504         for a password if the server demands password authentication.
505         However, <span class="application">pg_basebackup</span> will waste a
506         connection attempt finding out that the server wants a password.
507         In some cases it is worth typing <code class="option">-W</code> to avoid the extra
508         connection attempt.
509        </p></dd></dl></div><p>
510    </p><p>
511     Other options are also available:
512
513     </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="option">-V</code><br /></span><span class="term"><code class="option">--version</code></span></dt><dd><p>
514        Prints the <span class="application">pg_basebackup</span> version and exits.
515        </p></dd><dt><span class="term"><code class="option">-?</code><br /></span><span class="term"><code class="option">--help</code></span></dt><dd><p>
516        Shows help about <span class="application">pg_basebackup</span> command line
517        arguments, and exits.
518        </p></dd></dl></div><p>
519    </p></div><div class="refsect1" id="id-1.9.4.10.7"><h2>Environment</h2><p>
520    This utility, like most other <span class="productname">PostgreSQL</span> utilities,
521    uses the environment variables supported by <span class="application">libpq</span>
522    (see <a class="xref" href="libpq-envars.html" title="32.15. Environment Variables">Section 32.15</a>).
523   </p><p>
524    The environment variable <code class="envar">PG_COLOR</code> specifies whether to use
525    color in diagnostic messages. Possible values are
526    <code class="literal">always</code>, <code class="literal">auto</code> and
527    <code class="literal">never</code>.
528   </p></div><div class="refsect1" id="id-1.9.4.10.8"><h2>Notes</h2><p>
529    At the beginning of the backup, a checkpoint needs to be performed on the
530    source server.  This can take some time (especially if the option
531    <code class="literal">--checkpoint=fast</code> is not used), during
532    which <span class="application">pg_basebackup</span> will appear to be idle.
533   </p><p>
534    The backup will include all files in the data directory and tablespaces,
535    including the configuration files and any additional files placed in the
536    directory by third parties, except certain temporary files managed by
537    PostgreSQL and operating system files.  But only regular files and
538    directories are copied, except that
539    symbolic links used for tablespaces are preserved.  Symbolic links pointing
540    to certain directories known to PostgreSQL are copied as empty directories.
541    Other symbolic links and special device files are skipped.
542    See <a class="xref" href="protocol-replication.html" title="54.4. Streaming Replication Protocol">Section 54.4</a> for the precise details.
543   </p><p>
544    In plain format, tablespaces will be backed up to the same path
545    they have on the source server, unless the
546    option <code class="literal">--tablespace-mapping</code> is used.  Without
547    this option, running a plain format base backup on the same host as the
548    server will not work if tablespaces are in use, because the backup would
549    have to be written to the same directory locations as the original
550    tablespaces.
551   </p><p>
552    When tar format is used, it is the user's responsibility to unpack each
553    tar file before starting a PostgreSQL server that uses the data. If there
554    are additional tablespaces, the
555    tar files for them need to be unpacked in the correct locations. In this
556    case the symbolic links for those tablespaces will be created by the server
557    according to the contents of the <code class="filename">tablespace_map</code> file that is
558    included in the <code class="filename">base.tar</code> file.
559   </p><p>
560    <span class="application">pg_basebackup</span> works with servers of the same
561    or older major version, down to 9.1. However, WAL streaming mode (<code class="literal">-X
562    stream</code>) only works with server version 9.3 and later, the tar format
563    (<code class="literal">--format=tar</code>) only works with server version 9.5
564    and later, and incremental backup (<code class="literal">--incremental</code>) only works
565    with server version 17 and later.
566   </p><p>
567    <span class="application">pg_basebackup</span> will preserve group permissions
568    for data files if group permissions are enabled on the source cluster.
569   </p></div><div class="refsect1" id="id-1.9.4.10.9"><h2>Examples</h2><p>
570    To create a base backup of the server at <code class="literal">mydbserver</code>
571    and store it in the local directory
572    <code class="filename">/usr/local/pgsql/data</code>:
573 </p><pre class="screen">
574 <code class="prompt">$</code> <strong class="userinput"><code>pg_basebackup -h mydbserver -D /usr/local/pgsql/data</code></strong>
575 </pre><p>
576   </p><p>
577    To create a backup of the local server with one compressed
578    tar file for each tablespace, and store it in the directory
579    <code class="filename">backup</code>, showing a progress report while running:
580 </p><pre class="screen">
581 <code class="prompt">$</code> <strong class="userinput"><code>pg_basebackup -D backup -Ft -z -P</code></strong>
582 </pre><p>
583   </p><p>
584    To create a backup of a single-tablespace local database and compress
585    this with <span class="productname">bzip2</span>:
586 </p><pre class="screen">
587 <code class="prompt">$</code> <strong class="userinput"><code>pg_basebackup -D - -Ft -X fetch | bzip2 &gt; backup.tar.bz2</code></strong>
588 </pre><p>
589    (This command will fail if there are multiple tablespaces in the
590    database.)
591   </p><p>
592    To create a backup of a local database where the tablespace in
593    <code class="filename">/opt/ts</code> is relocated
594    to <code class="filename">./backup/ts</code>:
595 </p><pre class="screen">
596 <code class="prompt">$</code> <strong class="userinput"><code>pg_basebackup -D backup/data -T /opt/ts=$(pwd)/backup/ts</code></strong>
597 </pre><p>
598    To create a backup of the local server with one tar file for each tablespace
599    compressed with <span class="application">gzip</span> at level 9, stored in the
600    directory <code class="filename">backup</code>:
601 </p><pre class="screen">
602 <code class="prompt">$</code> <strong class="userinput"><code>pg_basebackup -D backup -Ft --compress=gzip:9</code></strong>
603 </pre></div><div class="refsect1" id="id-1.9.4.10.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="app-pgdump.html" title="pg_dump"><span class="refentrytitle"><span class="application">pg_dump</span></span></a>, <a class="xref" href="progress-reporting.html#BASEBACKUP-PROGRESS-REPORTING" title="27.4.6. Base Backup Progress Reporting">Section 27.4.6</a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="app-pgamcheck.html" title="pg_amcheck">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="reference-client.html" title="PostgreSQL Client Applications">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="pgbench.html" title="pgbench">Next</a></td></tr><tr><td width="40%" align="left" valign="top"><span class="application">pg_amcheck</span> </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> <span class="application">pgbench</span></td></tr></table></div></body></html>