Skip to content

Commit aaa0f06

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 88d322a commit aaa0f06

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

doc/src/sgml/backup.sgml

+14-8
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

+9-1
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,14 @@ CREATE DATABASE foo WITH TEMPLATE template0;
13661366
information might have to be changed. It might also be appropriate to
13671367
truncate the target tables before initiating a new full table copy.
13681368
</para>
1369+
1370+
<para>
1371+
It is generally recommended to use the <option>-X</option>
1372+
(<option>--no-psqlrc</option>) option when restoring a database from a
1373+
plain-text <application>pg_dump</application> script to ensure a clean
1374+
restore process and prevent potential conflicts with
1375+
non-default <application>psql</application> configurations.
1376+
</para>
13691377
</refsect1>
13701378

13711379
<refsect1 id="pg-dump-examples" xreflabel="Examples">
@@ -1383,7 +1391,7 @@ CREATE DATABASE foo WITH TEMPLATE template0;
13831391
<literal>newdb</literal>:
13841392

13851393
<screen>
1386-
<prompt>$</prompt> <userinput>psql -d newdb -f db.sql</userinput>
1394+
<prompt>$</prompt> <userinput>psql -X -d newdb -f db.sql</userinput>
13871395
</screen>
13881396
</para>
13891397

doc/src/sgml/ref/pg_dumpall.sgml

+13-2
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,17 @@ PostgreSQL documentation
763763
database creation will fail for databases in non-default
764764
locations.
765765
</para>
766+
767+
<para>
768+
It is generally recommended to use the <option>-X</option>
769+
(<option>--no-psqlrc</option>) option when restoring a database from a
770+
<application>pg_dumpall</application> script to ensure a clean restore
771+
process and prevent potential conflicts with non-default
772+
<application>psql</application> configurations. Additionally, because
773+
the <application>pg_dumpall</application> script may
774+
include <application>psql</application> meta-commands, it may be
775+
incompatible with clients other than <application>psql</application>.
776+
</para>
766777
</refsect1>
767778

768779

@@ -779,9 +790,9 @@ PostgreSQL documentation
779790
<para>
780791
To restore database(s) from this file, you can use:
781792
<screen>
782-
<prompt>$</prompt> <userinput>psql -f db.out postgres</userinput>
793+
<prompt>$</prompt> <userinput>psql -X -f db.out -d postgres</userinput>
783794
</screen>
784-
It is not important to which database you connect here since the
795+
It is not important which database you connect to here since the
785796
script file created by <application>pg_dumpall</application> will
786797
contain the appropriate commands to create and connect to the saved
787798
databases. An exception is that if you specified <option>--clean</option>,

0 commit comments

Comments
 (0)