Home Explore Blog CI



postgresql

26th chunk of `doc/src/sgml/ref/pg_dump.sgml`
b92f45fd0714070a92771c7a1e1e608b7278d47966034e430000000100000c98

<prompt>$</prompt> <userinput>pg_dump -Fd mydb -f dumpdir</userinput>
</screen>
  </para>

  <para>
   To dump a database into a directory-format archive in parallel with
   5 worker jobs:

<screen>
<prompt>$</prompt> <userinput>pg_dump -Fd mydb -j 5 -f dumpdir</userinput>
</screen>
  </para>

  <para>
   To reload an archive file into a (freshly created) database named
   <literal>newdb</literal>:

<screen>
<prompt>$</prompt> <userinput>pg_restore -d newdb db.dump</userinput>
</screen>
  </para>

  <para>
   To reload an archive file into the same database it was dumped from,
   discarding the current contents of that database:

<screen>
<prompt>$</prompt> <userinput>pg_restore -d postgres --clean --create db.dump</userinput>
</screen>
  </para>

  <para>
   To dump a single table named <literal>mytab</literal>:

<screen>
<prompt>$</prompt> <userinput>pg_dump -t mytab mydb &gt; db.sql</userinput>
</screen>
  </para>

  <para>
   To dump all tables whose names start with <literal>emp</literal> in the
   <literal>detroit</literal> schema, except for the table named
   <literal>employee_log</literal>:

<screen>
<prompt>$</prompt> <userinput>pg_dump -t 'detroit.emp*' -T detroit.employee_log mydb &gt; db.sql</userinput>
</screen>
  </para>

  <para>
   To dump all schemas whose names start with <literal>east</literal> or
   <literal>west</literal> and end in <literal>gsm</literal>, excluding any schemas whose
   names contain the word <literal>test</literal>:

<screen>
<prompt>$</prompt> <userinput>pg_dump -n 'east*gsm' -n 'west*gsm' -N '*test*' mydb &gt; db.sql</userinput>
</screen>
  </para>

  <para>
   The same, using regular expression notation to consolidate the switches:

<screen>
<prompt>$</prompt> <userinput>pg_dump -n '(east|west)*gsm' -N '*test*' mydb &gt; db.sql</userinput>
</screen>
  </para>

  <para>
   To dump all database objects except for tables whose names begin with
   <literal>ts_</literal>:

<screen>
<prompt>$</prompt> <userinput>pg_dump -T 'ts_*' mydb &gt; db.sql</userinput>
</screen>
  </para>

  <para>
   To specify an upper-case or mixed-case name in <option>-t</option> and related
   switches, you need to double-quote the name; else it will be folded to
   lower case (see <xref linkend="app-psql-patterns"/>).  But
   double quotes are special to the shell, so in turn they must be quoted.
   Thus, to dump a single table with a mixed-case name, you need something
   like

<screen>
<prompt>$</prompt> <userinput>pg_dump -t "\"MixedCaseName\"" mydb &gt; mytab.sql</userinput>
</screen></para>

  <para>
   To dump all tables whose names start with <literal>mytable</literal>, except
   for table <literal>mytable2</literal>, specify a filter file
   <filename>filter.txt</filename> like:
<programlisting>
include table mytable*
exclude table mytable2
</programlisting>

<screen>
<prompt>$</prompt> <userinput>pg_dump --filter=filter.txt mydb &gt; db.sql</userinput>
</screen></para>

 </refsect1>

 <refsect1>
  <title>See Also</title>

  <simplelist type="inline">
   <member><xref linkend="app-pg-dumpall"/></member>
   <member><xref linkend="app-pgrestore"/></member>
   <member><xref linkend="app-psql"/></member>
  </simplelist>
 </refsect1>

</refentry>

Title: pg_dump Examples: Advanced Table and Schema Selection
Summary
This section provides more advanced examples of using pg_dump, including parallel directory-format dumps, selective table and schema dumping with wildcards and regular expressions, excluding specific tables or schemas, and handling mixed-case table names. It also demonstrates using a filter file for complex inclusion/exclusion rules and refers to related utilities like pg_dumpall, pg_restore, and psql.