Skip to content

Commit c768f54

Browse files
committed
Doc: recommend "psql -X" for restoring pg_dump scripts.
This practice avoids possible problems caused by non-default psql options, such as disabling AUTOCOMMIT. Author: Shinya Kato <Shinya11.Kato@oss.nttdata.com> Reviewed-by: Robert Treat <rob@xzilla.net> Discussion: https://postgr.es/m/96ff23a5d858ff72ca8e823a014d16fe@oss.nttdata.com Backpatch-through: 13
1 parent 8cedf6a commit c768f54

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

doc/src/sgml/backup.sgml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,28 @@ pg_dump <replaceable class="parameter">dbname</replaceable> &gt; <replaceable cl
106106

107107
<para>
108108
Text files created by <application>pg_dump</application> are intended to
109-
be read in by the <application>psql</application> program. The
110-
general command form to restore a dump is
109+
be read by the <application>psql</application> program using its default
110+
settings. The general command form to restore a text dump is
111111
<synopsis>
112-
psql <replaceable class="parameter">dbname</replaceable> &lt; <replaceable class="parameter">dumpfile</replaceable>
112+
psql -X <replaceable class="parameter">dbname</replaceable> &lt; <replaceable class="parameter">dumpfile</replaceable>
113113
</synopsis>
114114
where <replaceable class="parameter">dumpfile</replaceable> is the
115115
file output by the <application>pg_dump</application> command. The database <replaceable
116116
class="parameter">dbname</replaceable> will not be created by this
117117
command, so you must create it yourself from <literal>template0</literal>
118118
before executing <application>psql</application> (e.g., with
119119
<literal>createdb -T template0 <replaceable
120-
class="parameter">dbname</replaceable></literal>). <application>psql</application>
120+
class="parameter">dbname</replaceable></literal>).
121+
To ensure <application>psql</application> runs with its default settings,
122+
use the <option>-X</option> (<option>--no-psqlrc</option>) option.
123+
<application>psql</application>
121124
supports options similar to <application>pg_dump</application> for specifying
122125
the database server to connect to and the user name to use. See
123126
the <xref linkend="app-psql"/> reference page for more information.
124-
Non-text file dumps are restored using the <xref
127+
</para>
128+
129+
<para>
130+
Non-text file dumps should be restored using the <xref
125131
linkend="app-pgrestore"/> utility.
126132
</para>
127133

@@ -141,7 +147,7 @@ psql <replaceable class="parameter">dbname</replaceable> &lt; <replaceable class
141147
behavior and have <application>psql</application> exit with an
142148
exit status of 3 if an SQL error occurs:
143149
<programlisting>
144-
psql --set ON_ERROR_STOP=on <replaceable>dbname</replaceable> &lt; <replaceable>dumpfile</replaceable>
150+
psql -X --set ON_ERROR_STOP=on <replaceable>dbname</replaceable> &lt; <replaceable>dumpfile</replaceable>
145151
</programlisting>
146152
Either way, you will only have a partially restored database.
147153
Alternatively, you can specify that the whole dump should be
@@ -160,7 +166,7 @@ psql --set ON_ERROR_STOP=on <replaceable>dbname</replaceable> &lt; <replaceable>
160166
write to or read from pipes makes it possible to dump a database
161167
directly from one server to another, for example:
162168
<programlisting>
163-
pg_dump -h <replaceable>host1</replaceable> <replaceable>dbname</replaceable> | psql -h <replaceable>host2</replaceable> <replaceable>dbname</replaceable>
169+
pg_dump -h <replaceable>host1</replaceable> <replaceable>dbname</replaceable> | psql -X -h <replaceable>host2</replaceable> <replaceable>dbname</replaceable>
164170
</programlisting>
165171
</para>
166172

@@ -205,7 +211,7 @@ pg_dumpall &gt; <replaceable>dumpfile</replaceable>
205211
</synopsis>
206212
The resulting dump can be restored with <application>psql</application>:
207213
<synopsis>
208-
psql -f <replaceable class="parameter">dumpfile</replaceable> postgres
214+
psql -X -f <replaceable class="parameter">dumpfile</replaceable> postgres
209215
</synopsis>
210216
(Actually, you can specify any existing database name to start from,
211217
but if you are loading into an empty cluster then <literal>postgres</literal>

doc/src/sgml/ref/pg_dump.sgml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1410,6 +1410,14 @@ CREATE DATABASE foo WITH TEMPLATE template0;
14101410
subscriber if the subscription had been originally created with
14111411
<literal>two_phase = true</literal> option.
14121412
</para>
1413+
1414+
<para>
1415+
It is generally recommended to use the <option>-X</option>
1416+
(<option>--no-psqlrc</option>) option when restoring a database from a
1417+
plain-text <application>pg_dump</application> script to ensure a clean
1418+
restore process and prevent potential conflicts with
1419+
non-default <application>psql</application> configurations.
1420+
</para>
14131421
</refsect1>
14141422

14151423
<refsect1 id="pg-dump-examples" xreflabel="Examples">
@@ -1427,7 +1435,7 @@ CREATE DATABASE foo WITH TEMPLATE template0;
14271435
<literal>newdb</literal>:
14281436

14291437
<screen>
1430-
<prompt>$</prompt> <userinput>psql -d newdb -f db.sql</userinput>
1438+
<prompt>$</prompt> <userinput>psql -X -d newdb -f db.sql</userinput>
14311439
</screen>
14321440
</para>
14331441

doc/src/sgml/ref/pg_dumpall.sgml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,17 @@ PostgreSQL documentation
786786
database creation will fail for databases in non-default
787787
locations.
788788
</para>
789+
790+
<para>
791+
It is generally recommended to use the <option>-X</option>
792+
(<option>--no-psqlrc</option>) option when restoring a database from a
793+
<application>pg_dumpall</application> script to ensure a clean restore
794+
process and prevent potential conflicts with non-default
795+
<application>psql</application> configurations. Additionally, because
796+
the <application>pg_dumpall</application> script may
797+
include <application>psql</application> meta-commands, it may be
798+
incompatible with clients other than <application>psql</application>.
799+
</para>
789800
</refsect1>
790801

791802

@@ -802,9 +813,9 @@ PostgreSQL documentation
802813
<para>
803814
To restore database(s) from this file, you can use:
804815
<screen>
805-
<prompt>$</prompt> <userinput>psql -f db.out postgres</userinput>
816+
<prompt>$</prompt> <userinput>psql -X -f db.out -d postgres</userinput>
806817
</screen>
807-
It is not important to which database you connect here since the
818+
It is not important which database you connect to here since the
808819
script file created by <application>pg_dumpall</application> will
809820
contain the appropriate commands to create and connect to the saved
810821
databases. An exception is that if you specified <option>--clean</option>,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy