doc: clarify pg_dump --no-comments meaning as SQL comments
[pgsql.git] / doc / src / sgml / ref / pg_dump.sgml
blobd66e901f51b882e68a791b3648160be6ff9a4631
1 <!--
2 doc/src/sgml/ref/pg_dump.sgml
3 PostgreSQL documentation
4 -->
6 <refentry id="app-pgdump">
7 <indexterm zone="app-pgdump">
8 <primary>pg_dump</primary>
9 </indexterm>
11 <refmeta>
12 <refentrytitle><application>pg_dump</application></refentrytitle>
13 <manvolnum>1</manvolnum>
14 <refmiscinfo>Application</refmiscinfo>
15 </refmeta>
17 <refnamediv>
18 <refname>pg_dump</refname>
20 <refpurpose>
21 extract a <productname>PostgreSQL</productname> database into a script file or other archive file
22 </refpurpose>
23 </refnamediv>
25 <refsynopsisdiv>
26 <cmdsynopsis>
27 <command>pg_dump</command>
28 <arg rep="repeat"><replaceable>connection-option</replaceable></arg>
29 <arg rep="repeat"><replaceable>option</replaceable></arg>
30 <arg choice="opt"><replaceable>dbname</replaceable></arg>
31 </cmdsynopsis>
32 </refsynopsisdiv>
35 <refsect1 id="pg-dump-description">
36 <title>Description</title>
38 <para>
39 <application>pg_dump</application> is a utility for exporting a
40 <productname>PostgreSQL</productname> database. It makes consistent
41 exports even if the database is being used concurrently.
42 <application>pg_dump</application> does not block other users
43 accessing the database (readers or writers).
44 Note, however, that except in simple cases,
45 <application>pg_dump</application> is generally not the right choice for
46 taking regular backups of production databases. See <xref
47 linkend="backup"/> for further discussion.
48 </para>
50 <para>
51 <application>pg_dump</application> only dumps a single database.
52 To export an entire cluster, or to export global objects that are
53 common to all databases in a cluster (such as roles and tablespaces),
54 use <xref linkend="app-pg-dumpall"/>.
55 </para>
57 <para>
58 Dumps can be output in script or archive file formats. Script
59 dumps are plain-text files containing the SQL commands required
60 to reconstruct the database to the state it was in at the time it was
61 saved. To restore from such a script, feed it to <xref
62 linkend="app-psql"/>. Script files
63 can be used to reconstruct the database even on other machines and
64 other architectures; with some modifications, even on other SQL
65 database products.
66 </para>
68 <para>
69 The alternative archive file formats must be used with
70 <xref linkend="app-pgrestore"/> to rebuild the database. They
71 allow <application>pg_restore</application> to be selective about
72 what is restored, or even to reorder the items prior to being
73 restored.
74 The archive file formats are designed to be portable across
75 architectures.
76 </para>
78 <para>
79 When used with one of the archive file formats and combined with
80 <application>pg_restore</application>,
81 <application>pg_dump</application> provides a flexible archival and
82 transfer mechanism. <application>pg_dump</application> can be used to
83 export an entire database, then <application>pg_restore</application>
84 can be used to examine the archive and/or select which parts of the
85 database are to be restored. The most flexible output file formats are
86 the <quote>custom</quote> format (<option>-Fc</option>) and the
87 <quote>directory</quote> format (<option>-Fd</option>). They allow
88 for selection and reordering of all archived items, support parallel
89 restoration, and are compressed by default. The <quote>directory</quote>
90 format is the only format that supports parallel dumps.
91 </para>
93 <para>
94 While running <application>pg_dump</application>, one should examine the
95 output for any warnings (printed on standard error), especially in
96 light of the limitations listed below.
97 </para>
99 </refsect1>
101 <refsect1 id="pg-dump-options">
102 <title>Options</title>
104 <para>
105 The following command-line options control the content and
106 format of the output.
108 <variablelist>
109 <varlistentry>
110 <term><replaceable class="parameter">dbname</replaceable></term>
111 <listitem>
112 <para>
113 Specifies the name of the database to be dumped. If this is
114 not specified, the environment variable
115 <envar>PGDATABASE</envar> is used. If that is not set, the
116 user name specified for the connection is used.
117 </para>
118 </listitem>
119 </varlistentry>
121 <varlistentry>
122 <term><option>-a</option></term>
123 <term><option>--data-only</option></term>
124 <listitem>
125 <para>
126 Dump only the data, not the schema (data definitions).
127 Table data, large objects, and sequence values are dumped.
128 </para>
130 <para>
131 This option is similar to, but for historical reasons not identical
132 to, specifying <option>--section=data</option>.
133 </para>
134 </listitem>
135 </varlistentry>
137 <varlistentry>
138 <term><option>-b</option></term>
139 <term><option>--large-objects</option></term>
140 <term><option>--blobs</option> (deprecated)</term>
141 <listitem>
142 <para>
143 Include large objects in the dump. This is the default behavior
144 except when <option>--schema</option>, <option>--table</option>, or
145 <option>--schema-only</option> is specified. The <option>-b</option>
146 switch is therefore only useful to add large objects to dumps
147 where a specific schema or table has been requested. Note that
148 large objects are considered data and therefore will be included when
149 <option>--data-only</option> is used, but not
150 when <option>--schema-only</option> is.
151 </para>
152 </listitem>
153 </varlistentry>
155 <varlistentry>
156 <term><option>-B</option></term>
157 <term><option>--no-large-objects</option></term>
158 <term><option>--no-blobs</option> (deprecated)</term>
159 <listitem>
160 <para>
161 Exclude large objects in the dump.
162 </para>
164 <para>
165 When both <option>-b</option> and <option>-B</option> are given, the behavior
166 is to output large objects, when data is being dumped, see the
167 <option>-b</option> documentation.
168 </para>
169 </listitem>
170 </varlistentry>
172 <varlistentry>
173 <term><option>-c</option></term>
174 <term><option>--clean</option></term>
175 <listitem>
176 <para>
177 Output commands to <command>DROP</command> all the dumped
178 database objects prior to outputting the commands for creating them.
179 This option is useful when the restore is to overwrite an existing
180 database. If any of the objects do not exist in the destination
181 database, ignorable error messages will be reported during
182 restore, unless <option>--if-exists</option> is also specified.
183 </para>
185 <para>
186 This option is ignored when emitting an archive (non-text) output
187 file. For the archive formats, you can specify the option when you
188 call <command>pg_restore</command>.
189 </para>
190 </listitem>
191 </varlistentry>
193 <varlistentry>
194 <term><option>-C</option></term>
195 <term><option>--create</option></term>
196 <listitem>
197 <para>
198 Begin the output with a command to create the
199 database itself and reconnect to the created database. (With a
200 script of this form, it doesn't matter which database in the
201 destination installation you connect to before running the script.)
202 If <option>--clean</option> is also specified, the script drops and
203 recreates the target database before reconnecting to it.
204 </para>
206 <para>
207 With <option>--create</option>, the output also includes the
208 database's comment if any, and any configuration variable settings
209 that are specific to this database, that is,
210 any <command>ALTER DATABASE ... SET ...</command>
211 and <command>ALTER ROLE ... IN DATABASE ... SET ...</command>
212 commands that mention this database.
213 Access privileges for the database itself are also dumped,
214 unless <option>--no-acl</option> is specified.
215 </para>
217 <para>
218 This option is ignored when emitting an archive (non-text) output
219 file. For the archive formats, you can specify the option when you
220 call <command>pg_restore</command>.
221 </para>
222 </listitem>
223 </varlistentry>
225 <varlistentry>
226 <term><option>-e <replaceable class="parameter">pattern</replaceable></option></term>
227 <term><option>--extension=<replaceable class="parameter">pattern</replaceable></option></term>
228 <listitem>
229 <para>
230 Dump only extensions matching <replaceable
231 class="parameter">pattern</replaceable>. When this option is not
232 specified, all non-system extensions in the target database will be
233 dumped. Multiple extensions can be selected by writing multiple
234 <option>-e</option> switches. The <replaceable
235 class="parameter">pattern</replaceable> parameter is interpreted as a
236 pattern according to the same rules used by
237 <application>psql</application>'s <literal>\d</literal> commands (see
238 <xref linkend="app-psql-patterns"/>), so multiple extensions can also
239 be selected by writing wildcard characters in the pattern. When using
240 wildcards, be careful to quote the pattern if needed to prevent the
241 shell from expanding the wildcards.
242 </para>
244 <para>
245 Any configuration relation registered by
246 <function>pg_extension_config_dump</function> is included in the
247 dump if its extension is specified by <option>--extension</option>.
248 </para>
250 <note>
251 <para>
252 When <option>-e</option> is specified,
253 <application>pg_dump</application> makes no attempt to dump any other
254 database objects that the selected extension(s) might depend upon.
255 Therefore, there is no guarantee that the results of a
256 specific-extension dump can be successfully restored by themselves
257 into a clean database.
258 </para>
259 </note>
260 </listitem>
261 </varlistentry>
263 <varlistentry>
264 <term><option>-E <replaceable class="parameter">encoding</replaceable></option></term>
265 <term><option>--encoding=<replaceable class="parameter">encoding</replaceable></option></term>
266 <listitem>
267 <para>
268 Create the dump in the specified character set encoding. By default,
269 the dump is created in the database encoding. (Another way to get the
270 same result is to set the <envar>PGCLIENTENCODING</envar> environment
271 variable to the desired dump encoding.) The supported encodings are
272 described in <xref linkend="multibyte-charset-supported"/>.
273 </para>
274 </listitem>
275 </varlistentry>
277 <varlistentry>
278 <term><option>-f <replaceable class="parameter">file</replaceable></option></term>
279 <term><option>--file=<replaceable class="parameter">file</replaceable></option></term>
280 <listitem>
281 <para>
282 Send output to the specified file. This parameter can be omitted for
283 file based output formats, in which case the standard output is used.
284 It must be given for the directory output format however, where it
285 specifies the target directory instead of a file. In this case the
286 directory is created by <command>pg_dump</command> and must not exist
287 before.
288 </para>
289 </listitem>
290 </varlistentry>
292 <varlistentry>
293 <term><option>-F <replaceable class="parameter">format</replaceable></option></term>
294 <term><option>--format=<replaceable class="parameter">format</replaceable></option></term>
295 <listitem>
296 <para>
297 Selects the format of the output.
298 <replaceable>format</replaceable> can be one of the following:
300 <variablelist>
301 <varlistentry>
302 <term><literal>p</literal></term>
303 <term><literal>plain</literal></term>
304 <listitem>
305 <para>
306 Output a plain-text <acronym>SQL</acronym> script file (the default).
307 </para>
308 </listitem>
309 </varlistentry>
311 <varlistentry>
312 <term><literal>c</literal></term>
313 <term><literal>custom</literal></term>
314 <listitem>
315 <para>
316 Output a custom-format archive suitable for input into
317 <application>pg_restore</application>.
318 Together with the directory output format, this is the most flexible
319 output format in that it allows manual selection and reordering of
320 archived items during restore. This format is also compressed by
321 default.
322 </para>
323 </listitem>
324 </varlistentry>
326 <varlistentry>
327 <term><literal>d</literal></term>
328 <term><literal>directory</literal></term>
329 <listitem>
330 <para>
331 Output a directory-format archive suitable for input into
332 <application>pg_restore</application>. This will create a directory
333 with one file for each table and large object being dumped, plus a
334 so-called Table of Contents file describing the dumped objects in a
335 machine-readable format that <application>pg_restore</application>
336 can read. A directory format archive can be manipulated with
337 standard Unix tools; for example, files in an uncompressed archive
338 can be compressed with the <application>gzip</application>,
339 <application>lz4</application>, or
340 <application>zstd</application> tools.
341 This format is compressed by default using <literal>gzip</literal>
342 and also supports parallel dumps.
343 </para>
344 </listitem>
345 </varlistentry>
347 <varlistentry>
348 <term><literal>t</literal></term>
349 <term><literal>tar</literal></term>
350 <listitem>
351 <para>
352 Output a <command>tar</command>-format archive suitable for input
353 into <application>pg_restore</application>. The tar format is
354 compatible with the directory format: extracting a tar-format
355 archive produces a valid directory-format archive.
356 However, the tar format does not support compression. Also, when
357 using tar format the relative order of table data items cannot be
358 changed during restore.
359 </para>
360 </listitem>
361 </varlistentry>
362 </variablelist></para>
363 </listitem>
364 </varlistentry>
366 <varlistentry>
367 <term><option>-j <replaceable class="parameter">njobs</replaceable></option></term>
368 <term><option>--jobs=<replaceable class="parameter">njobs</replaceable></option></term>
369 <listitem>
370 <para>
371 Run the dump in parallel by dumping <replaceable class="parameter">njobs</replaceable>
372 tables simultaneously. This option may reduce the time needed to perform the dump but it also
373 increases the load on the database server. You can only use this option with the
374 directory output format because this is the only output format where multiple processes
375 can write their data at the same time.
376 </para>
377 <para><application>pg_dump</application> will open <replaceable class="parameter">njobs</replaceable>
378 + 1 connections to the database, so make sure your <xref linkend="guc-max-connections"/>
379 setting is high enough to accommodate all connections.
380 </para>
381 <para>
382 Requesting exclusive locks on database objects while running a parallel dump could
383 cause the dump to fail. The reason is that the <application>pg_dump</application> leader process
384 requests shared locks (<link linkend="locking-tables">ACCESS SHARE</link>) on the
385 objects that the worker processes are going to dump later in order to
386 make sure that nobody deletes them and makes them go away while the dump is running.
387 If another client then requests an exclusive lock on a table, that lock will not be
388 granted but will be queued waiting for the shared lock of the leader process to be
389 released. Consequently any other access to the table will not be granted either and
390 will queue after the exclusive lock request. This includes the worker process trying
391 to dump the table. Without any precautions this would be a classic deadlock situation.
392 To detect this conflict, the <application>pg_dump</application> worker process requests another
393 shared lock using the <literal>NOWAIT</literal> option. If the worker process is not granted
394 this shared lock, somebody else must have requested an exclusive lock in the meantime
395 and there is no way to continue with the dump, so <application>pg_dump</application> has no choice
396 but to abort the dump.
397 </para>
398 <para>
399 To perform a parallel dump, the database server needs to support
400 synchronized snapshots, a feature that was introduced in
401 <productname>PostgreSQL</productname> 9.2 for primary servers and 10
402 for standbys. With this feature, database clients can ensure they see
403 the same data set even though they use different connections.
404 <command>pg_dump -j</command> uses multiple database connections; it
405 connects to the database once with the leader process and once again
406 for each worker job. Without the synchronized snapshot feature, the
407 different worker jobs wouldn't be guaranteed to see the same data in
408 each connection, which could lead to an inconsistent backup.
409 </para>
410 </listitem>
411 </varlistentry>
413 <varlistentry>
414 <term><option>-n <replaceable class="parameter">pattern</replaceable></option></term>
415 <term><option>--schema=<replaceable class="parameter">pattern</replaceable></option></term>
416 <listitem>
417 <para>
418 Dump only schemas matching <replaceable
419 class="parameter">pattern</replaceable>; this selects both the
420 schema itself, and all its contained objects. When this option is
421 not specified, all non-system schemas in the target database will be
422 dumped. Multiple schemas can be
423 selected by writing multiple <option>-n</option> switches. The
424 <replaceable class="parameter">pattern</replaceable> parameter is
425 interpreted as a pattern according to the same rules used by
426 <application>psql</application>'s <literal>\d</literal> commands
427 (see <xref linkend="app-psql-patterns"/>),
428 so multiple schemas can also be selected by writing wildcard characters
429 in the pattern. When using wildcards, be careful to quote the pattern
430 if needed to prevent the shell from expanding the wildcards; see
431 <xref linkend="pg-dump-examples"/> below.
432 </para>
434 <note>
435 <para>
436 When <option>-n</option> is specified, <application>pg_dump</application>
437 makes no attempt to dump any other database objects that the selected
438 schema(s) might depend upon. Therefore, there is no guarantee
439 that the results of a specific-schema dump can be successfully
440 restored by themselves into a clean database.
441 </para>
442 </note>
444 <note>
445 <para>
446 Non-schema objects such as large objects are not dumped when <option>-n</option> is
447 specified. You can add large objects back to the dump with the
448 <option>--large-objects</option> switch.
449 </para>
450 </note>
452 </listitem>
453 </varlistentry>
455 <varlistentry>
456 <term><option>-N <replaceable class="parameter">pattern</replaceable></option></term>
457 <term><option>--exclude-schema=<replaceable class="parameter">pattern</replaceable></option></term>
458 <listitem>
459 <para>
460 Do not dump any schemas matching <replaceable
461 class="parameter">pattern</replaceable>. The pattern is
462 interpreted according to the same rules as for <option>-n</option>.
463 <option>-N</option> can be given more than once to exclude schemas
464 matching any of several patterns.
465 </para>
467 <para>
468 When both <option>-n</option> and <option>-N</option> are given, the behavior
469 is to dump just the schemas that match at least one <option>-n</option>
470 switch but no <option>-N</option> switches. If <option>-N</option> appears
471 without <option>-n</option>, then schemas matching <option>-N</option> are
472 excluded from what is otherwise a normal dump.
473 </para>
474 </listitem>
475 </varlistentry>
477 <varlistentry>
478 <term><option>-O</option></term>
479 <term><option>--no-owner</option></term>
480 <listitem>
481 <para>
482 Do not output commands to set
483 ownership of objects to match the original database.
484 By default, <application>pg_dump</application> issues
485 <command>ALTER OWNER</command> or
486 <command>SET SESSION AUTHORIZATION</command>
487 statements to set ownership of created database objects.
488 These statements
489 will fail when the script is run unless it is started by a superuser
490 (or the same user that owns all of the objects in the script).
491 To make a script that can be restored by any user, but will give
492 that user ownership of all the objects, specify <option>-O</option>.
493 </para>
495 <para>
496 This option is ignored when emitting an archive (non-text) output
497 file. For the archive formats, you can specify the option when you
498 call <command>pg_restore</command>.
499 </para>
500 </listitem>
501 </varlistentry>
503 <varlistentry>
504 <term><option>-R</option></term>
505 <term><option>--no-reconnect</option></term>
506 <listitem>
507 <para>
508 This option is obsolete but still accepted for backwards
509 compatibility.
510 </para>
511 </listitem>
512 </varlistentry>
514 <varlistentry>
515 <term><option>-s</option></term>
516 <term><option>--schema-only</option></term>
517 <listitem>
518 <para>
519 Dump only the object definitions (schema), not data.
520 </para>
521 <para>
522 This option is the inverse of <option>--data-only</option>.
523 It is similar to, but for historical reasons not identical to,
524 specifying
525 <option>--section=pre-data --section=post-data</option>.
526 </para>
527 <para>
528 (Do not confuse this with the <option>--schema</option> option, which
529 uses the word <quote>schema</quote> in a different meaning.)
530 </para>
531 <para>
532 To exclude table data for only a subset of tables in the database,
533 see <option>--exclude-table-data</option>.
534 </para>
535 </listitem>
536 </varlistentry>
538 <varlistentry>
539 <term><option>-S <replaceable class="parameter">username</replaceable></option></term>
540 <term><option>--superuser=<replaceable class="parameter">username</replaceable></option></term>
541 <listitem>
542 <para>
543 Specify the superuser user name to use when disabling triggers.
544 This is relevant only if <option>--disable-triggers</option> is used.
545 (Usually, it's better to leave this out, and instead start the
546 resulting script as superuser.)
547 </para>
548 </listitem>
549 </varlistentry>
551 <varlistentry>
552 <term><option>-t <replaceable class="parameter">pattern</replaceable></option></term>
553 <term><option>--table=<replaceable class="parameter">pattern</replaceable></option></term>
554 <listitem>
555 <para>
556 Dump only tables with names matching
557 <replaceable class="parameter">pattern</replaceable>. Multiple tables
558 can be selected by writing multiple <option>-t</option> switches. The
559 <replaceable class="parameter">pattern</replaceable> parameter is
560 interpreted as a pattern according to the same rules used by
561 <application>psql</application>'s <literal>\d</literal> commands
562 (see <xref linkend="app-psql-patterns"/>),
563 so multiple tables can also be selected by writing wildcard characters
564 in the pattern. When using wildcards, be careful to quote the pattern
565 if needed to prevent the shell from expanding the wildcards; see
566 <xref linkend="pg-dump-examples"/> below.
567 </para>
569 <para>
570 As well as tables, this option can be used to dump the definition of matching
571 views, materialized views, foreign tables, and sequences. It will not dump the
572 contents of views or materialized views, and the contents of foreign tables will
573 only be dumped if the corresponding foreign server is specified with
574 <option>--include-foreign-data</option>.
575 </para>
577 <para>
578 The <option>-n</option> and <option>-N</option> switches have no effect when
579 <option>-t</option> is used, because tables selected by <option>-t</option> will
580 be dumped regardless of those switches, and non-table objects will not
581 be dumped.
582 </para>
584 <note>
585 <para>
586 When <option>-t</option> is specified, <application>pg_dump</application>
587 makes no attempt to dump any other database objects that the selected
588 table(s) might depend upon. Therefore, there is no guarantee
589 that the results of a specific-table dump can be successfully
590 restored by themselves into a clean database.
591 </para>
592 </note>
594 </listitem>
595 </varlistentry>
597 <varlistentry>
598 <term><option>-T <replaceable class="parameter">pattern</replaceable></option></term>
599 <term><option>--exclude-table=<replaceable class="parameter">pattern</replaceable></option></term>
600 <listitem>
601 <para>
602 Do not dump any tables matching <replaceable
603 class="parameter">pattern</replaceable>. The pattern is
604 interpreted according to the same rules as for <option>-t</option>.
605 <option>-T</option> can be given more than once to exclude tables
606 matching any of several patterns.
607 </para>
609 <para>
610 When both <option>-t</option> and <option>-T</option> are given, the behavior
611 is to dump just the tables that match at least one <option>-t</option>
612 switch but no <option>-T</option> switches. If <option>-T</option> appears
613 without <option>-t</option>, then tables matching <option>-T</option> are
614 excluded from what is otherwise a normal dump.
615 </para>
616 </listitem>
617 </varlistentry>
619 <varlistentry>
620 <term><option>-v</option></term>
621 <term><option>--verbose</option></term>
622 <listitem>
623 <para>
624 Specifies verbose mode. This will cause
625 <application>pg_dump</application> to output detailed object
626 comments and start/stop times to the dump file, and progress
627 messages to standard error.
628 Repeating the option causes additional debug-level messages
629 to appear on standard error.
630 </para>
631 </listitem>
632 </varlistentry>
634 <varlistentry>
635 <term><option>-V</option></term>
636 <term><option>--version</option></term>
637 <listitem>
638 <para>
639 Print the <application>pg_dump</application> version and exit.
640 </para>
641 </listitem>
642 </varlistentry>
644 <varlistentry>
645 <term><option>-x</option></term>
646 <term><option>--no-privileges</option></term>
647 <term><option>--no-acl</option></term>
648 <listitem>
649 <para>
650 Prevent dumping of access privileges (grant/revoke commands).
651 </para>
652 </listitem>
653 </varlistentry>
655 <varlistentry>
656 <term><option>-Z <replaceable class="parameter">level</replaceable></option></term>
657 <term><option>-Z <replaceable class="parameter">method</replaceable></option>[:<replaceable>detail</replaceable>]</term>
658 <term><option>--compress=<replaceable class="parameter">level</replaceable></option></term>
659 <term><option>--compress=<replaceable class="parameter">method</replaceable></option>[:<replaceable>detail</replaceable>]</term>
660 <listitem>
661 <para>
662 Specify the compression method and/or the compression level to use.
663 The compression method can be set to <literal>gzip</literal>,
664 <literal>lz4</literal>, <literal>zstd</literal>,
665 or <literal>none</literal> for no compression.
666 A compression detail string can optionally be specified. If the
667 detail string is an integer, it specifies the compression level.
668 Otherwise, it should be a comma-separated list of items, each of the
669 form <literal>keyword</literal> or <literal>keyword=value</literal>.
670 Currently, the supported keywords are <literal>level</literal> and
671 <literal>long</literal>.
672 </para>
674 <para>
675 If no compression level is specified, the default compression
676 level will be used. If only a level is specified without mentioning
677 an algorithm, <literal>gzip</literal> compression will be used if
678 the level is greater than <literal>0</literal>, and no compression
679 will be used if the level is <literal>0</literal>.
680 </para>
682 <para>
683 For the custom and directory archive formats, this specifies compression of
684 individual table-data segments, and the default is to compress using
685 <literal>gzip</literal> at a moderate level. For plain text output,
686 setting a nonzero compression level causes the entire output file to be compressed,
687 as though it had been fed through <application>gzip</application>,
688 <application>lz4</application>, or <application>zstd</application>;
689 but the default is not to compress.
690 With zstd compression, <literal>long</literal> mode may improve the
691 compression ratio, at the cost of increased memory use.
692 </para>
693 <para>
694 The tar archive format currently does not support compression at all.
695 </para>
696 </listitem>
697 </varlistentry>
699 <varlistentry>
700 <term><option>--binary-upgrade</option></term>
701 <listitem>
702 <para>
703 This option is for use by in-place upgrade utilities. Its use
704 for other purposes is not recommended or supported. The
705 behavior of the option may change in future releases without
706 notice.
707 </para>
708 </listitem>
709 </varlistentry>
711 <varlistentry>
712 <term><option>--column-inserts</option></term>
713 <term><option>--attribute-inserts</option></term>
714 <listitem>
715 <para>
716 Dump data as <command>INSERT</command> commands with explicit
717 column names (<literal>INSERT INTO
718 <replaceable>table</replaceable>
719 (<replaceable>column</replaceable>, ...) VALUES
720 ...</literal>). This will make restoration very slow; it is mainly
721 useful for making dumps that can be loaded into
722 non-<productname>PostgreSQL</productname> databases.
723 Any error during restoring will cause only rows that are part of the
724 problematic <command>INSERT</command> to be lost, rather than the
725 entire table contents.
726 </para>
727 </listitem>
728 </varlistentry>
730 <varlistentry>
731 <term><option>--disable-dollar-quoting</option></term>
732 <listitem>
733 <para>
734 This option disables the use of dollar quoting for function bodies,
735 and forces them to be quoted using SQL standard string syntax.
736 </para>
737 </listitem>
738 </varlistentry>
740 <varlistentry>
741 <term><option>--disable-triggers</option></term>
742 <listitem>
743 <para>
744 This option is relevant only when creating a data-only dump.
745 It instructs <application>pg_dump</application> to include commands
746 to temporarily disable triggers on the target tables while
747 the data is restored. Use this if you have referential
748 integrity checks or other triggers on the tables that you
749 do not want to invoke during data restore.
750 </para>
752 <para>
753 Presently, the commands emitted for <option>--disable-triggers</option>
754 must be done as superuser. So, you should also specify
755 a superuser name with <option>-S</option>, or preferably be careful to
756 start the resulting script as a superuser.
757 </para>
759 <para>
760 This option is ignored when emitting an archive (non-text) output
761 file. For the archive formats, you can specify the option when you
762 call <command>pg_restore</command>.
763 </para>
764 </listitem>
765 </varlistentry>
767 <varlistentry>
768 <term><option>--enable-row-security</option></term>
769 <listitem>
770 <para>
771 This option is relevant only when dumping the contents of a table
772 which has row security. By default, <application>pg_dump</application> will set
773 <xref linkend="guc-row-security"/> to off, to ensure
774 that all data is dumped from the table. If the user does not have
775 sufficient privileges to bypass row security, then an error is thrown.
776 This parameter instructs <application>pg_dump</application> to set
777 <xref linkend="guc-row-security"/> to on instead, allowing the user
778 to dump the parts of the contents of the table that they have access to.
779 </para>
781 <para>
782 Note that if you use this option currently, you probably also want
783 the dump be in <command>INSERT</command> format, as the
784 <command>COPY FROM</command> during restore does not support row security.
785 </para>
786 </listitem>
787 </varlistentry>
789 <varlistentry>
790 <term><option>--exclude-extension=<replaceable class="parameter">pattern</replaceable></option></term>
791 <listitem>
792 <para>
793 Do not dump any extensions matching <replaceable
794 class="parameter">pattern</replaceable>. The pattern is
795 interpreted according to the same rules as for <option>-e</option>.
796 <option>--exclude-extension</option> can be given more than once to exclude extensions
797 matching any of several patterns.
798 </para>
800 <para>
801 When both <option>-e</option> and <option>--exclude-extension</option> are given, the behavior
802 is to dump just the extensions that match at least one <option>-e</option>
803 switch but no <option>--exclude-extension</option> switches. If <option>--exclude-extension</option>
804 appears without <option>-e</option>, then extensions matching <option>--exclude-extension</option> are
805 excluded from what is otherwise a normal dump.
806 </para>
807 </listitem>
808 </varlistentry>
810 <varlistentry>
811 <term><option>--exclude-table-and-children=<replaceable class="parameter">pattern</replaceable></option></term>
812 <listitem>
813 <para>
814 This is the same as
815 the <option>-T</option>/<option>--exclude-table</option> option,
816 except that it also excludes any partitions or inheritance child
817 tables of the table(s) matching the
818 <replaceable class="parameter">pattern</replaceable>.
819 </para>
820 </listitem>
821 </varlistentry>
823 <varlistentry>
824 <term><option>--exclude-table-data=<replaceable class="parameter">pattern</replaceable></option></term>
825 <listitem>
826 <para>
827 Do not dump data for any tables matching <replaceable
828 class="parameter">pattern</replaceable>. The pattern is
829 interpreted according to the same rules as for <option>-t</option>.
830 <option>--exclude-table-data</option> can be given more than once to
831 exclude tables matching any of several patterns. This option is
832 useful when you need the definition of a particular table even
833 though you do not need the data in it.
834 </para>
835 <para>
836 To exclude data for all tables in the database, see <option>--schema-only</option>.
837 </para>
838 </listitem>
839 </varlistentry>
841 <varlistentry>
842 <term><option>--exclude-table-data-and-children=<replaceable class="parameter">pattern</replaceable></option></term>
843 <listitem>
844 <para>
845 This is the same as the <option>--exclude-table-data</option> option,
846 except that it also excludes data of any partitions or inheritance
847 child tables of the table(s) matching the
848 <replaceable class="parameter">pattern</replaceable>.
849 </para>
850 </listitem>
851 </varlistentry>
853 <varlistentry>
854 <term><option>--extra-float-digits=<replaceable class="parameter">ndigits</replaceable></option></term>
855 <listitem>
856 <para>
857 Use the specified value of <option>extra_float_digits</option> when dumping
858 floating-point data, instead of the maximum available precision.
859 Routine dumps made for backup purposes should not use this option.
860 </para>
861 </listitem>
862 </varlistentry>
864 <varlistentry>
865 <term><option>--filter=<replaceable class="parameter">filename</replaceable></option></term>
866 <listitem>
867 <para>
868 Specify a filename from which to read patterns for objects to include
869 or exclude from the dump. The patterns are interpreted according to the
870 same rules as the corresponding options:
871 <option>-t</option>/<option>--table</option>,
872 <option>--table-and-children</option>,
873 <option>-T</option>/<option>--exclude-table</option>, and
874 <option>--exclude-table-and-children</option> for tables,
875 <option>-n</option>/<option>--schema</option> and
876 <option>-N</option>/<option>--exclude-schema</option> for schemas,
877 <option>--include-foreign-data</option> for data on foreign servers,
878 <option>--exclude-table-data</option> and
879 <option>--exclude-table-data-and-children</option> for table data, and
880 <option>-e</option>/<option>--extension</option> and
881 <option>--exclude-extension</option> for extensions.
882 To read from <literal>STDIN</literal>, use <filename>-</filename> as the
883 filename. The <option>--filter</option> option can be specified in
884 conjunction with the above listed options for including or excluding
885 objects, and can also be specified more than once for multiple filter
886 files.
887 </para>
889 <para>
890 The file lists one object pattern per row, with the following format:
891 <synopsis>
892 { include | exclude } { extension | foreign_data | table | table_and_children | table_data | table_data_and_children | schema } <replaceable class="parameter">PATTERN</replaceable>
893 </synopsis>
894 </para>
896 <para>
897 The first keyword specifies whether the objects matched by the pattern
898 are to be included or excluded. The second keyword specifies the type
899 of object to be filtered using the pattern:
900 <itemizedlist>
901 <listitem>
902 <para>
903 <literal>extension</literal>: extensions. This works like the
904 <option>-e</option>/<option>--extension</option> or
905 <option>--exclude-extension</option> option.
906 </para>
907 </listitem>
908 <listitem>
909 <para>
910 <literal>foreign_data</literal>: data on foreign servers. This works like
911 the <option>--include-foreign-data</option> option. This keyword can
912 only be used with the <literal>include</literal> keyword.
913 </para>
914 </listitem>
915 <listitem>
916 <para>
917 <literal>table</literal>: tables. This works like the
918 <option>-t</option>/<option>--table</option> or
919 <option>-T</option>/<option>--exclude-table</option> option.
920 </para>
921 </listitem>
922 <listitem>
923 <para>
924 <literal>table_and_children</literal>: tables including any partitions
925 or inheritance child tables. This works like the
926 <option>--table-and-children</option> or
927 <option>--exclude-table-and-children</option> option.
928 </para>
929 </listitem>
930 <listitem>
931 <para>
932 <literal>table_data</literal>: table data of any tables matching
933 <replaceable>pattern</replaceable>. This works like the
934 <option>--exclude-table-data</option> option. This keyword can only
935 be used with the <literal>exclude</literal> keyword.
936 </para>
937 </listitem>
938 <listitem>
939 <para>
940 <literal>table_data_and_children</literal>: table data of any tables
941 matching <replaceable>pattern</replaceable> as well as any partitions
942 or inheritance children of the table(s). This works like the
943 <option>--exclude-table-data-and-children</option> option. This
944 keyword can only be used with the <literal>exclude</literal> keyword.
945 </para>
946 </listitem>
947 <listitem>
948 <para>
949 <literal>schema</literal>: schemas. This works like the
950 <option>-n</option>/<option>--schema</option> or
951 <option>-N</option>/<option>--exclude-schema</option> option.
952 </para>
953 </listitem>
954 </itemizedlist>
955 </para>
957 <para>
958 Lines starting with <literal>#</literal> are considered comments and
959 ignored. Comments can be placed after an object pattern row as well.
960 Blank lines are also ignored. See <xref linkend="app-psql-patterns"/>
961 for how to perform quoting in patterns.
962 </para>
964 <para>
965 Example files are listed below in the <xref linkend="pg-dump-examples"/>
966 section.
967 </para>
969 </listitem>
970 </varlistentry>
972 <varlistentry>
973 <term><option>--if-exists</option></term>
974 <listitem>
975 <para>
976 Use <literal>DROP ... IF EXISTS</literal> commands to drop objects
977 in <option>--clean</option> mode. This suppresses <quote>does not
978 exist</quote> errors that might otherwise be reported. This
979 option is not valid unless <option>--clean</option> is also
980 specified.
981 </para>
982 </listitem>
983 </varlistentry>
985 <varlistentry>
986 <term><option>--include-foreign-data=<replaceable class="parameter">foreignserver</replaceable></option></term>
987 <listitem>
988 <para>
989 Dump the data for any foreign table with a foreign server
990 matching <replaceable class="parameter">foreignserver</replaceable>
991 pattern. Multiple foreign servers can be selected by writing multiple
992 <option>--include-foreign-data</option> switches.
993 Also, the <replaceable class="parameter">foreignserver</replaceable> parameter is
994 interpreted as a pattern according to the same rules used by
995 <application>psql</application>'s <literal>\d</literal> commands
996 (see <xref linkend="app-psql-patterns"/>),
997 so multiple foreign servers can also be selected by writing wildcard characters
998 in the pattern. When using wildcards, be careful to quote the pattern
999 if needed to prevent the shell from expanding the wildcards; see
1000 <xref linkend="pg-dump-examples"/> below.
1001 The only exception is that an empty pattern is disallowed.
1002 </para>
1004 <note>
1005 <para>
1006 Using wildcards in <option>--include-foreign-data</option> may result
1007 in access to unexpected foreign servers. Also, to use this option securely,
1008 make sure that the named server must have a trusted owner.
1009 </para>
1010 </note>
1012 <note>
1013 <para>
1014 When <option>--include-foreign-data</option> is specified,
1015 <application>pg_dump</application> does not check that the foreign
1016 table is writable. Therefore, there is no guarantee that the
1017 results of a foreign table dump can be successfully restored.
1018 </para>
1019 </note>
1020 </listitem>
1021 </varlistentry>
1023 <varlistentry>
1024 <term><option>--inserts</option></term>
1025 <listitem>
1026 <para>
1027 Dump data as <command>INSERT</command> commands (rather
1028 than <command>COPY</command>). This will make restoration very slow;
1029 it is mainly useful for making dumps that can be loaded into
1030 non-<productname>PostgreSQL</productname> databases.
1031 Any error during restoring will cause only rows that are part of the
1032 problematic <command>INSERT</command> to be lost, rather than the
1033 entire table contents. Note that the restore might fail altogether if
1034 you have rearranged column order. The
1035 <option>--column-inserts</option> option is safe against column order
1036 changes, though even slower.
1037 </para>
1038 </listitem>
1039 </varlistentry>
1041 <varlistentry>
1042 <term><option>--load-via-partition-root</option></term>
1043 <listitem>
1044 <para>
1045 When dumping data for a table partition, make
1046 the <command>COPY</command> or <command>INSERT</command> statements
1047 target the root of the partitioning hierarchy that contains it, rather
1048 than the partition itself. This causes the appropriate partition to
1049 be re-determined for each row when the data is loaded. This may be
1050 useful when restoring data on a server where rows do not always fall
1051 into the same partitions as they did on the original server. That
1052 could happen, for example, if the partitioning column is of type text
1053 and the two systems have different definitions of the collation used
1054 to sort the partitioning column.
1055 </para>
1056 </listitem>
1057 </varlistentry>
1059 <varlistentry>
1060 <term><option>--lock-wait-timeout=<replaceable class="parameter">timeout</replaceable></option></term>
1061 <listitem>
1062 <para>
1063 Do not wait forever to acquire shared table locks at the beginning of
1064 the dump. Instead fail if unable to lock a table within the specified
1065 <replaceable class="parameter">timeout</replaceable>. The timeout may be
1066 specified in any of the formats accepted by <command>SET
1067 statement_timeout</command>. (Allowed formats vary depending on the server
1068 version you are dumping from, but an integer number of milliseconds
1069 is accepted by all versions.)
1070 </para>
1071 </listitem>
1072 </varlistentry>
1074 <varlistentry>
1075 <term><option>--no-comments</option></term>
1076 <listitem>
1077 <para>
1078 Do not dump <command>COMMENT</command> commands.
1079 </para>
1080 </listitem>
1081 </varlistentry>
1083 <varlistentry>
1084 <term><option>--no-publications</option></term>
1085 <listitem>
1086 <para>
1087 Do not dump publications.
1088 </para>
1089 </listitem>
1090 </varlistentry>
1092 <varlistentry>
1093 <term><option>--no-security-labels</option></term>
1094 <listitem>
1095 <para>
1096 Do not dump security labels.
1097 </para>
1098 </listitem>
1099 </varlistentry>
1101 <varlistentry>
1102 <term><option>--no-subscriptions</option></term>
1103 <listitem>
1104 <para>
1105 Do not dump subscriptions.
1106 </para>
1107 </listitem>
1108 </varlistentry>
1110 <varlistentry>
1111 <term><option>--no-sync</option></term>
1112 <listitem>
1113 <para>
1114 By default, <command>pg_dump</command> will wait for all files
1115 to be written safely to disk. This option causes
1116 <command>pg_dump</command> to return without waiting, which is
1117 faster, but means that a subsequent operating system crash can leave
1118 the dump corrupt. Generally, this option is useful for testing
1119 but should not be used when dumping data from production installation.
1120 </para>
1121 </listitem>
1122 </varlistentry>
1124 <varlistentry>
1125 <term><option>--no-table-access-method</option></term>
1126 <listitem>
1127 <para>
1128 Do not output commands to select table access methods.
1129 With this option, all objects will be created with whichever
1130 table access method is the default during restore.
1131 </para>
1133 <para>
1134 This option is ignored when emitting an archive (non-text) output
1135 file. For the archive formats, you can specify the option when you
1136 call <command>pg_restore</command>.
1137 </para>
1138 </listitem>
1139 </varlistentry>
1141 <varlistentry>
1142 <term><option>--no-tablespaces</option></term>
1143 <listitem>
1144 <para>
1145 Do not output commands to select tablespaces.
1146 With this option, all objects will be created in whichever
1147 tablespace is the default during restore.
1148 </para>
1150 <para>
1151 This option is ignored when emitting an archive (non-text) output
1152 file. For the archive formats, you can specify the option when you
1153 call <command>pg_restore</command>.
1154 </para>
1155 </listitem>
1156 </varlistentry>
1158 <varlistentry>
1159 <term><option>--no-toast-compression</option></term>
1160 <listitem>
1161 <para>
1162 Do not output commands to set <acronym>TOAST</acronym> compression
1163 methods.
1164 With this option, all columns will be restored with the default
1165 compression setting.
1166 </para>
1167 </listitem>
1168 </varlistentry>
1170 <varlistentry>
1171 <term><option>--no-unlogged-table-data</option></term>
1172 <listitem>
1173 <para>
1174 Do not dump the contents of unlogged tables and sequences. This
1175 option has no effect on whether or not the table and sequence
1176 definitions (schema) are dumped; it only suppresses dumping the table
1177 and sequence data. Data in unlogged tables and sequences
1178 is always excluded when dumping from a standby server.
1179 </para>
1180 </listitem>
1181 </varlistentry>
1183 <varlistentry>
1184 <term><option>--on-conflict-do-nothing</option></term>
1185 <listitem>
1186 <para>
1187 Add <literal>ON CONFLICT DO NOTHING</literal> to
1188 <command>INSERT</command> commands.
1189 This option is not valid unless <option>--inserts</option>,
1190 <option>--column-inserts</option> or
1191 <option>--rows-per-insert</option> is also specified.
1192 </para>
1193 </listitem>
1194 </varlistentry>
1196 <varlistentry>
1197 <term><option>--quote-all-identifiers</option></term>
1198 <listitem>
1199 <para>
1200 Force quoting of all identifiers. This option is recommended when
1201 dumping a database from a server whose <productname>PostgreSQL</productname>
1202 major version is different from <application>pg_dump</application>'s, or when
1203 the output is intended to be loaded into a server of a different
1204 major version. By default, <application>pg_dump</application> quotes only
1205 identifiers that are reserved words in its own major version.
1206 This sometimes results in compatibility issues when dealing with
1207 servers of other versions that may have slightly different sets
1208 of reserved words. Using <option>--quote-all-identifiers</option> prevents
1209 such issues, at the price of a harder-to-read dump script.
1210 </para>
1211 </listitem>
1212 </varlistentry>
1214 <varlistentry>
1215 <term><option>--rows-per-insert=<replaceable class="parameter">nrows</replaceable></option></term>
1216 <listitem>
1217 <para>
1218 Dump data as <command>INSERT</command> commands (rather than
1219 <command>COPY</command>). Controls the maximum number of rows per
1220 <command>INSERT</command> command. The value specified must be a
1221 number greater than zero. Any error during restoring will cause only
1222 rows that are part of the problematic <command>INSERT</command> to be
1223 lost, rather than the entire table contents.
1224 </para>
1225 </listitem>
1226 </varlistentry>
1228 <varlistentry>
1229 <term><option>--section=<replaceable class="parameter">sectionname</replaceable></option></term>
1230 <listitem>
1231 <para>
1232 Only dump the named section. The section name can be
1233 <option>pre-data</option>, <option>data</option>, or <option>post-data</option>.
1234 This option can be specified more than once to select multiple
1235 sections. The default is to dump all sections.
1236 </para>
1237 <para>
1238 The data section contains actual table data, large-object
1239 contents, and sequence values.
1240 Post-data items include definitions of indexes, triggers, rules,
1241 and constraints other than validated check constraints.
1242 Pre-data items include all other data definition items.
1243 </para>
1244 </listitem>
1245 </varlistentry>
1247 <varlistentry>
1248 <term><option>--serializable-deferrable</option></term>
1249 <listitem>
1250 <para>
1251 Use a <literal>serializable</literal> transaction for the dump, to
1252 ensure that the snapshot used is consistent with later database
1253 states; but do this by waiting for a point in the transaction stream
1254 at which no anomalies can be present, so that there isn't a risk of
1255 the dump failing or causing other transactions to roll back with a
1256 <literal>serialization_failure</literal>. See <xref linkend="mvcc"/>
1257 for more information about transaction isolation and concurrency
1258 control.
1259 </para>
1261 <para>
1262 This option is not beneficial for a dump which is intended only for
1263 disaster recovery. It could be useful for a dump used to load a
1264 copy of the database for reporting or other read-only load sharing
1265 while the original database continues to be updated. Without it the
1266 dump may reflect a state which is not consistent with any serial
1267 execution of the transactions eventually committed. For example, if
1268 batch processing techniques are used, a batch may show as closed in
1269 the dump without all of the items which are in the batch appearing.
1270 </para>
1272 <para>
1273 This option will make no difference if there are no read-write
1274 transactions active when pg_dump is started. If read-write
1275 transactions are active, the start of the dump may be delayed for an
1276 indeterminate length of time. Once running, performance with or
1277 without the switch is the same.
1278 </para>
1279 </listitem>
1280 </varlistentry>
1282 <varlistentry>
1283 <term><option>--snapshot=<replaceable class="parameter">snapshotname</replaceable></option></term>
1284 <listitem>
1285 <para>
1286 Use the specified synchronized snapshot when making a dump of the
1287 database (see
1288 <xref linkend="functions-snapshot-synchronization-table"/> for more
1289 details).
1290 </para>
1291 <para>
1292 This option is useful when needing to synchronize the dump with
1293 a logical replication slot (see <xref linkend="logicaldecoding"/>)
1294 or with a concurrent session.
1295 </para>
1296 <para>
1297 In the case of a parallel dump, the snapshot name defined by this
1298 option is used rather than taking a new snapshot.
1299 </para>
1300 </listitem>
1301 </varlistentry>
1303 <varlistentry>
1304 <term><option>--strict-names</option></term>
1305 <listitem>
1306 <para>
1307 Require that each
1308 extension (<option>-e</option>/<option>--extension</option>),
1309 schema (<option>-n</option>/<option>--schema</option>) and
1310 table (<option>-t</option>/<option>--table</option>) pattern
1311 match at least one extension/schema/table in the database to be dumped.
1312 This also applies to filters used with <option>--filter</option>.
1313 Note that if none of the extension/schema/table patterns find
1314 matches, <application>pg_dump</application> will generate an error
1315 even without <option>--strict-names</option>.
1316 </para>
1317 <para>
1318 This option has no effect
1319 on <option>--exclude-extension</option>,
1320 <option>-N</option>/<option>--exclude-schema</option>,
1321 <option>-T</option>/<option>--exclude-table</option>,
1322 or <option>--exclude-table-data</option>. An exclude pattern failing
1323 to match any objects is not considered an error.
1324 </para>
1325 </listitem>
1326 </varlistentry>
1328 <varlistentry>
1329 <term><option>--sync-method=<replaceable class="parameter">method</replaceable></option></term>
1330 <listitem>
1331 <para>
1332 When set to <literal>fsync</literal>, which is the default,
1333 <command>pg_dump --format=directory</command> will recursively open and
1334 synchronize all files in the archive directory.
1335 </para>
1336 <para>
1337 On Linux, <literal>syncfs</literal> may be used instead to ask the
1338 operating system to synchronize the whole file system that contains the
1339 archive directory. See <xref linkend="guc-recovery-init-sync-method"/>
1340 for information about the caveats to be aware of when using
1341 <literal>syncfs</literal>.
1342 </para>
1343 <para>
1344 This option has no effect when <option>--no-sync</option> is used or
1345 <option>--format</option> is not set to <literal>directory</literal>.
1346 </para>
1347 </listitem>
1348 </varlistentry>
1350 <varlistentry>
1351 <term><option>--table-and-children=<replaceable class="parameter">pattern</replaceable></option></term>
1352 <listitem>
1353 <para>
1354 This is the same as
1355 the <option>-t</option>/<option>--table</option> option,
1356 except that it also includes any partitions or inheritance child
1357 tables of the table(s) matching the
1358 <replaceable class="parameter">pattern</replaceable>.
1359 </para>
1360 </listitem>
1361 </varlistentry>
1363 <varlistentry>
1364 <term><option>--use-set-session-authorization</option></term>
1365 <listitem>
1366 <para>
1367 Output SQL-standard <command>SET SESSION AUTHORIZATION</command> commands
1368 instead of <command>ALTER OWNER</command> commands to determine object
1369 ownership. This makes the dump more standards-compatible, but
1370 depending on the history of the objects in the dump, might not restore
1371 properly. Also, a dump using <command>SET SESSION AUTHORIZATION</command>
1372 will certainly require superuser privileges to restore correctly,
1373 whereas <command>ALTER OWNER</command> requires lesser privileges.
1374 </para>
1375 </listitem>
1376 </varlistentry>
1378 <varlistentry>
1379 <term><option>-?</option></term>
1380 <term><option>--help</option></term>
1381 <listitem>
1382 <para>
1383 Show help about <application>pg_dump</application> command line
1384 arguments, and exit.
1385 </para>
1386 </listitem>
1387 </varlistentry>
1389 </variablelist>
1390 </para>
1392 <para>
1393 The following command-line options control the database connection parameters.
1395 <variablelist>
1396 <varlistentry>
1397 <term><option>-d <replaceable class="parameter">dbname</replaceable></option></term>
1398 <term><option>--dbname=<replaceable class="parameter">dbname</replaceable></option></term>
1399 <listitem>
1400 <para>
1401 Specifies the name of the database to connect to. This is
1402 equivalent to specifying <replaceable
1403 class="parameter">dbname</replaceable> as the first non-option
1404 argument on the command line. The <replaceable>dbname</replaceable>
1405 can be a <link linkend="libpq-connstring">connection string</link>.
1406 If so, connection string parameters will override any conflicting
1407 command line options.
1408 </para>
1409 </listitem>
1410 </varlistentry>
1412 <varlistentry>
1413 <term><option>-h <replaceable class="parameter">host</replaceable></option></term>
1414 <term><option>--host=<replaceable class="parameter">host</replaceable></option></term>
1415 <listitem>
1416 <para>
1417 Specifies the host name of the machine on which the server is
1418 running. If the value begins with a slash, it is used as the
1419 directory for the Unix domain socket. The default is taken
1420 from the <envar>PGHOST</envar> environment variable, if set,
1421 else a Unix domain socket connection is attempted.
1422 </para>
1423 </listitem>
1424 </varlistentry>
1426 <varlistentry>
1427 <term><option>-p <replaceable class="parameter">port</replaceable></option></term>
1428 <term><option>--port=<replaceable class="parameter">port</replaceable></option></term>
1429 <listitem>
1430 <para>
1431 Specifies the TCP port or local Unix domain socket file
1432 extension on which the server is listening for connections.
1433 Defaults to the <envar>PGPORT</envar> environment variable, if
1434 set, or a compiled-in default.
1435 </para>
1436 </listitem>
1437 </varlistentry>
1439 <varlistentry>
1440 <term><option>-U <replaceable>username</replaceable></option></term>
1441 <term><option>--username=<replaceable class="parameter">username</replaceable></option></term>
1442 <listitem>
1443 <para>
1444 User name to connect as.
1445 </para>
1446 </listitem>
1447 </varlistentry>
1449 <varlistentry>
1450 <term><option>-w</option></term>
1451 <term><option>--no-password</option></term>
1452 <listitem>
1453 <para>
1454 Never issue a password prompt. If the server requires
1455 password authentication and a password is not available by
1456 other means such as a <filename>.pgpass</filename> file, the
1457 connection attempt will fail. This option can be useful in
1458 batch jobs and scripts where no user is present to enter a
1459 password.
1460 </para>
1461 </listitem>
1462 </varlistentry>
1464 <varlistentry>
1465 <term><option>-W</option></term>
1466 <term><option>--password</option></term>
1467 <listitem>
1468 <para>
1469 Force <application>pg_dump</application> to prompt for a
1470 password before connecting to a database.
1471 </para>
1473 <para>
1474 This option is never essential, since
1475 <application>pg_dump</application> will automatically prompt
1476 for a password if the server demands password authentication.
1477 However, <application>pg_dump</application> will waste a
1478 connection attempt finding out that the server wants a password.
1479 In some cases it is worth typing <option>-W</option> to avoid the extra
1480 connection attempt.
1481 </para>
1482 </listitem>
1483 </varlistentry>
1485 <varlistentry>
1486 <term><option>--role=<replaceable class="parameter">rolename</replaceable></option></term>
1487 <listitem>
1488 <para>
1489 Specifies a role name to be used to create the dump.
1490 This option causes <application>pg_dump</application> to issue a
1491 <command>SET ROLE</command> <replaceable class="parameter">rolename</replaceable>
1492 command after connecting to the database. It is useful when the
1493 authenticated user (specified by <option>-U</option>) lacks privileges
1494 needed by <application>pg_dump</application>, but can switch to a role with
1495 the required rights. Some installations have a policy against
1496 logging in directly as a superuser, and use of this option allows
1497 dumps to be made without violating the policy.
1498 </para>
1499 </listitem>
1500 </varlistentry>
1501 </variablelist>
1502 </para>
1503 </refsect1>
1505 <refsect1>
1506 <title>Environment</title>
1508 <variablelist>
1509 <varlistentry>
1510 <term><envar>PGDATABASE</envar></term>
1511 <term><envar>PGHOST</envar></term>
1512 <term><envar>PGOPTIONS</envar></term>
1513 <term><envar>PGPORT</envar></term>
1515 <term><envar>PGUSER</envar></term>
1516 <listitem>
1517 <para>
1518 Default connection parameters.
1519 </para>
1520 </listitem>
1522 </varlistentry>
1524 <varlistentry>
1525 <term><envar>PG_COLOR</envar></term>
1526 <listitem>
1527 <para>
1528 Specifies whether to use color in diagnostic messages. Possible values
1529 are <literal>always</literal>, <literal>auto</literal> and
1530 <literal>never</literal>.
1531 </para>
1532 </listitem>
1533 </varlistentry>
1534 </variablelist>
1536 <para>
1537 This utility, like most other <productname>PostgreSQL</productname> utilities,
1538 also uses the environment variables supported by <application>libpq</application>
1539 (see <xref linkend="libpq-envars"/>).
1540 </para>
1542 </refsect1>
1544 <refsect1 id="app-pgdump-diagnostics">
1545 <title>Diagnostics</title>
1547 <para>
1548 <application>pg_dump</application> internally executes
1549 <command>SELECT</command> statements. If you have problems running
1550 <application>pg_dump</application>, make sure you are able to
1551 select information from the database using, for example, <xref
1552 linkend="app-psql"/>. Also, any default connection settings and environment
1553 variables used by the <application>libpq</application> front-end
1554 library will apply.
1555 </para>
1557 <para>
1558 The database activity of <application>pg_dump</application> is
1559 normally collected by the cumulative statistics system. If this is
1560 undesirable, you can set parameter <varname>track_counts</varname>
1561 to false via <envar>PGOPTIONS</envar> or the <literal>ALTER
1562 USER</literal> command.
1563 </para>
1565 </refsect1>
1568 <refsect1 id="pg-dump-notes">
1569 <title>Notes</title>
1571 <para>
1572 If your database cluster has any local additions to the <literal>template1</literal> database,
1573 be careful to restore the output of <application>pg_dump</application> into a
1574 truly empty database; otherwise you are likely to get errors due to
1575 duplicate definitions of the added objects. To make an empty database
1576 without any local additions, copy from <literal>template0</literal> not <literal>template1</literal>,
1577 for example:
1578 <programlisting>
1579 CREATE DATABASE foo WITH TEMPLATE template0;
1580 </programlisting>
1581 </para>
1583 <para>
1584 When a data-only dump is chosen and the option <option>--disable-triggers</option>
1585 is used, <application>pg_dump</application> emits commands
1586 to disable triggers on user tables before inserting the data,
1587 and then commands to re-enable them after the data has been
1588 inserted. If the restore is stopped in the middle, the system
1589 catalogs might be left in the wrong state.
1590 </para>
1592 <para>
1593 The dump file produced by <application>pg_dump</application>
1594 does not contain the statistics used by the optimizer to make
1595 query planning decisions. Therefore, it is wise to run
1596 <command>ANALYZE</command> after restoring from a dump file
1597 to ensure optimal performance; see <xref linkend="vacuum-for-statistics"/>
1598 and <xref linkend="autovacuum"/> for more information.
1599 </para>
1601 <para>
1602 Because <application>pg_dump</application> is used to transfer data
1603 to newer versions of <productname>PostgreSQL</productname>, the output of
1604 <application>pg_dump</application> can be expected to load into
1605 <productname>PostgreSQL</productname> server versions newer than
1606 <application>pg_dump</application>'s version. <application>pg_dump</application> can also
1607 dump from <productname>PostgreSQL</productname> servers older than its own version.
1608 (Currently, servers back to version 9.2 are supported.)
1609 However, <application>pg_dump</application> cannot dump from
1610 <productname>PostgreSQL</productname> servers newer than its own major version;
1611 it will refuse to even try, rather than risk making an invalid dump.
1612 Also, it is not guaranteed that <application>pg_dump</application>'s output can
1613 be loaded into a server of an older major version &mdash; not even if the
1614 dump was taken from a server of that version. Loading a dump file
1615 into an older server may require manual editing of the dump file
1616 to remove syntax not understood by the older server.
1617 Use of the <option>--quote-all-identifiers</option> option is recommended
1618 in cross-version cases, as it can prevent problems arising from varying
1619 reserved-word lists in different <productname>PostgreSQL</productname> versions.
1620 </para>
1622 <para>
1623 When dumping logical replication subscriptions,
1624 <application>pg_dump</application> will generate <command>CREATE
1625 SUBSCRIPTION</command> commands that use the <literal>connect = false</literal>
1626 option, so that restoring the subscription does not make remote connections
1627 for creating a replication slot or for initial table copy. That way, the
1628 dump can be restored without requiring network access to the remote
1629 servers. It is then up to the user to reactivate the subscriptions in a
1630 suitable way. If the involved hosts have changed, the connection
1631 information might have to be changed. It might also be appropriate to
1632 truncate the target tables before initiating a new full table copy. If users
1633 intend to copy initial data during refresh they must create the slot with
1634 <literal>two_phase = false</literal>. After the initial sync, the
1635 <link linkend="sql-createsubscription-params-with-two-phase"><literal>two_phase</literal></link>
1636 option will be automatically enabled by the subscriber if the subscription
1637 had been originally created with <literal>two_phase = true</literal> option.
1638 </para>
1639 </refsect1>
1641 <refsect1 id="pg-dump-examples" xreflabel="Examples">
1642 <title>Examples</title>
1644 <para>
1645 To dump a database called <literal>mydb</literal> into an SQL-script file:
1646 <screen>
1647 <prompt>$</prompt> <userinput>pg_dump mydb &gt; db.sql</userinput>
1648 </screen>
1649 </para>
1651 <para>
1652 To reload such a script into a (freshly created) database named
1653 <literal>newdb</literal>:
1655 <screen>
1656 <prompt>$</prompt> <userinput>psql -d newdb -f db.sql</userinput>
1657 </screen>
1658 </para>
1660 <para>
1661 To dump a database into a custom-format archive file:
1663 <screen>
1664 <prompt>$</prompt> <userinput>pg_dump -Fc mydb &gt; db.dump</userinput>
1665 </screen>
1666 </para>
1668 <para>
1669 To dump a database into a directory-format archive:
1671 <screen>
1672 <prompt>$</prompt> <userinput>pg_dump -Fd mydb -f dumpdir</userinput>
1673 </screen>
1674 </para>
1676 <para>
1677 To dump a database into a directory-format archive in parallel with
1678 5 worker jobs:
1680 <screen>
1681 <prompt>$</prompt> <userinput>pg_dump -Fd mydb -j 5 -f dumpdir</userinput>
1682 </screen>
1683 </para>
1685 <para>
1686 To reload an archive file into a (freshly created) database named
1687 <literal>newdb</literal>:
1689 <screen>
1690 <prompt>$</prompt> <userinput>pg_restore -d newdb db.dump</userinput>
1691 </screen>
1692 </para>
1694 <para>
1695 To reload an archive file into the same database it was dumped from,
1696 discarding the current contents of that database:
1698 <screen>
1699 <prompt>$</prompt> <userinput>pg_restore -d postgres --clean --create db.dump</userinput>
1700 </screen>
1701 </para>
1703 <para>
1704 To dump a single table named <literal>mytab</literal>:
1706 <screen>
1707 <prompt>$</prompt> <userinput>pg_dump -t mytab mydb &gt; db.sql</userinput>
1708 </screen>
1709 </para>
1711 <para>
1712 To dump all tables whose names start with <literal>emp</literal> in the
1713 <literal>detroit</literal> schema, except for the table named
1714 <literal>employee_log</literal>:
1716 <screen>
1717 <prompt>$</prompt> <userinput>pg_dump -t 'detroit.emp*' -T detroit.employee_log mydb &gt; db.sql</userinput>
1718 </screen>
1719 </para>
1721 <para>
1722 To dump all schemas whose names start with <literal>east</literal> or
1723 <literal>west</literal> and end in <literal>gsm</literal>, excluding any schemas whose
1724 names contain the word <literal>test</literal>:
1726 <screen>
1727 <prompt>$</prompt> <userinput>pg_dump -n 'east*gsm' -n 'west*gsm' -N '*test*' mydb &gt; db.sql</userinput>
1728 </screen>
1729 </para>
1731 <para>
1732 The same, using regular expression notation to consolidate the switches:
1734 <screen>
1735 <prompt>$</prompt> <userinput>pg_dump -n '(east|west)*gsm' -N '*test*' mydb &gt; db.sql</userinput>
1736 </screen>
1737 </para>
1739 <para>
1740 To dump all database objects except for tables whose names begin with
1741 <literal>ts_</literal>:
1743 <screen>
1744 <prompt>$</prompt> <userinput>pg_dump -T 'ts_*' mydb &gt; db.sql</userinput>
1745 </screen>
1746 </para>
1748 <para>
1749 To specify an upper-case or mixed-case name in <option>-t</option> and related
1750 switches, you need to double-quote the name; else it will be folded to
1751 lower case (see <xref linkend="app-psql-patterns"/>). But
1752 double quotes are special to the shell, so in turn they must be quoted.
1753 Thus, to dump a single table with a mixed-case name, you need something
1754 like
1756 <screen>
1757 <prompt>$</prompt> <userinput>pg_dump -t "\"MixedCaseName\"" mydb &gt; mytab.sql</userinput>
1758 </screen></para>
1760 <para>
1761 To dump all tables whose names start with <literal>mytable</literal>, except
1762 for table <literal>mytable2</literal>, specify a filter file
1763 <filename>filter.txt</filename> like:
1764 <programlisting>
1765 include table mytable*
1766 exclude table mytable2
1767 </programlisting>
1769 <screen>
1770 <prompt>$</prompt> <userinput>pg_dump --filter=filter.txt mydb &gt; db.sql</userinput>
1771 </screen></para>
1773 </refsect1>
1775 <refsect1>
1776 <title>See Also</title>
1778 <simplelist type="inline">
1779 <member><xref linkend="app-pg-dumpall"/></member>
1780 <member><xref linkend="app-pgrestore"/></member>
1781 <member><xref linkend="app-psql"/></member>
1782 </simplelist>
1783 </refsect1>
1785 </refentry>