pg_restore.sgml 20.9 KB
Newer Older
1
<!-- $Header: /cvsroot/pgsql/doc/src/sgml/ref/pg_restore.sgml,v 1.14 2001/08/22 20:23:23 petere Exp $ -->
2

3
<refentry id="APP-PGRESTORE">
4 5 6 7
 <docinfo>
  <date>2001-03-06</date>
 </docinfo>

8
 <refmeta>
9 10
  <refentrytitle>
   pg_restore
11
  </refentrytitle>
12
  <manvolnum>1</manvolnum>
13 14
  <refmiscinfo>Application</refmiscinfo>
 </refmeta>
15

16
 <refnamediv>
17 18
  <refname>pg_restore</refname>

19
  <refpurpose>
20
   Restore a <PRODUCTNAME>Postgres</PRODUCTNAME> database from an archive file created by
21
<command>pg_dump</command>
22 23
  </refpurpose>
 </refnamediv>
24

25
 <refsynopsisdiv>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  <cmdsynopsis>
   <command>pg_restore</command>
   <arg> -a </arg>
   <arg> -c </arg>
   <arg> -C </arg>
   <arg> -d <replaceable class="parameter">dbname</replaceable> </arg>
   <arg> -f <replaceable class="parameter">archive-file</replaceable> </arg>
   <arg> -F <replaceable class="parameter">format</replaceable> </arg>
   <arg> -i  <replaceable class="parameter">index</replaceable> </arg>
   <arg> -l </arg>
   <arg> -L <replaceable class="parameter">contents-file</replaceable> </arg>
   <group> <arg> -N </arg> <arg> -o </arg> <arg> -r </arg> </group>
   <arg> -O </arg>
   <arg> -P <replaceable class="parameter">function-name</replaceable> </arg>
   <arg> -R </arg>
   <arg> -s </arg>
   <arg> -S </arg>
   <arg> -t  <replaceable class="parameter">table</replaceable> </arg>
   <arg> -T  <replaceable class="parameter">trigger</replaceable> </arg>
   <arg> -v </arg>
   <arg> -x </arg>
47
   <arg> -X <replaceable>keyword</replaceable></arg>
48 49
   <arg> -h  <replaceable class="parameter">host</replaceable> </arg>
   <arg> -p  <replaceable class="parameter">port</replaceable> </arg>
50 51
   <arg> -U <replaceable>username</replaceable> </arg>
   <arg> -W </arg>
52 53 54 55 56 57 58 59 60 61 62 63
   <arg> <replaceable class="parameter">archive-file</replaceable> </arg>
  </cmdsynopsis>
 </refsynopsisdiv>


 <refsect1 id="app-pgrestore-description">
  <title>
   Description
  </title>

  <para>
   <command>pg_restore</command> is a utility for restoring a
64 65 66
   <productname>Postgres</productname> database from an archive
   created by <xref linkend="app-pgdump"> in one of the non-plain-text
   formats.
67 68 69
  </para>

  <para>
70 71 72 73 74 75 76 77
   The archive files contain information for
   <command>pg_restore</command> to rebuild the database, but also
   allow <command>pg_restore</command> to be selective about what is
   restored, or even to reorder the items prior to being restored. The
   archive files are designed to be portable across architectures.  It
   will issue the commands necessary to re-generate all user-defined
   types, functions, tables, indexes, aggregates, and operators, as
   well as the data in the tables.
78 79 80
  </para>

  <para>
81 82 83 84 85 86 87 88
   <command>pg_restore</command> can operate in two modes:  If a
   database name is specified, the archive is restored directly into
   the database.  Otherwise, a script containing the SQL commands
   necessary to rebuild the database is created (and written to a file
   or standard output), similar to the ones created by the
   <command>pg_dump</command> plain text format.  Some of the options
   controlling the script output are therefore analogous to
   <command>pg_dump</command>.
89 90 91
  </para>

  <para>
92 93 94 95 96 97
   Obviously, <command>pg_restore</command> cannot restore information
   that is not present in the archive file; for instance, if the
   archive was made using the <quote>dump data as
   <command>INSERT</command>s</quote> option,
   <command>pg_restore</command> will not be able to load the data
   using <command>COPY</command> statements.
98 99 100
  </para>

  <para>
