<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >pg_restore</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 8.0.11 Documentation" HREF="index.html"><LINK REL="UP" TITLE="PostgreSQL Client Applications" HREF="reference-client.html"><LINK REL="PREVIOUS" TITLE="pg_dumpall" HREF="app-pg-dumpall.html"><LINK REL="NEXT" TITLE="psql" HREF="app-psql.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META NAME="creation" CONTENT="2007-02-02T03:57:22"></HEAD ><BODY CLASS="REFENTRY" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" >PostgreSQL 8.0.11 Documentation</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="app-pg-dumpall.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="app-pg-dumpall.html" >Fast Backward</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="app-psql.html" >Fast Forward</A ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="app-psql.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="APP-PGRESTORE" ></A >pg_restore</H1 ><DIV CLASS="REFNAMEDIV" ><A NAME="AEN48117" ></A ><H2 >Name</H2 >pg_restore -- restore a <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > database from an archive file created by pg_dump </DIV ><A NAME="AEN48121" ></A ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN48123" ></A ><H2 >Synopsis</H2 ><P ><TT CLASS="COMMAND" >pg_restore</TT > [<TT CLASS="REPLACEABLE" ><I >option</I ></TT >...] [<TT CLASS="REPLACEABLE" ><I >filename</I ></TT >]</P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="APP-PGRESTORE-DESCRIPTION" ></A ><H2 >Description</H2 ><P > <SPAN CLASS="APPLICATION" >pg_restore</SPAN > is a utility for restoring a <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > database from an archive created by <A HREF="app-pgdump.html" >pg_dump</A > in one of the non-plain-text formats. It will issue the commands necessary to reconstruct the database to the state it was in at the time it was saved. The archive files also allow <SPAN CLASS="APPLICATION" >pg_restore</SPAN > 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. </P ><P > <SPAN CLASS="APPLICATION" >pg_restore</SPAN > can operate in two modes: If a database name is specified, the archive is restored directly into the database. (Large objects can only be restored by using such a direct database connection.) 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 <SPAN CLASS="APPLICATION" >pg_dump</SPAN > plain text format. Some of the options controlling the script output are therefore analogous to <SPAN CLASS="APPLICATION" >pg_dump</SPAN > options. </P ><P > Obviously, <SPAN CLASS="APPLICATION" >pg_restore</SPAN > cannot restore information that is not present in the archive file. For instance, if the archive was made using the <SPAN CLASS="QUOTE" >"dump data as <TT CLASS="COMMAND" >INSERT</TT > commands"</SPAN > option, <SPAN CLASS="APPLICATION" >pg_restore</SPAN > will not be able to load the data using <TT CLASS="COMMAND" >COPY</TT > statements. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="APP-PGRESTORE-OPTIONS" ></A ><H2 >Options</H2 ><P > <SPAN CLASS="APPLICATION" >pg_restore</SPAN > accepts the following command line arguments. <P ></P ></P><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="REPLACEABLE" ><I >filename</I ></TT ></DT ><DD ><P > Specifies the location of the archive file to be restored. If not specified, the standard input is used. </P ></DD ><DT ><TT CLASS="OPTION" >-a</TT ><BR><TT CLASS="OPTION" >--data-only</TT ></DT ><DD ><P > Restore only the data, not the schema (data definitions). </P ></DD ><DT ><TT CLASS="OPTION" >-c</TT ><BR><TT CLASS="OPTION" >--clean</TT ></DT ><DD ><P > Clean (drop) database objects before recreating them. </P ></DD ><DT ><TT CLASS="OPTION" >-C</TT ><BR><TT CLASS="OPTION" >--create</TT ></DT ><DD ><P > Create the database before restoring into it. (When this option is used, the database named with <TT CLASS="OPTION" >-d</TT > is used only to issue the initial <TT CLASS="COMMAND" >CREATE DATABASE</TT > command. All data is restored into the database name that appears in the archive.) </P ></DD ><DT ><TT CLASS="OPTION" >-d <TT CLASS="REPLACEABLE" ><I >dbname</I ></TT ></TT ><BR><TT CLASS="OPTION" >--dbname=<TT CLASS="REPLACEABLE" ><I >dbname</I ></TT ></TT ></DT ><DD ><P > Connect to database <TT CLASS="REPLACEABLE" ><I >dbname</I ></TT > and restore directly into the database. </P ></DD ><DT ><TT CLASS="OPTION" >-e</TT ><BR><TT CLASS="OPTION" >--exit-on-error</TT ></DT ><DD ><P > Exit if an error is encountered while sending SQL commands to the database. The default is to continue and to display a count of errors at the end of the restoration. </P ></DD ><DT ><TT CLASS="OPTION" >-f <TT CLASS="REPLACEABLE" ><I >filename</I ></TT ></TT ><BR><TT CLASS="OPTION" >--file=<TT CLASS="REPLACEABLE" ><I >filename</I ></TT ></TT ></DT ><DD ><P > Specify output file for generated script, or for the listing when used with <TT CLASS="OPTION" >-l</TT >. Default is the standard output. </P ></DD ><DT ><TT CLASS="OPTION" >-F <TT CLASS="REPLACEABLE" ><I >format</I ></TT ></TT ><BR><TT CLASS="OPTION" >--format=<TT CLASS="REPLACEABLE" ><I >format</I ></TT ></TT ></DT ><DD ><P > Specify format of the archive. It is not necessary to specify the format, since <SPAN CLASS="APPLICATION" >pg_restore</SPAN > will determine the format automatically. If specified, it can be one of the following: <P ></P ></P><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="LITERAL" >t</TT ></DT ><DD ><P > The archive is a <TT CLASS="COMMAND" >tar</TT > archive. Using this archive format allows reordering and/or exclusion of schema elements at the time the database is restored. It is also possible to limit which data is reloaded at restore time. </P ></DD ><DT ><TT CLASS="LITERAL" >c</TT ></DT ><DD ><P > The archive is in the custom format of <SPAN CLASS="APPLICATION" >pg_dump</SPAN >. This is the most flexible format in that it allows reordering of data load as well as schema elements. This format is also compressed by default. </P ></DD ></DL ></DIV ><P> </P ></DD ><DT ><TT CLASS="OPTION" >-i</TT ><BR><TT CLASS="OPTION" >--ignore-version</TT ></DT ><DD ><P > Ignore database version checks. </P ></DD ><DT ><TT CLASS="OPTION" >-I <TT CLASS="REPLACEABLE" ><I >index</I ></TT ></TT ><BR><TT CLASS="OPTION" >--index=<TT CLASS="REPLACEABLE" ><I >index</I ></TT ></TT ></DT ><DD ><P > Restore definition of named index only. </P ></DD ><DT ><TT CLASS="OPTION" >-l</TT ><BR><TT CLASS="OPTION" >--list</TT ></DT ><DD ><P > List the contents of the archive. The output of this operation can be used with the <TT CLASS="OPTION" >-L</TT > option to restrict and reorder the items that are restored. </P ></DD ><DT ><TT CLASS="OPTION" >-L <TT CLASS="REPLACEABLE" ><I >list-file</I ></TT ></TT ><BR><TT CLASS="OPTION" >--use-list=<TT CLASS="REPLACEABLE" ><I >list-file</I ></TT ></TT ></DT ><DD ><P > Restore elements in <TT CLASS="REPLACEABLE" ><I > list-file</I ></TT > only, and in the order they appear in the file. Lines can be moved and may also be commented out by placing a <TT CLASS="LITERAL" >;</TT > at the start of the line. (See below for examples.) </P ></DD ><DT ><TT CLASS="OPTION" >-O</TT ><BR><TT CLASS="OPTION" >--no-owner</TT ></DT ><DD ><P > Do not output commands to set ownership of objects to match the original database. By default, <SPAN CLASS="APPLICATION" >pg_restore</SPAN > issues <TT CLASS="COMMAND" >ALTER OWNER</TT > or <TT CLASS="COMMAND" >SET SESSION AUTHORIZATION</TT > statements to set ownership of created schema elements. These statements will fail unless the initial connection to the database is made by a superuser (or the same user that owns all of the objects in the script). With <TT CLASS="OPTION" >-O</TT >, any user name can be used for the initial connection, and this user will own all the created objects. </P ></DD ><DT ><TT CLASS="OPTION" >-P <TT CLASS="REPLACEABLE" ><I >function-name(argtype [, ...])</I ></TT ></TT ><BR><TT CLASS="OPTION" >--function=<TT CLASS="REPLACEABLE" ><I >function-name(argtype [, ...])</I ></TT ></TT ></DT ><DD ><P > Restore the named function only. Be careful to spell the function name and arguments exactly as they appear in the dump file's table of contents. </P ></DD ><DT ><TT CLASS="OPTION" >-R</TT ><BR><TT CLASS="OPTION" >--no-reconnect</TT ></DT ><DD ><P > This option is obsolete but still accepted for backwards compatibility. </P ></DD ><DT ><TT CLASS="OPTION" >-s</TT ><BR><TT CLASS="OPTION" >--schema-only</TT ></DT ><DD ><P > Restore only the schema (data definitions), not the data. Sequence values will be reset. </P ></DD ><DT ><TT CLASS="OPTION" >-S <TT CLASS="REPLACEABLE" ><I >username</I ></TT ></TT ><BR><TT CLASS="OPTION" >--superuser=<TT CLASS="REPLACEABLE" ><I >username</I ></TT ></TT ></DT ><DD ><P > Specify the superuser user name to use when disabling triggers. This is only relevant if <TT CLASS="OPTION" >--disable-triggers</TT > is used. </P ></DD ><DT ><TT CLASS="OPTION" >-t <TT CLASS="REPLACEABLE" ><I >table</I ></TT ></TT ><BR><TT CLASS="OPTION" >--table=<TT CLASS="REPLACEABLE" ><I >table</I ></TT ></TT ></DT ><DD ><P > Restore definition and/or data of named table only. </P ></DD ><DT ><TT CLASS="OPTION" >-T <TT CLASS="REPLACEABLE" ><I >trigger</I ></TT ></TT ><BR><TT CLASS="OPTION" >--trigger=<TT CLASS="REPLACEABLE" ><I >trigger</I ></TT ></TT ></DT ><DD ><P > Restore named trigger only. </P ></DD ><DT ><TT CLASS="OPTION" >-v</TT ><BR><TT CLASS="OPTION" >--verbose</TT ></DT ><DD ><P > Specifies verbose mode. </P ></DD ><DT ><TT CLASS="OPTION" >-x</TT ><BR><TT CLASS="OPTION" >--no-privileges</TT ><BR><TT CLASS="OPTION" >--no-acl</TT ></DT ><DD ><P > Prevent restoration of access privileges (grant/revoke commands). </P ></DD ><DT ><TT CLASS="OPTION" >-X use-set-session-authorization</TT ><BR><TT CLASS="OPTION" >--use-set-session-authorization</TT ></DT ><DD ><P > Output SQL standard SET SESSION AUTHORIZATION commands instead of OWNER TO commands. This makes the dump more standards compatible, but depending on the history of the objects in the dump, may not restore properly. </P ></DD ><DT ><TT CLASS="OPTION" >-X disable-triggers</TT ><BR><TT CLASS="OPTION" >--disable-triggers</TT ></DT ><DD ><P > This option is only relevant when performing a data-only restore. It instructs <SPAN CLASS="APPLICATION" >pg_restore</SPAN > to execute commands to temporarily disable triggers on the target tables while the data is reloaded. Use this if you have referential integrity checks or other triggers on the tables that you do not want to invoke during data reload. </P ><P > Presently, the commands emitted for <TT CLASS="OPTION" >--disable-triggers</TT > must be done as superuser. So, you should also specify a superuser name with <TT CLASS="OPTION" >-S</TT >, or preferably run <SPAN CLASS="APPLICATION" >pg_restore</SPAN > as a <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > superuser. </P ></DD ></DL ></DIV ><P> </P ><P > <SPAN CLASS="APPLICATION" >pg_restore</SPAN > also accepts the following command line arguments for connection parameters: <P ></P ></P><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="OPTION" >-h <TT CLASS="REPLACEABLE" ><I >host</I ></TT ></TT ><BR><TT CLASS="OPTION" >--host=<TT CLASS="REPLACEABLE" ><I >host</I ></TT ></TT ></DT ><DD ><P > Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix domain socket. The default is taken from the <TT CLASS="ENVAR" >PGHOST</TT > environment variable, if set, else a Unix domain socket connection is attempted. </P ></DD ><DT ><TT CLASS="OPTION" >-p <TT CLASS="REPLACEABLE" ><I >port</I ></TT ></TT ><BR><TT CLASS="OPTION" >--port=<TT CLASS="REPLACEABLE" ><I >port</I ></TT ></TT ></DT ><DD ><P > Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections. Defaults to the <TT CLASS="ENVAR" >PGPORT</TT > environment variable, if set, or a compiled-in default. </P ></DD ><DT ><TT CLASS="OPTION" >-U <TT CLASS="REPLACEABLE" ><I >username</I ></TT ></TT ></DT ><DD ><P > Connect as the given user </P ></DD ><DT ><TT CLASS="OPTION" >-W</TT ></DT ><DD ><P > Force a password prompt. This should happen automatically if the server requires password authentication. </P ></DD ></DL ></DIV ><P> </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN48397" ></A ><H2 >Environment</H2 ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="ENVAR" >PGHOST</TT ><BR><TT CLASS="ENVAR" >PGPORT</TT ><BR><TT CLASS="ENVAR" >PGUSER</TT ></DT ><DD ><P > Default connection parameters </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="REFSECT1" ><A NAME="APP-PGRESTORE-DIAGNOSTICS" ></A ><H2 >Diagnostics</H2 ><P > When a direct database connection is specified using the <TT CLASS="OPTION" >-d</TT > option, <SPAN CLASS="APPLICATION" >pg_restore</SPAN > internally executes <ACRONYM CLASS="ACRONYM" >SQL</ACRONYM > statements. If you have problems running <SPAN CLASS="APPLICATION" >pg_restore</SPAN >, make sure you are able to select information from the database using, for example, <A HREF="app-psql.html" ><SPAN CLASS="APPLICATION" >psql</SPAN ></A >. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="APP-PGRESTORE-NOTES" ></A ><H2 >Notes</H2 ><P > If your installation has any local additions to the <TT CLASS="LITERAL" >template1</TT > database, be careful to load the output of <SPAN CLASS="APPLICATION" >pg_restore</SPAN > into a truly empty database; otherwise you are likely to get errors due to duplicate definitions of the added objects. To make an empty database without any local additions, copy from <TT CLASS="LITERAL" >template0</TT > not <TT CLASS="LITERAL" >template1</TT >, for example: </P><PRE CLASS="PROGRAMLISTING" >CREATE DATABASE foo WITH TEMPLATE template0;</PRE ><P> </P ><P > The limitations of <SPAN CLASS="APPLICATION" >pg_restore</SPAN > are detailed below. <P ></P ></P><UL ><LI ><P > When restoring data to a pre-existing table and the option <TT CLASS="OPTION" >--disable-triggers</TT > is used, <SPAN CLASS="APPLICATION" >pg_restore</SPAN > emits commands to disable triggers on user tables before inserting the data then emits commands to re-enable them after the data has been inserted. If the restore is stopped in the middle, the system catalogs may be left in the wrong state. </P ></LI ><LI ><P > <SPAN CLASS="APPLICATION" >pg_restore</SPAN > will not restore large objects for a single table. If an archive contains large objects, then all large objects will be restored. </P ></LI ></UL ><P> </P ><P > See also the <A HREF="app-pgdump.html" >pg_dump</A > documentation for details on limitations of <SPAN CLASS="APPLICATION" >pg_dump</SPAN >. </P ><P > Once restored, it is wise to run <TT CLASS="COMMAND" >ANALYZE</TT > on each restored table so the optimizer has useful statistics. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="APP-PGRESTORE-EXAMPLES" ></A ><H2 >Examples</H2 ><P > To dump a database called <TT CLASS="LITERAL" >mydb</TT > that contains large objects to a <TT CLASS="FILENAME" >tar</TT > file: </P><PRE CLASS="SCREEN" ><SAMP CLASS="PROMPT" >$</SAMP > <KBD CLASS="USERINPUT" >pg_dump -Ft -b mydb > db.tar</KBD ></PRE ><P> </P ><P > To reload this database (with large objects) to an existing database called <TT CLASS="LITERAL" >newdb</TT >: </P><PRE CLASS="SCREEN" ><SAMP CLASS="PROMPT" >$</SAMP > <KBD CLASS="USERINPUT" >pg_restore -d newdb db.tar</KBD ></PRE ><P> </P ><P > To reorder database items, it is first necessary to dump the table of contents of the archive: </P><PRE CLASS="SCREEN" ><SAMP CLASS="PROMPT" >$</SAMP > <KBD CLASS="USERINPUT" >pg_restore -l archive.file > archive.list</KBD ></PRE ><P> The listing file consists of a header and one line for each item, e.g., </P><PRE CLASS="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</PRE ><P> Semicolons start a comment, and the numbers at the start of lines refer to the internal archive ID assigned to each item. </P ><P > Lines in the file can be commented out, deleted, and reordered. For example, </P><PRE CLASS="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</PRE ><P> could be used as input to <SPAN CLASS="APPLICATION" >pg_restore</SPAN > and would only restore items 10 and 6, in that order: </P><PRE CLASS="SCREEN" ><SAMP CLASS="PROMPT" >$</SAMP > <KBD CLASS="USERINPUT" >pg_restore -L archive.list archive.file</KBD ></PRE ><P> </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN48464" ></A ><H2 >History</H2 ><P > The <SPAN CLASS="APPLICATION" >pg_restore</SPAN > utility first appeared in <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > 7.1. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN48469" ></A ><H2 >See Also</H2 ><A HREF="app-pgdump.html" >pg_dump</A >, <A HREF="app-pg-dumpall.html" ><SPAN CLASS="APPLICATION" >pg_dumpall</SPAN ></A >, <A HREF="app-psql.html" ><SPAN CLASS="APPLICATION" >psql</SPAN ></A ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="app-pg-dumpall.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="app-psql.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><SPAN CLASS="APPLICATION" >pg_dumpall</SPAN ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="reference-client.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><SPAN CLASS="APPLICATION" >psql</SPAN ></TD ></TR ></TABLE ></DIV ></BODY ></HTML >