Home Explore Blog CI



postgresql

16th chunk of `doc/src/sgml/ref/pg_restore.sgml`
0b197afb0839ae92e2f2e20868cc195f396779ea6b19357f0000000100000dad
 restore optimizer statistics
   if included in the dump file.  If not all statistics were restored, it may
   be useful to run <command>ANALYZE</command> on each restored table so the
   optimizer has useful statistics; see <xref
   linkend="vacuum-for-statistics"/> and <xref linkend="autovacuum"/> for more
   information.
  </para>

 </refsect1>


 <refsect1 id="app-pgrestore-examples">
  <title>Examples</title>

  <para>
   Assume we have dumped a database called <literal>mydb</literal> into a
   custom-format dump file:

<screen>
<prompt>$</prompt> <userinput>pg_dump -Fc mydb &gt; db.dump</userinput>
</screen>
  </para>

  <para>
   To drop the database and recreate it from the dump:

<screen>
<prompt>$</prompt> <userinput>dropdb mydb</userinput>
<prompt>$</prompt> <userinput>pg_restore -C -d postgres db.dump</userinput>
</screen>

   The database named in the <option>-d</option> switch can be any database existing
   in the cluster; <application>pg_restore</application> only uses it to issue the
   <command>CREATE DATABASE</command> command for <literal>mydb</literal>.  With
   <option>-C</option>, data is always restored into the database name that appears
   in the dump file.
  </para>

  <para>
   To restore the dump into a new database called <literal>newdb</literal>:

<screen>
<prompt>$</prompt> <userinput>createdb -T template0 newdb</userinput>
<prompt>$</prompt> <userinput>pg_restore -d newdb db.dump</userinput>
</screen>

   Notice we don't use <option>-C</option>, and instead connect directly to the
   database to be restored into.  Also note that we clone the new database
   from <literal>template0</literal> not <literal>template1</literal>, to ensure it is
   initially empty.
  </para>

  <para>
   To reorder database items, it is first necessary to dump the table of
   contents of the archive:
<screen>
<prompt>$</prompt> <userinput>pg_restore -l db.dump &gt; db.list</userinput>
</screen>
   The listing file consists of a header and one line for each item, e.g.:
<programlisting>
;
; Archive created at Mon Sep 14 13:55:39 2009
;     dbname: DBDEMOS
;     TOC Entries: 81
;     Compression: 9
;     Dump Version: 1.10-0
;     Format: CUSTOM
;     Integer: 4 bytes
;     Offset: 8 bytes
;     Dumped from database version: 8.3.5
;     Dumped by pg_dump version: 8.3.8
;
;
; Selected TOC Entries:
;
3; 2615 2200 SCHEMA - public pasha
1861; 0 0 COMMENT - SCHEMA public pasha
1862; 0 0 ACL - public pasha
317; 1247 17715 TYPE public composite pasha
319; 1247 25899 DOMAIN public domain0 pasha
</programlisting>
   Semicolons start a comment, and the numbers at the start of lines refer to the
   internal archive ID assigned to each item.
  </para>

  <para>
   Lines in the file can be commented out, deleted, and reordered. For example:
<programlisting>
10; 145433 TABLE map_resolutions postgres
;2; 145344 TABLE species postgres
;4; 145359 TABLE nt_header postgres
6; 145402 TABLE species_records postgres
;8; 145416 TABLE ss_old postgres
</programlisting>
   could be used as input to <application>pg_restore</application> and would only restore
   items 10 and 6, in that order:
<screen>
<prompt>$</prompt> <userinput>pg_restore -L db.list db.dump</userinput>
</screen></para>

 </refsect1>

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

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

Title: pg_restore Examples
Summary
This section provides practical examples of using pg_restore, including restoring a database from a dump file, creating a new database from a dump, and reordering database items by manipulating the table of contents of the archive. It also references related tools like pg_dump, pg_dumpall, and psql.