101 102 103 104 105 106 107 108 109 110 111
   The most flexible output file format is the <quote>custom</quote>
   format (<option>-Fc</option>). It allows for selection and
   reordering of all archived items, and is compressed by default. The
   <filename>tar</filename> format (<option>-Ft</option>) is not
   compressed and it is not possible to reorder data when loading, but
   it is otherwise quite flexible.
  </para>

  <para>
   To reorder the items, it is first necessary to dump the table of
   contents of the archive:
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
<screen>
<prompt>$</prompt> <userinput>pg_restore archive.file -l &gt; archive.list</userinput>
</screen>
   This file consists of a header and one line for each item, e.g.,
<programlisting>
;
; Archive created at Fri Jul 28 22:28:36 2000
;     dbname: birds
;     TOC Entries: 74
;     Compression: 0
;     Dump Version: 1.4-0
;     Format: CUSTOM
;
;
; Selected TOC Entries:
;
2; 145344 TABLE species postgres
3; 145344 ACL species
4; 145359 TABLE nt_header postgres
5; 145359 ACL nt_header
6; 145402 TABLE species_records postgres
7; 145402 ACL species_records
8; 145416 TABLE ss_old postgres
9; 145416 ACL ss_old
10; 145433 TABLE map_resolutions postgres
11; 145433 ACL map_resolutions
12; 145443 TABLE hs_old postgres
13; 145443 ACL hs_old
</programlisting>
   Semi-colons are comment delimiters, 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 <command>pg_restore</command> and would only restore
   items 10 and 6, in that order.
<screen>
<prompt>$</prompt> <userinput>pg_restore archive.file -L archive.list</userinput>
</screen>
  </para>

  <refsect2 id="app-pgrestore-options">
162
   <title>
163
    Options
164
   </title>
165

166
   <para>
167 168
    <command>pg_restore</command> accepts the following command
    line arguments.  (Long option forms are only available on some platforms.)
169 170 171 172 173 174

    <variablelist>
     <varlistentry>
      <term><replaceable class="parameter">archive-name</replaceable></term>
      <listitem>
       <para>
175
       Specifies the location of the archive file to be restored.
176
       If not specified, and no <option>-f</option> option is specified, then the standard input is used.
177 178 179 180 181 182
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term>-a</term>
183
      <term>--data-only</term>
184 185 186 187 188 189 190
      <listitem>
       <para>
	Restore only the data, no schema (definitions).
       </para>
      </listitem>
     </varlistentry>

191 192
     <varlistentry>
      <term>-c</term>
193
      <term>--clean</term>
194 195 196 197 198 199 200 201 202
      <listitem>
       <para>
	Clean (drop) schema prior to create.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term>-C</term>
203
      <term>--create</term>
204 205 206 207 208 209 210
      <listitem>
       <para>
	Include SQL to create the schema.
       </para>
      </listitem>
     </varlistentry>

211 212
     <varlistentry>
      <term>-d <replaceable class="parameter">dbname</replaceable></term>
213
      <term>--dbname=<replaceable class="parameter">dbname</replaceable></term>
214 215
      <listitem>
       <para>
216 217 218 219 220 221 222
        Connect to database <replaceable class="parameter">dbname</replaceable> and restore
        directly into the database. BLOBs can only be restored by using a direct database connection.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
223 224
      <term>-f <replaceable>filename</replaceable></term>
      <term>--file=<replaceable>filename</replaceable></term>
225 226
      <listitem>
       <para>
227 228
        Specify output file for generated script. (Use with the
        <option>-l</option> option.) Default is the standard output.
229 230 231 232 233 234
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term>-F <replaceable class="parameter">format</replaceable></term>
235
      <term>--format=<replaceable class="parameter">format</replaceable></term>
236 237 238
      <listitem>
       <para>
	  Specify format of the archive.
239
        It is not necessary to specify the format, since <COMMAND>pg_restore</COMMAND> will
240 241 242 243 244 245 246 247 248
        determine the format automatically. If specified, it can be one of the following:
       </para>

       <variablelist>

        <varlistentry>
         <term>t</term>
         <listitem>
          <para>
249
           Archive is a <filename>tar</filename> archive. Using this archive format allows reordering and/or
