Skip to content

Commit d4bacdc

Browse files
committed
psql: fix \connect with URIs and conninfo strings
psql was already accepting conninfo strings as the first parameter in \connect, but the way it worked wasn't sane; some of the other parameters would get the previous connection's values, causing it to connect to a completely unexpected server or, more likely, not finding any server at all because of completely wrong combinations of parameters. Fix by explicitely checking for a conninfo-looking parameter in the dbname position; if one is found, use its complete specification rather than mix with the other arguments. Also, change tab-completion to not try to complete conninfo/URI-looking "dbnames" and document that conninfos are accepted as first argument. There was a weak consensus to backpatch this, because while the behavior of using the dbname as a conninfo is nowhere documented for \connect, it is reasonable to expect that it works because it does work in many other contexts. Therefore this is backpatched all the way back to 9.0. To implement this, routines previously private to libpq have been duplicated so that psql can decide what looks like a conninfo/URI string. In back branches, just duplicate the same code all the way back to 9.2, where URIs where introduced; 9.0 and 9.1 have a simpler version. In master, the routines are moved to src/common and renamed. Author: David Fetter, Andrew Dunstan. Some editorialization by me (probably earning a Gierth's "Sloppy" badge in the process.) Reviewers: Andrew Gierth, Erik Rijkers, Pavel Stěhule, Stephen Frost, Robert Haas, Andrew Dunstan.
1 parent 993674e commit d4bacdc

File tree

6 files changed

+127
-35
lines changed

6 files changed

+127
-35
lines changed

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

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -771,23 +771,31 @@ testdb=>
771771
</varlistentry>
772772

773773
<varlistentry>
774-
<term><literal>\c</literal> or <literal>\connect</literal> <literal>[ <replaceable class="parameter">dbname</replaceable> [ <replaceable class="parameter">username</replaceable> ] [ <replaceable class="parameter">host</replaceable> ] [ <replaceable class="parameter">port</replaceable> ] ]</literal></term>
774+
<term><literal>\c</literal> or <literal>\connect</literal> <literal>[ <replaceable class="parameter">dbname</replaceable> [ <replaceable class="parameter">username</replaceable> ] [ <replaceable class="parameter">host</replaceable> ] [ <replaceable class="parameter">port</replaceable> ] ] | <replaceable class="parameter">conninfo</replaceable> </literal></term>
775775
<listitem>
776776
<para>
777777
Establishes a new connection to a <productname>PostgreSQL</>
778-
server. If the new connection is successfully made, the
779-
previous connection is closed. If any of <replaceable
780-
class="parameter">dbname</replaceable>, <replaceable
781-
class="parameter">username</replaceable>, <replaceable
782-
class="parameter">host</replaceable> or <replaceable
783-
class="parameter">port</replaceable> are omitted or specified
784-
as <literal>-</literal>, the value of that parameter from the
785-
previous connection is used. If there is no previous
786-
connection, the <application>libpq</application> default for
787-
the parameter's value is used.
778+
server. The connection parameters to use can be specified either
779+
using a positional syntax, or using <literal>conninfo</> connection
780+
strings as detailed in <xref linkend="libpq-connstring">.
788781
</para>
789782

790783
<para>
784+
When using positional parameters, if any of
785+
<replaceable class="parameter">dbname</replaceable>,
786+
<replaceable class="parameter">username</replaceable>,
787+
<replaceable class="parameter">host</replaceable> or
788+
<replaceable class="parameter">port</replaceable> are omitted or
789+
specified as <literal>-</literal>, the value of that parameter from
790+
the previous connection is used; if there is no previous connection,
791+
the <application>libpq</application> default for the parameter's value
792+
is used. When using <literal>conninfo</> strings, no values from the
793+
previous connection are used for the new connection.
794+
</para>
795+
796+
<para>
797+
If the new connection is successfully made, the previous
798+
connection is closed.
791799
If the connection attempt failed (wrong user name, access
792800
denied, etc.), the previous connection will only be kept if
793801
<application>psql</application> is in interactive mode. When
@@ -797,6 +805,16 @@ testdb=&gt;
797805
mechanism that scripts are not accidentally acting on the
798806
wrong database on the other hand.
799807
</para>
808+
809+
<para>
810+
Examples:
811+
</para>
812+
<programlisting>
813+
=&gt; \c mydb myuser host.dom 6432
814+
=&gt; \c service=foo
815+
=&gt; \c "host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable"
816+
=&gt; \c postgresql://tom@localhost/mydb?application_name=myapp
817+
</programlisting>
800818
</listitem>
801819
</varlistentry>
802820

src/bin/psql/command.c

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,16 +1506,38 @@ do_connect(char *dbname, char *user, char *host, char *port)
15061506
PGconn *o_conn = pset.db,
15071507
*n_conn;
15081508
char *password = NULL;
1509+
bool keep_password;
1510+
bool has_connection_string;
15091511

1510-
if (!dbname)
1511-
dbname = PQdb(o_conn);
1512+
/* grab values from the old connection, unless supplied by caller */
15121513
if (!user)
15131514
user = PQuser(o_conn);
15141515
if (!host)
15151516
host = PQhost(o_conn);
15161517
if (!port)
15171518
port = PQport(o_conn);
15181519

1520+
has_connection_string =
1521+
dbname ? recognized_connection_string(dbname) : false;
1522+
1523+
/*
1524+
* Any change in the parameters read above makes us discard the password.
1525+
* We also discard it if we're to use a conninfo rather than the positional
1526+
* syntax.
1527+
*/
1528+
keep_password =
1529+
((strcmp(user, PQuser(o_conn)) == 0) &&
1530+
(!host || strcmp(host, PQhost(o_conn)) == 0) &&
1531+
(strcmp(port, PQport(o_conn)) == 0) &&
1532+
!has_connection_string);
1533+
1534+
/*
1535+
* Grab dbname from old connection unless supplied by caller. No password
1536+
* discard if this changes: passwords aren't (usually) database-specific.
1537+
*/
1538+
if (!dbname)
1539+
dbname = PQdb(o_conn);
1540+
15191541
/*
15201542
* If the user asked to be prompted for a password, ask for one now. If
15211543
* not, use the password from the old connection, provided the username
@@ -1530,7 +1552,7 @@ do_connect(char *dbname, char *user, char *host, char *port)
15301552
{
15311553
password = prompt_for_password(user);
15321554
}
1533-
else if (o_conn && user && strcmp(PQuser(o_conn), user) == 0)
1555+
else if (o_conn && keep_password)
15341556
{
15351557
password = strdup(PQpass(o_conn));
15361558
}
@@ -1540,23 +1562,30 @@ do_connect(char *dbname, char *user, char *host, char *port)
15401562
#define PARAMS_ARRAY_SIZE 8
15411563
const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
15421564
const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
1543-
1544-
keywords[0] = "host";
1545-
values[0] = host;
1546-
keywords[1] = "port";
1547-
values[1] = port;
1548-
keywords[2] = "user";
1549-
values[2] = user;
1550-
keywords[3] = "password";
1551-
values[3] = password;
1552-
keywords[4] = "dbname";
1553-
values[4] = dbname;
1554-
keywords[5] = "fallback_application_name";
1555-
values[5] = pset.progname;
1556-
keywords[6] = "client_encoding";
1557-
values[6] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
1558-
keywords[7] = NULL;
1559-
values[7] = NULL;
1565+
int paramnum = 0;
1566+
1567+
keywords[0] = "dbname";
1568+
values[0] = dbname;
1569+
1570+
if (!has_connection_string)
1571+
{
1572+
keywords[++paramnum] = "host";
1573+
values[paramnum] = host;
1574+
keywords[++paramnum] = "port";
1575+
values[paramnum] = port;
1576+
keywords[++paramnum] = "user";
1577+
values[paramnum] = user;
1578+
}
1579+
keywords[++paramnum] = "password";
1580+
values[paramnum] = password;
1581+
keywords[++paramnum] = "fallback_application_name";
1582+
values[paramnum] = pset.progname;
1583+
keywords[++paramnum] = "client_encoding";
1584+
values[paramnum] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
1585+
1586+
/* add array terminator */
1587+
keywords[++paramnum] = NULL;
1588+
values[paramnum] = NULL;
15601589

15611590
n_conn = PQconnectdbParams(keywords, values, true);
15621591

src/bin/psql/common.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,3 +1686,44 @@ expand_tilde(char **filename)
16861686

16871687
return;
16881688
}
1689+
1690+
/*
1691+
* Checks if connection string starts with either of the valid URI prefix
1692+
* designators.
1693+
*
1694+
* Returns the URI prefix length, 0 if the string doesn't contain a URI prefix.
1695+
*
1696+
* XXX This is a duplicate of the eponymous libpq function.
1697+
*/
1698+
static int
1699+
uri_prefix_length(const char *connstr)
1700+
{
1701+
/* The connection URI must start with either of the following designators: */
1702+
static const char uri_designator[] = "postgresql://";
1703+
static const char short_uri_designator[] = "postgres://";
1704+
1705+
if (strncmp(connstr, uri_designator,
1706+
sizeof(uri_designator) - 1) == 0)
1707+
return sizeof(uri_designator) - 1;
1708+
1709+
if (strncmp(connstr, short_uri_designator,
1710+
sizeof(short_uri_designator) - 1) == 0)
1711+
return sizeof(short_uri_designator) - 1;
1712+
1713+
return 0;
1714+
}
1715+
1716+
/*
1717+
* Recognized connection string either starts with a valid URI prefix or
1718+
* contains a "=" in it.
1719+
*
1720+
* Must be consistent with parse_connection_string: anything for which this
1721+
* returns true should at least look like it's parseable by that routine.
1722+
*
1723+
* XXX This is a duplicate of the eponymous libpq function.
1724+
*/
1725+
bool
1726+
recognized_connection_string(const char *connstr)
1727+
{
1728+
return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
1729+
}

