Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf656517

Browse files
committed
Fix connection string handling in psql's \connect command.
psql's \connect claims to be able to re-use previous connectionparameters, but in fact it only re-uses the database name, user name,host name (and possibly hostaddr, depending on version), and port.This is problematic for assorted use cases. Notably, pg_dump[all]emits "\connect databasename" commands which we would like to havere-use all other parameters. If such a script is loaded in a psql runthat initially had "-d connstring" with some non-default parameters,those other parameters would be lost, potentially causing connectionfailure. (Thus, this is the same kind of bug addressed in commitsa45bc8a and8e5793a, although the details are much different.)To fix, redesign do_connect() so that it pulls out all propertiesof the old PGconn using PQconninfo(), and then replaces individualproperties in that array. In the case where we don't wish to re-useanything, get libpq's default settings using PQconndefaults() andreplace entries in that, so that we don't need different code pathsfor the two cases.This does result in an additional behavioral change for cases wherethe original connection parameters allowed multiple hosts, say"psql -h host1,host2", and the \connect request allows re-use of thehost setting. Because the previous coding relied on PQhost(), itwould only permit reconnection to the same host originally selected.Although one can think of scenarios where that's a good thing, thereare others where it is not. Moreover, that behavior doesn't seem tomeet the principle of least surprise, nor was it documented; nor isit even clear it was intended, since that coding long pre-dates theaddition of multi-host support to libpq. Hence, this patch is contentto drop it and re-use the host list as given.Per Peter Eisentraut's comments on bug #16604. Back-patch to allsupported branches.Discussion:https://postgr.es/m/16604-933f4b8791227b15@postgresql.org
1 parenta818000 commitf656517

File tree

2 files changed

+217
-126
lines changed

2 files changed

+217
-126
lines changed

‎doc/src/sgml/ref/psql-ref.sgml

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -890,35 +890,47 @@ testdb=>
890890
<para>
891891
Establishes a new connection to a <productname>PostgreSQL</productname>
892892
server. The connection parameters to use can be specified either
893-
using a positional syntax, or using <replaceable>conninfo</replaceable> connection
894-
strings as detailed in <xref linkend="libpq-connstring"/>.
893+
using a positional syntax (one or more of database name, user,
894+
host, and port), or using a <replaceable>conninfo</replaceable>
895+
connection string as detailed in
896+
<xref linkend="libpq-connstring"/>. If no arguments are given, a
897+
new connection is made using the same parameters as before.
895898
</para>
896899

897900
<para>
898-
Where the command omits database name, user, host, or port, the new
899-
connection can reuse values from the previous connection. By default,
900-
values from the previous connection are reused except when processing
901-
a <replaceable>conninfo</replaceable> string. Passing a first argument
902-
of <literal>-reuse-previous=on</literal>
903-
or <literal>-reuse-previous=off</literal> overrides that default.
904-
When the command neither specifies nor reuses a particular parameter,
905-
the <application>libpq</application> default is used. Specifying any
901+
Specifying any
906902
of <replaceable class="parameter">dbname</replaceable>,
907903
<replaceable class="parameter">username</replaceable>,
908904
<replaceable class="parameter">host</replaceable> or
909905
<replaceable class="parameter">port</replaceable>
910906
as <literal>-</literal> is equivalent to omitting that parameter.
911-
If <literal>hostaddr</literal> was specified in the original
912-
connection's <structname>conninfo</structname>, that address is reused
913-
for the new connection (disregarding any other host specification).
907+
</para>
908+
909+
<para>
910+
The new connection can re-use connection parameters from the previous
911+
connection; not only database name, user, host, and port, but other
912+
settings such as <replaceable>sslmode</replaceable>. By default,
913+
parameters are re-used in the positional syntax, but not when
914+
a <replaceable>conninfo</replaceable> string is given. Passing a
915+
first argument of <literal>-reuse-previous=on</literal>
916+
or <literal>-reuse-previous=off</literal> overrides that default. If
917+
parameters are re-used, then any parameter not explicitly specified as
918+
a positional parameter or in the <replaceable>conninfo</replaceable>
919+
string is taken from the existing connection's parameters. An
920+
exception is that if the <replaceable>host</replaceable> setting
921+
is changed from its previous value using the positional syntax,
922+
any <replaceable>hostaddr</replaceable> setting present in the
923+
existing connection's parameters is dropped.
924+
When the command neither specifies nor reuses a particular parameter,
925+
the <application>libpq</application> default is used.
914926
</para>
915927

916928
<para>
917929
If the new connection is successfully made, the previous
918930
connection is closed.
919-
If the connection attemptfailed (wrong user name, access
920-
denied, etc.), the previous connection willonlybe kept if
921-
<application>psql</application> is in interactive mode.When
931+
If the connection attemptfails (wrong user name, access
932+
denied, etc.), the previous connection will be kept if
933+
<application>psql</application> is in interactive mode.But when
922934
executing a non-interactive script, processing will
923935
immediately stop with an error. This distinction was chosen as
924936
a user convenience against typos on the one hand, and a safety
@@ -933,6 +945,7 @@ testdb=&gt;
933945
=&gt; \c mydb myuser host.dom 6432
934946
=&gt; \c service=foo
935947
=&gt; \c "host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable"
948+
=&gt; \c -reuse-previous=on sslmode=require -- changes only sslmode
936949
=&gt; \c postgresql://tom@localhost/mydb?application_name=myapp
937950
</programlisting>
938951
</listitem>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp