1PG_BASEBACKUP(1) PostgreSQL 10.7 Documentation PG_BASEBACKUP(1)
2
3
4
6 pg_basebackup - take a base backup of a PostgreSQL cluster
7
9 pg_basebackup [option...]
10
12 pg_basebackup is used to take base backups of a running PostgreSQL
13 database cluster. These are taken without affecting other clients to
14 the database, and can be used both for point-in-time recovery (see
15 Section 25.3) and as the starting point for a log shipping or streaming
16 replication standby servers (see Section 26.2).
17
18 pg_basebackup makes a binary copy of the database cluster files, while
19 making sure the system is put in and out of backup mode automatically.
20 Backups are always taken of the entire database cluster; it is not
21 possible to back up individual databases or database objects. For
22 individual database backups, a tool such as pg_dump(1) must be used.
23
24 The backup is made over a regular PostgreSQL connection, and uses the
25 replication protocol. The connection must be made with a superuser or a
26 user having REPLICATION permissions (see Section 21.2), and pg_hba.conf
27 must explicitly permit the replication connection. The server must also
28 be configured with max_wal_senders set high enough to leave at least
29 one session available for the backup and one for WAL streaming (if
30 used).
31
32 There can be multiple pg_basebackups running at the same time, but it
33 is better from a performance point of view to take only one backup, and
34 copy the result.
35
36 pg_basebackup can make a base backup from not only the master but also
37 the standby. To take a backup from the standby, set up the standby so
38 that it can accept replication connections (that is, set
39 max_wal_senders and hot_standby, and configure host-based
40 authentication). You will also need to enable full_page_writes on the
41 master.
42
43 Note that there are some limitations in an online backup from the
44 standby:
45
46 · The backup history file is not created in the database cluster
47 backed up.
48
49 · If you are using -X none, there is no guarantee that all WAL files
50 required for the backup are archived at the end of backup.
51
52 · If the standby is promoted to the master during online backup, the
53 backup fails.
54
55 · All WAL records required for the backup must contain sufficient
56 full-page writes, which requires you to enable full_page_writes on
57 the master and not to use a tool like pg_compresslog as
58 archive_command to remove full-page writes from WAL files.
59
60
62 The following command-line options control the location and format of
63 the output.
64
65 -D directory
66 --pgdata=directory
67 Directory to write the output to. pg_basebackup will create the
68 directory and any parent directories if necessary. The directory
69 may already exist, but it is an error if the directory already
70 exists and is not empty.
71
72 When the backup is in tar mode, and the directory is specified as -
73 (dash), the tar file will be written to stdout.
74
75 This option is required.
76
77 -F format
78 --format=format
79 Selects the format for the output. format can be one of the
80 following:
81
82 p
83 plain
84 Write the output as plain files, with the same layout as the
85 current data directory and tablespaces. When the cluster has no
86 additional tablespaces, the whole database will be placed in
87 the target directory. If the cluster contains additional
88 tablespaces, the main data directory will be placed in the
89 target directory, but all other tablespaces will be placed in
90 the same absolute path as they have on the server.
91
92 This is the default format.
93
94 t
95 tar
96 Write the output as tar files in the target directory. The main
97 data directory will be written to a file named base.tar, and
98 all other tablespaces will be named after the tablespace OID.
99
100 If the value - (dash) is specified as target directory, the tar
101 contents will be written to standard output, suitable for
102 piping to for example gzip. This is only possible if the
103 cluster has no additional tablespaces and WAL streaming is not
104 used.
105
106 -r rate
107 --max-rate=rate
108 The maximum transfer rate of data transferred from the server.
109 Values are in kilobytes per second. Use a suffix of M to indicate
110 megabytes per second. A suffix of k is also accepted, and has no
111 effect. Valid values are between 32 kilobytes per second and 1024
112 megabytes per second.
113
114 The purpose is to limit the impact of pg_basebackup on the running
115 server.
116
117 This option always affects transfer of the data directory. Transfer
118 of WAL files is only affected if the collection method is fetch.
119
120 -R
121 --write-recovery-conf
122 Write a minimal recovery.conf in the output directory (or into the
123 base archive file when using tar format) to ease setting up a
124 standby server. The recovery.conf file will record the connection
125 settings and, if specified, the replication slot that pg_basebackup
126 is using, so that the streaming replication will use the same
127 settings later on.
128
129 -S slotname
130 --slot=slotname
131 This option can only be used together with -X stream. It causes the
132 WAL streaming to use the specified replication slot. If the base
133 backup is intended to be used as a streaming replication standby
134 using replication slots, it should then use the same replication
135 slot name in recovery.conf. That way, it is ensured that the server
136 does not remove any necessary WAL data in the time between the end
137 of the base backup and the start of streaming replication.
138
139 If this option is not specified and the server supports temporary
140 replication slots (version 10 and later), then a temporary
141 replication slot is automatically used for WAL streaming.
142
143 --no-slot
144 This option prevents the creation of a temporary replication slot
145 during the backup even if it's supported by the server.
146
147 Temporary replication slots are created by default if no slot name
148 is given with the option -S when using log streaming.
149
150 The main purpose of this option is to allow taking a base backup
151 when the server is out of free replication slots. Using replication
152 slots is almost always preferred, because it prevents needed WAL
153 from being removed by the server during the backup.
154
155 -T olddir=newdir
156 --tablespace-mapping=olddir=newdir
157 Relocate the tablespace in directory olddir to newdir during the
158 backup. To be effective, olddir must exactly match the path
159 specification of the tablespace as it is currently defined. (But it
160 is not an error if there is no tablespace in olddir contained in
161 the backup.) Both olddir and newdir must be absolute paths. If a
162 path happens to contain a = sign, escape it with a backslash. This
163 option can be specified multiple times for multiple tablespaces.
164 See examples below.
165
166 If a tablespace is relocated in this way, the symbolic links inside
167 the main data directory are updated to point to the new location.
168 So the new data directory is ready to be used for a new server
169 instance with all tablespaces in the updated locations.
170
171 --waldir=waldir
172 Specifies the location for the write-ahead log directory. waldir
173 must be an absolute path. The write-ahead log directory can only be
174 specified when the backup is in plain mode.
175
176 -X method
177 --wal-method=method
178 Includes the required write-ahead log files (WAL files) in the
179 backup. This will include all write-ahead logs generated during the
180 backup. Unless the method none is specified, it is possible to
181 start a postmaster directly in the extracted directory without the
182 need to consult the log archive, thus making this a completely
183 standalone backup.
184
185 The following methods for collecting the write-ahead logs are
186 supported:
187
188 n
189 none
190 Don't include write-ahead log in the backup.
191
192 f
193 fetch
194 The write-ahead log files are collected at the end of the
195 backup. Therefore, it is necessary for the wal_keep_segments
196 parameter to be set high enough that the log is not removed
197 before the end of the backup. If the log has been rotated when
198 it's time to transfer it, the backup will fail and be unusable.
199
200 When tar format mode is used, the write-ahead log files will be
201 written to the base.tar file.
202
203 s
204 stream
205 Stream the write-ahead log while the backup is created. This
206 will open a second connection to the server and start streaming
207 the write-ahead log in parallel while running the backup.
208 Therefore, it will use up two connections configured by the
209 max_wal_senders parameter. As long as the client can keep up
210 with write-ahead log received, using this mode requires no
211 extra write-ahead logs to be saved on the master.
212
213 When tar format mode is used, the write-ahead log files will be
214 written to a separate file named pg_wal.tar (if the server is a
215 version earlier than 10, the file will be named pg_xlog.tar).
216
217 This value is the default.
218
219
220 -z
221 --gzip
222 Enables gzip compression of tar file output, with the default
223 compression level. Compression is only available when using the tar
224 format, and the suffix .gz will automatically be added to all tar
225 filenames.
226
227 -Z level
228 --compress=level
229 Enables gzip compression of tar file output, and specifies the
230 compression level (0 through 9, 0 being no compression and 9 being
231 best compression). Compression is only available when using the tar
232 format, and the suffix .gz will automatically be added to all tar
233 filenames.
234
235 The following command-line options control the generation of the backup
236 and the running of the program.
237
238 -c fast|spread
239 --checkpoint=fast|spread
240 Sets checkpoint mode to fast (immediate) or spread (default) (see
241 Section 25.3.3).
242
243 -l label
244 --label=label
245 Sets the label for the backup. If none is specified, a default
246 value of “pg_basebackup base backup” will be used.
247
248 -n
249 --no-clean
250 By default, when pg_basebackup aborts with an error, it removes any
251 directories it might have created before discovering that it cannot
252 finish the job (for example, data directory and write-ahead log
253 directory). This option inhibits tidying-up and is thus useful for
254 debugging.
255
256 Note that tablespace directories are not cleaned up either way.
257
258 -P
259 --progress
260 Enables progress reporting. Turning this on will deliver an
261 approximate progress report during the backup. Since the database
262 may change during the backup, this is only an approximation and may
263 not end at exactly 100%. In particular, when WAL log is included in
264 the backup, the total amount of data cannot be estimated in
265 advance, and in this case the estimated target size will increase
266 once it passes the total estimate without WAL.
267
268 When this is enabled, the backup will start by enumerating the size
269 of the entire database, and then go back and send the actual
270 contents. This may make the backup take slightly longer, and in
271 particular it will take longer before the first data is sent.
272
273 -N
274 --no-sync
275 By default, pg_basebackup will wait for all files to be written
276 safely to disk. This option causes pg_basebackup to return without
277 waiting, which is faster, but means that a subsequent operating
278 system crash can leave the base backup corrupt. Generally, this
279 option is useful for testing but should not be used when creating a
280 production installation.
281
282 -v
283 --verbose
284 Enables verbose mode. Will output some extra steps during startup
285 and shutdown, as well as show the exact file name that is currently
286 being processed if progress reporting is also enabled.
287
288 The following command-line options control the database connection
289 parameters.
290
291 -d connstr
292 --dbname=connstr
293 Specifies parameters used to connect to the server, as a connection
294 string. See Section 33.1.1 for more information.
295
296 The option is called --dbname for consistency with other client
297 applications, but because pg_basebackup doesn't connect to any
298 particular database in the cluster, database name in the connection
299 string will be ignored.
300
301 -h host
302 --host=host
303 Specifies the host name of the machine on which the server is
304 running. If the value begins with a slash, it is used as the
305 directory for the Unix domain socket. The default is taken from the
306 PGHOST environment variable, if set, else a Unix domain socket
307 connection is attempted.
308
309 -p port
310 --port=port
311 Specifies the TCP port or local Unix domain socket file extension
312 on which the server is listening for connections. Defaults to the
313 PGPORT environment variable, if set, or a compiled-in default.
314
315 -s interval
316 --status-interval=interval
317 Specifies the number of seconds between status packets sent back to
318 the server. This allows for easier monitoring of the progress from
319 server. A value of zero disables the periodic status updates
320 completely, although an update will still be sent when requested by
321 the server, to avoid timeout disconnect. The default value is 10
322 seconds.
323
324 -U username
325 --username=username
326 User name to connect as.
327
328 -w
329 --no-password
330 Never issue a password prompt. If the server requires password
331 authentication and a password is not available by other means such
332 as a .pgpass file, the connection attempt will fail. This option
333 can be useful in batch jobs and scripts where no user is present to
334 enter a password.
335
336 -W
337 --password
338 Force pg_basebackup to prompt for a password before connecting to a
339 database.
340
341 This option is never essential, since pg_basebackup will
342 automatically prompt for a password if the server demands password
343 authentication. However, pg_basebackup will waste a connection
344 attempt finding out that the server wants a password. In some cases
345 it is worth typing -W to avoid the extra connection attempt.
346
347 Other options are also available:
348
349 -V
350 --version
351 Print the pg_basebackup version and exit.
352
353 -?
354 --help
355 Show help about pg_basebackup command line arguments, and exit.
356
358 This utility, like most other PostgreSQL utilities, uses the
359 environment variables supported by libpq (see Section 33.14).
360
362 At the beginning of the backup, a checkpoint needs to be written on the
363 server the backup is taken from. Especially if the option
364 --checkpoint=fast is not used, this can take some time during which
365 pg_basebackup will be appear to be idle.
366
367 The backup will include all files in the data directory and
368 tablespaces, including the configuration files and any additional files
369 placed in the directory by third parties, except certain temporary
370 files managed by PostgreSQL. But only regular files and directories are
371 copied, except that symbolic links used for tablespaces are preserved.
372 Symbolic links pointing to certain directories known to PostgreSQL are
373 copied as empty directories. Other symbolic links and special device
374 files are skipped. See Section 52.4 for the precise details.
375
376 Tablespaces will in plain format by default be backed up to the same
377 path they have on the server, unless the option --tablespace-mapping is
378 used. Without this option, running a plain format base backup on the
379 same host as the server will not work if tablespaces are in use,
380 because the backup would have to be written to the same directory
381 locations as the original tablespaces.
382
383 When tar format mode is used, it is the user's responsibility to unpack
384 each tar file before starting the PostgreSQL server. If there are
385 additional tablespaces, the tar files for them need to be unpacked in
386 the correct locations. In this case the symbolic links for those
387 tablespaces will be created by the server according to the contents of
388 the tablespace_map file that is included in the base.tar file.
389
390 pg_basebackup works with servers of the same or an older major version,
391 down to 9.1. However, WAL streaming mode (-X stream) only works with
392 server version 9.3 and later, and tar format mode (--format=tar) of the
393 current version only works with server version 9.5 or later.
394
396 To create a base backup of the server at mydbserver and store it in the
397 local directory /usr/local/pgsql/data:
398
399 $ pg_basebackup -h mydbserver -D /usr/local/pgsql/data
400
401 To create a backup of the local server with one compressed tar file for
402 each tablespace, and store it in the directory backup, showing a
403 progress report while running:
404
405 $ pg_basebackup -D backup -Ft -z -P
406
407 To create a backup of a single-tablespace local database and compress
408 this with bzip2:
409
410 $ pg_basebackup -D - -Ft -X fetch | bzip2 > backup.tar.bz2
411
412 (This command will fail if there are multiple tablespaces in the
413 database.)
414
415 To create a backup of a local database where the tablespace in /opt/ts
416 is relocated to ./backup/ts:
417
418 $ pg_basebackup -D backup/data -T /opt/ts=$(pwd)/backup/ts
419
420
422 pg_dump(1)
423
424
425
426PostgreSQL 10.7 2019 PG_BASEBACKUP(1)