250 251 252 253 254 255 256 257 258 259
           exclusion of schema elements at the time the database is restored. It is also possible to limit which
           data is reloaded at restore time.
          </para>
         </listitem>
        </varlistentry>

        <varlistentry>
         <term>c</term>
         <listitem>
          <para>
260
           Archive is in the custom format of <command>pg_dump</command>. This is the most flexible format
261 262 263 264 265 266 267 268 269 270 271 272 273
           in that it allows reordering of data load as well as schema elements.
           This format is also compressed by default.
          </para>
         </listitem>
        </varlistentry>

       </variablelist>

      </listitem>
     </varlistentry>

     <varlistentry>
      <term>-i <replaceable class="parameter">index</replaceable></term>
274
      <term>--index=<replaceable class="parameter">index</replaceable></term>
275 276 277 278 279 280 281 282 283
      <listitem>
       <para>
	Restore definition for named <replaceable class="parameter">index</replaceable> only.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term>-l</term>
284
      <term>--list</term>
285 286
      <listitem>
       <para>
287
        List the contents of the archive. The output of this command can be used with the <option>-L</option> option
288
        to restrict and reorder the items that are restored.
289 290 291 292
       </para>
      </listitem>
     </varlistentry>

293 294
     <varlistentry>
      <term>-L <replaceable class="parameter">list-file</replaceable></term>
295
      <term>--use-list=<replaceable class="parameter">list-file</replaceable></term>
296 297
      <listitem>
       <para>
298 299 300
	  Restore elements in <REPLACEABLE CLASS="PARAMETER">list-file</REPLACEABLE> only, and in the
        order they appear in the file. Lines can be moved and may also be commented out by placing a ';' at the
        start of the line.
301 302 303 304
       </para>
      </listitem>
     </varlistentry>

305 306
     <varlistentry>
      <term>-N</term>
307
      <term>--orig-order</term>
308
      <listitem>
309
       <para>
310 311
        Restore items in the original dump order. By default <command>pg_dump</command> will dump items in an order convenient
        to <command>pg_dump</command>, then save the archive in a modified OID order. This option overrides the OID ordering.
312 313 314 315 316 317
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term>-o</term>
318
      <term>--oid-order</term>
319 320
      <listitem>
       <para>
321 322
        Restore items in the OID order. By default <command>pg_dump</command> will dump items in an order convenient
        to <command>pg_dump</command>, then save the archive in a modified OID order. This option enforces strict OID ordering.
323 324 325 326 327 328
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term>-O</term>
329
      <term>--no-owner</term>
330 331
      <listitem>
       <para>
332
        Prevent any attempt to restore original object ownership. Objects will be owned by the user name used
333 334 335 336 337 338
        to attach to the database.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
339 340
      <term>-P <replaceable class="parameter">function-name</replaceable></term>
      <term>--function=<replaceable class="parameter">function-name</replaceable></term>
341 342 343 344 345 346 347 348 349
      <listitem>
       <para>
        Specify a procedure or function to be restored.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term>-r</term>
350
      <term>--rearrange</term>
351 352
      <listitem>
       <para>
353 354
        Restore items in modified OID order. By default <command>pg_dump</command> will dump items in an order convenient
        to <command>pg_dump</command>, then save the archive in a modified OID order. Most objects
355
        will be restored in OID order, but some things (e.g., rules and indexes) will be restored at the end of
356 357 358 359 360 361 362
        the process irrespective of their OIDs. This option is the default.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term>-R</term>
363
      <term>--no-reconnect</term>
364 365
      <listitem>
       <para>
366 367 368 369 370 371 372 373 374 375 376 377 378 379
        While restoring an archive, <command>pg_restore</command>
        typically has to reconnect to the database several times with
        different user names to set the correct ownership of the
        created objects.  If this is undesriable (e.g., because manual
        interaction (passwords) would be necessary for each
        reconnection), this option prevents
        <command>pg_restore</command> from issuing any reconnection
        requests.  (A connection request while in plain text mode, not
        connected to a database, is made by putting out a <xref
        linkend="app-psql"> <command>\connect</command> command.)
        However, this option is a rather blunt instrument because it
        makes <command>pg_restore</command> lose all object ownership
        information, <emphasis>unless</emphasis> you use the
        <option>-X use-set-session-authorization</option> option.
380 381 382 383 384 385
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term>-s</term>
386
      <term>--schema-only</term>
