Skip to content

Commit 4106942

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 fb60050 commit 4106942

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
@@ -1408,6 +1408,14 @@ CREATE DATABASE foo WITH TEMPLATE template0;
14081408
information might have to be changed. It might also be appropriate to
14091409
truncate the target tables before initiating a new full table copy.
14101410
</para>
1411+
1412+
<para>
1413+
It is generally recommended to use the <option>-X</option>
1414+
(<option>--no-psqlrc</option>) option when restoring a database from a
1415+
plain-text <application>pg_dump</application> script to ensure a clean
1416+
restore process and prevent potential conflicts with
1417+
non-default <application>psql</application> configurations.
1418+
</para>
14111419
</refsect1>
14121420

14131421
<refsect1 id="pg-dump-examples" xreflabel="Examples">
@@ -1425,7 +1433,7 @@ CREATE DATABASE foo WITH TEMPLATE template0;
14251433
<literal>newdb</literal>:
14261434

14271435
<screen>
1428-
<prompt>$</prompt> <userinput>psql -d newdb -f db.sql</userinput>
1436+
<prompt>$</prompt> <userinput>psql -X -d newdb -f db.sql</userinput>
14291437
</screen>
14301438
</para>
14311439

doc/src/sgml/ref/pg_dumpall.sgml

+13-2
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,17 @@ PostgreSQL documentation
774774
database creation will fail for databases in non-default
775775
locations.
776776
</para>
777+
778+
<para>
779+
It is generally recommended to use the <option>-X</option>
780+
(<option>--no-psqlrc</option>) option when restoring a database from a
781+
<application>pg_dumpall</application> script to ensure a clean restore
782+
process and prevent potential conflicts with non-default
783+
<application>psql</application> configurations. Additionally, because
784+
the <application>pg_dumpall</application> script may
785+
include <application>psql</application> meta-commands, it may be
786+
incompatible with clients other than <application>psql</application>.
787+
</para>
777788
</refsect1>
778789

779790

@@ -790,9 +801,9 @@ PostgreSQL documentation
790801
<para>
791802
To restore database(s) from this file, you can use:
792803
<screen>
793-
<prompt>$</prompt> <userinput>psql -f db.out postgres</userinput>
804+
<prompt>$</prompt> <userinput>psql -X -f db.out -d postgres</userinput>
794805
</screen>
795-
It is not important to which database you connect here since the
806+
It is not important which database you connect to here since the
796807
script file created by <application>pg_dumpall</application> will
797808
contain the appropriate commands to create and connect to the saved
798809
databases. An exception is that if you specified <option>--clean</option>,

0 commit comments

Comments
 (0)