src/bin/psql/common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,6 @@ extern const char *session_username(void);
6363

6464
extern void expand_tilde(char **filename);
6565

66+
extern bool recognized_connection_string(const char *connstr);
67+
6668
#endif /* COMMON_H */

src/bin/psql/help.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ slashUsage(unsigned short int pager)
256256
fprintf(output, "\n");
257257

258258
fprintf(output, _("Connection\n"));
259-
fprintf(output, _(" \\c[onnect] [DBNAME|- USER|- HOST|- PORT|-]\n"
260-
" connect to new database (currently \"%s\")\n"),
259+
fprintf(output, _(" \\c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}\n"
260+
" connect to new database (currently \"%s\")\n"),
261261
currdb);
262262
fprintf(output, _(" \\encoding [ENCODING] show or set client encoding\n"));
263263
fprintf(output, _(" \\password [USERNAME] securely change the password for a user\n"));

src/bin/psql/tab-complete.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2938,8 +2938,10 @@ psql_completion(char *text, int start, int end)
29382938
/* Backslash commands */
29392939
/* TODO: \dc \dd \dl */
29402940
else if (strcmp(prev_wd, "\\connect") == 0 || strcmp(prev_wd, "\\c") == 0)
2941-
COMPLETE_WITH_QUERY(Query_for_list_of_databases);
2942-
2941+
{
2942+
if (!recognized_connection_string(text))
2943+
COMPLETE_WITH_QUERY(Query_for_list_of_databases);
2944+
}
29432945
else if (strncmp(prev_wd, "\\da", strlen("\\da")) == 0)
29442946
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_aggregates, NULL);
29452947
else if (strncmp(prev_wd, "\\db", strlen("\\db")) == 0)

0 commit comments

Comments
 (0)