387 388 389
      <listitem>
       <para>
	Restore the schema (definitions), no data. Sequence values will be reset.
390 391 392
       </para>
      </listitem>
     </varlistentry>
393 394 395

     <varlistentry>
      <term>-S <replaceable class="parameter">username</replaceable></term>
396
      <term>--superuser=<replaceable class="parameter">username</replaceable></term>
397 398
      <listitem>
       <para>
399 400
        Specify the superuser user name to use when disabling triggers and/or setting ownership of schema elements.
        By default, <COMMAND>pg_restore</COMMAND> will use the current user name if it is a superuser.
401 402 403 404 405 406
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term>-t <replaceable class="parameter">table</replaceable></term>
407
      <term>--table=<replaceable class="parameter">table</replaceable></term>
408 409 410 411 412 413 414 415 416
      <listitem>
       <para>
	Restore schema/data for <REPLACEABLE CLASS="PARAMETER">table</REPLACEABLE> only.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term>-T <replaceable class="parameter">trigger</replaceable></term>
417
      <term>--trigger=<replaceable class="parameter">trigger</replaceable></term>
418 419 420 421 422 423 424
      <listitem>
       <para>
	  Restore definition of <REPLACEABLE CLASS="PARAMETER">trigger</REPLACEABLE> only.
       </para>
      </listitem>
     </varlistentry>

425 426
     <varlistentry>
      <term>-v</term>
427
      <term>--verbose</term>
428 429 430 431 432 433 434 435 436
      <listitem>
       <para>
	Specifies verbose mode.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term>-x</term>
437
      <term>--no-privileges</term>
438
      <term>--no-acl</term>
439 440
      <listitem>
       <para>
441
	Prevent restoration of access privileges (grant/revoke commands).
442 443 444 445
       </para>
      </listitem>
     </varlistentry>

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
     <varlistentry>
      <term>-X use-set-session-authorization</term>
      <term>--use-set-session-authorization</term>
      <listitem>
       <para>
        Normally, if restoring an archive requires altering the
        current database user (e.g., to set correct object
        ownerships), a new connection to the database must be openend,
        which might require manual interaction (e.g., passwords).  If
        you use the <option>-X use-set-session-authorization</option>,
        then <command>pg_restore</command> will instead use the <xref
        linkend="sql-set-session-authorization"> command.  This has
        the same effect, but it requires that the user restoring the
        archive is a database superuser.  This option effectively
        overrides the <option>-R</option> option.
       </para>
      </listitem>
     </varlistentry>

465 466
    </variablelist>
   </para>
467

468
   <para>
469
    <command>pg_restore</command> also accepts
470 471 472 473 474
    the following command line arguments for connection parameters:

    <variablelist>
     <varlistentry>
      <term>-h <replaceable class="parameter">host</replaceable></term>
475
      <term>--host=<replaceable class="parameter">host</replaceable></term>
476 477
      <listitem>
       <para>
478 479
	Specifies the host name of the machine on which the
	<command>postmaster</command>
480
	is running.  If host begins with a slash, it is used 
481
	as the directory for the Unix domain socket.
482 483 484 485 486 487
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term>-p <replaceable class="parameter">port</replaceable></term>
488
      <term>--port=<replaceable class="parameter">port</replaceable></term>
489 490
      <listitem>
       <para>
491
	Specifies the Internet TCP/IP port or local Unix domain socket file
492
	extension on which the <command>postmaster</command>
493 494 495 496 497 498 499
	is listening for connections.  The port number defaults to 5432,
	or the value of the <envar>PGPORT</envar>
	environment variable (if set).
       </para>
      </listitem>
     </varlistentry>

500
     <varlistentry>
501
      <term>-U <replaceable>username</replaceable></term>
502 503
      <listitem>
       <para>
504 505 506 507 508 509 510 511 512 513 514
        Connect as the given user.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term>-W</term>
      <listitem>
       <para>
        Force a password prompt.  This should happen automatically if
        the server requires password authentication.
515 516 517
       </para>
      </listitem>
     </varlistentry>
518 519 520 521
    </variablelist>
   </para>
  </refsect2>

522
 </refsect1>
523

