diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2020-10-21 16:18:40 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2020-10-21 16:19:00 -0400 |
commit | 85c54287af56fe351b53913ea2b81e9d6145f964 (patch) | |
tree | ec03415a47ff931d657da46419da1b565cc2cc67 /doc/src | |
parent | 831611b11c34ed0066633864f42ff67a10286aee (diff) | |
download | postgresql-85c54287af56fe351b53913ea2b81e9d6145f964.tar.gz postgresql-85c54287af56fe351b53913ea2b81e9d6145f964.zip |
Fix connection string handling in psql's \connect command.
psql's \connect claims to be able to re-use previous connection
parameters, 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 have
re-use all other parameters. If such a script is loaded in a psql run
that initially had "-d connstring" with some non-default parameters,
those other parameters would be lost, potentially causing connection
failure. (Thus, this is the same kind of bug addressed in commits
a45bc8a4f and 8e5793ab6, although the details are much different.)
To fix, redesign do_connect() so that it pulls out all properties
of the old PGconn using PQconninfo(), and then replaces individual
properties in that array. In the case where we don't wish to re-use
anything, get libpq's default settings using PQconndefaults() and
replace entries in that, so that we don't need different code paths
for the two cases.
This does result in an additional behavioral change for cases where
the original connection parameters allowed multiple hosts, say
"psql -h host1,host2", and the \connect request allows re-use of the
host setting. Because the previous coding relied on PQhost(), it
would only permit reconnection to the same host originally selected.
Although one can think of scenarios where that's a good thing, there
are others where it is not. Moreover, that behavior doesn't seem to
meet the principle of least surprise, nor was it documented; nor is
it even clear it was intended, since that coding long pre-dates the
addition of multi-host support to libpq. Hence, this patch is content
to drop it and re-use the host list as given.
Per Peter Eisentraut's comments on bug #16604. Back-patch to all
supported branches.
Discussion: https://postgr.es/m/16604-933f4b8791227b15@postgresql.org
Diffstat (limited to 'doc/src')
-rw-r--r-- | doc/src/sgml/ref/psql-ref.sgml | 45 |
1 files changed, 29 insertions, 16 deletions
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index ee3fc095779..7d0d3616573 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -889,35 +889,47 @@ testdb=> <para> Establishes a new connection to a <productname>PostgreSQL</productname> server. The connection parameters to use can be specified either - using a positional syntax, or using <replaceable>conninfo</replaceable> connection - strings as detailed in <xref linkend="libpq-connstring"/>. + using a positional syntax (one or more of database name, user, + host, and port), or using a <replaceable>conninfo</replaceable> + connection string as detailed in + <xref linkend="libpq-connstring"/>. If no arguments are given, a + new connection is made using the same parameters as before. </para> <para> - Where the command omits database name, user, host, or port, the new - connection can reuse values from the previous connection. By default, - values from the previous connection are reused except when processing - a <replaceable>conninfo</replaceable> string. Passing a first argument - of <literal>-reuse-previous=on</literal> - or <literal>-reuse-previous=off</literal> overrides that default. - When the command neither specifies nor reuses a particular parameter, - the <application>libpq</application> default is used. Specifying any + Specifying any of <replaceable class="parameter">dbname</replaceable>, <replaceable class="parameter">username</replaceable>, <replaceable class="parameter">host</replaceable> or <replaceable class="parameter">port</replaceable> as <literal>-</literal> is equivalent to omitting that parameter. - If <literal>hostaddr</literal> was specified in the original - connection's <structname>conninfo</structname>, that address is reused - for the new connection (disregarding any other host specification). + </para> + + <para> + The new connection can re-use connection parameters from the previous + connection; not only database name, user, host, and port, but other + settings such as <replaceable>sslmode</replaceable>. By default, + parameters are re-used in the positional syntax, but not when + a <replaceable>conninfo</replaceable> string is given. Passing a + first argument of <literal>-reuse-previous=on</literal> + or <literal>-reuse-previous=off</literal> overrides that default. If + parameters are re-used, then any parameter not explicitly specified as + a positional parameter or in the <replaceable>conninfo</replaceable> + string is taken from the existing connection's parameters. An + exception is that if the <replaceable>host</replaceable> setting + is changed from its previous value using the positional syntax, + any <replaceable>hostaddr</replaceable> setting present in the + existing connection's parameters is dropped. + When the command neither specifies nor reuses a particular parameter, + the <application>libpq</application> default is used. </para> <para> If the new connection is successfully made, the previous connection is closed. - If the connection attempt failed (wrong user name, access - denied, etc.), the previous connection will only be kept if - <application>psql</application> is in interactive mode. When + If the connection attempt fails (wrong user name, access + denied, etc.), the previous connection will be kept if + <application>psql</application> is in interactive mode. But when executing a non-interactive script, processing will immediately stop with an error. This distinction was chosen as a user convenience against typos on the one hand, and a safety @@ -932,6 +944,7 @@ testdb=> => \c mydb myuser host.dom 6432 => \c service=foo => \c "host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable" +=> \c -reuse-previous=on sslmode=require -- changes only sslmode => \c postgresql://tom@localhost/mydb?application_name=myapp </programlisting> </listitem> |