524 525 526 527 528 529 530 531 532 533

 <refsect1 id="app-pgrestore-diagnostics">
  <title>Diagnostics</title>

  <msgset>
   <msgentry>
    <msg>
     <msgmain>
      <msgtext>
<screen><computeroutput>
534
Connection to database 'template1' failed.
535 536 537
connectDBStart() -- connect() failed: No such file or directory
        Is the postmaster running locally
        and accepting connections on Unix socket '/tmp/.s.PGSQL.5432'?
538 539 540 541 542 543
</computeroutput></screen>
      </msgtext>
     </msgmain>
    </msg>

    <msgexplan>
544
       <para>
545 546
	<command>pg_restore</command> could not attach to the
	<command>postmaster</command>
547
	process on the specified host and port.  If you see this message,
548
	ensure that the <command>postmaster</command>
549 550 551 552
	is running on the proper host and that you have specified the proper
	port.  If your site uses an authentication system, ensure that you
	have obtained the required authentication credentials.
       </para>
553 554 555
    </msgexplan>
   </msgentry>
  </msgset>  
556 557 558

   <note>
    <para>
559
     When a direct database connection is specified using the -d option, <command>pg_restore</command>
560
     internally executes  <command>SQL</command> statements. If you  have problems running
561
     <command>pg_restore</command>,
562
     make sure you are able to select information from the database using, for
563
     example, <command>psql</command>.
564 565 566 567
    </para>
   </note>
 </refsect1>

568 569

 <refsect1 id="app-pgrestore-notes">
570 571 572
  <title>
   Notes
  </title>
573

574
  <para>
575
   The limitations of <command>pg_restore</command> are detailed below.
576 577 578 579

   <itemizedlist>
    <listitem>
     <para>
580
      When restoring data to a table, <command>pg_restore</command> emits queries
581 582
      to disable triggers on user tables before inserting the data then emits queries to
      re-enable them after the data has been inserted.  If the restore is stopped in the
583 584 585 586 587 588
      middle, the system catalogs may be left in the wrong state.
     </para>
    </listitem>

    <listitem>
     <para>
589
      <command>pg_restore</command> will not restore BLOBs for a single table. If
590
      an archive contains BLOBs, then all BLOBs will be restored.
591 592 593 594 595
     </para>
    </listitem>

   </itemizedlist>
  </para>
596 597 598 599 600

  <para>
   See the <xref linkend="app-pgdump"> documentation for details on
   limitation of <command>pg_dump</command>.
  </para>
601 602
 </refsect1>

603 604

 <refsect1 id="app-pgrestore-examples">
605
  <title>
606
   Examples
607 608
  </title>

609 610 611 612 613
  <para>
   To dump a database:
<screen>
<prompt>$</prompt> <userinput>pg_dump mydb &gt; db.out</userinput>
</screen>
614 615
  </para>

616 617
  <para>
   To reload this database:
618 619 620
<screen>
<prompt>$</prompt> <userinput>psql -d database -f db.out</userinput>
</screen>
621 622 623
  </para>

  <para>
624 625
   To dump a database called mydb that contains
   <acronym>BLOB</acronym>s to a <filename>tar</filename> file:
626

627 628 629
<screen>
<prompt>$</prompt> <userinput>pg_dump -Ft -b mydb &gt; db.tar</userinput>
</screen>
630 631 632
  </para>

  <para>
633 634
   To reload this database (with <acronym>BLOB</acronym>s) to an
   existing database called newdb:
635

636 637 638
<screen>
<prompt>$</prompt> <userinput>pg_restore -d newdb db.tar</userinput>
</screen>
639 640
  </para>

641 642
 </refsect1>

643 644 645 646 647 648 649 650
 <refsect1>
  <title>History</title>

  <para>
   The <command>pg_restore</command> utility first appeared in
   PostgreSQL 7.1.
  </para>
 </refsect1>
651 652 653

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

655 656 657 658 659 660
  <simplelist type="inline">
   <member><xref linkend="app-pgdump"></member>
   <member><xref linkend="app-pg-dumpall"></member>
   <member><xref linkend="app-psql"></member>
   <member><citetitle>PostgreSQL Administrator's Guide</citetitle></member>
  </simplelist>
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
 </refsect1>
</refentry>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:nil
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"../reference.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:"/usr/lib/sgml/catalog"
sgml-local-ecat-files:nil
End:
-->