Skip to content

Commit f4540ca

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 44f8f56 commit f4540ca

File tree

6 files changed

+134
-37
lines changed

6 files changed

+134
-37
lines changed

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

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

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

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

src/bin/psql/command.c

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,8 @@ do_connect(char *dbname, char *user, char *host, char *port)
15591559
PGconn *o_conn = pset.db,
15601560
*n_conn;
15611561
char *password = NULL;
1562+
bool keep_password;
1563+
bool has_connection_string;
15621564

15631565
if (!o_conn && (!dbname || !user || !host || !port))
15641566
{
@@ -1572,15 +1574,35 @@ do_connect(char *dbname, char *user, char *host, char *port)
15721574
return false;
15731575
}
15741576

1575-
if (!dbname)
1576-
dbname = PQdb(o_conn);
1577+
/* grab values from the old connection, unless supplied by caller */
15771578
if (!user)
15781579
user = PQuser(o_conn);
15791580
if (!host)
15801581
host = PQhost(o_conn);
15811582
if (!port)
15821583
port = PQport(o_conn);
15831584

1585+
has_connection_string =
1586+
dbname ? recognized_connection_string(dbname) : false;
1587+
1588+
/*
1589+
* Any change in the parameters read above makes us discard the password.
1590+
* We also discard it if we're to use a conninfo rather than the positional
1591+
* syntax.
1592+
*/
1593+
keep_password =
1594+
((strcmp(user, PQuser(o_conn)) == 0) &&
1595+
(!host || strcmp(host, PQhost(o_conn)) == 0) &&
1596+
(strcmp(port, PQport(o_conn)) == 0) &&
1597+
!has_connection_string);
1598+
1599+
/*
1600+
* Grab dbname from old connection unless supplied by caller. No password
1601+
* discard if this changes: passwords aren't (usually) database-specific.
1602+
*/
1603+
if (!dbname)
1604+
dbname = PQdb(o_conn);
1605+
15841606
/*
15851607
* If the user asked to be prompted for a password, ask for one now. If
15861608
* not, use the password from the old connection, provided the username
@@ -1595,42 +1617,53 @@ do_connect(char *dbname, char *user, char *host, char *port)
15951617
{
15961618
password = prompt_for_password(user);
15971619
}
1598-
else if (o_conn && user && strcmp(PQuser(o_conn), user) == 0)
1620+
else if (o_conn && keep_password)
15991621
{
1600-
password = pg_strdup(PQpass(o_conn));
1622+
password = PQpass(o_conn);
1623+
if (password && *password)
1624+
password = pg_strdup(password);
1625+
else
1626+
password = NULL;
16011627
}
16021628

16031629
while (true)
16041630
{
16051631
#define PARAMS_ARRAY_SIZE 8
16061632
const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
16071633
const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
1634+
int paramnum = 0;
1635+
1636+
keywords[0] = "dbname";
1637+
values[0] = dbname;
1638+
1639+
if (!has_connection_string)
1640+
{
1641+
keywords[++paramnum] = "host";
1642+
values[paramnum] = host;
1643+
keywords[++paramnum] = "port";
1644+
values[paramnum] = port;
1645+
keywords[++paramnum] = "user";
1646+
values[paramnum] = user;
1647+
}
1648+
keywords[++paramnum] = "password";
1649+
values[paramnum] = password;
1650+
keywords[++paramnum] = "fallback_application_name";
1651+
values[paramnum] = pset.progname;
1652+
keywords[++paramnum] = "client_encoding";
1653+
values[paramnum] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
16081654

1609-
keywords[0] = "host";
1610-
values[0] = host;
1611-
keywords[1] = "port";
1612-
values[1] = port;
1613-
keywords[2] = "user";
1614-
values[2] = user;
1615-
keywords[3] = "password";
1616-
values[3] = password;
1617-
keywords[4] = "dbname";
1618-
values[4] = dbname;
1619-
keywords[5] = "fallback_application_name";
1620-
values[5] = pset.progname;
1621-
keywords[6] = "client_encoding";
1622-
values[6] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
1623-
keywords[7] = NULL;
1624-
values[7] = NULL;
1655+
/* add array terminator */
1656+
keywords[++paramnum] = NULL;
1657+
values[paramnum] = NULL;
16251658

16261659
n_conn = PQconnectdbParams(keywords, values, true);
16271660

1628-
free(keywords);
1629-
free(values);
1661+
pg_free(keywords);
1662+
pg_free(values);
16301663

16311664
/* We can immediately discard the password -- no longer needed */
16321665
if (password)
1633-
free(password);
1666+
pg_free(password);
16341667

16351668
if (PQstatus(n_conn) == CONNECTION_OK)
16361669
break;

src/bin/psql/common.c

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

17151715
return;
17161716
}
1717+
1718+
/*
1719+
* Checks if connection string starts with either of the valid URI prefix
1720+
* designators.
1721+
*
1722+
* Returns the URI prefix length, 0 if the string doesn't contain a URI prefix.
1723+
*
1724+
* XXX This is a duplicate of the eponymous libpq function.
1725+
*/
1726+
static int
1727+
uri_prefix_length(const char *connstr)
1728+
{
1729+
/* The connection URI must start with either of the following designators: */
1730+
static const char uri_designator[] = "postgresql://";
1731+
static const char short_uri_designator[] = "postgres://";
1732+
1733+
if (strncmp(connstr, uri_designator,
1734+
sizeof(uri_designator) - 1) == 0)
1735+
return sizeof(uri_designator) - 1;
1736+
1737+
if (strncmp(connstr, short_uri_designator,
1738+
sizeof(short_uri_designator) - 1) == 0)
1739+
return sizeof(short_uri_designator) - 1;
1740+
1741+
return 0;
1742+
}
1743+
1744+
/*
1745+
* Recognized connection string either starts with a valid URI prefix or
1746+
* contains a "=" in it.
1747+
*
1748+
* Must be consistent with parse_connection_string: anything for which this
1749+
* returns true should at least look like it's parseable by that routine.
1750+
*
1751+
* XXX This is a duplicate of the eponymous libpq function.
1752+
*/
1753+
bool
1754+
recognized_connection_string(const char *connstr)
1755+
{
1756+
return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
1757+
}

src/bin/psql/common.h

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

4747
extern void expand_tilde(char **filename);
4848

49+
extern bool recognized_connection_string(const char *connstr);
50+
4951
#endif /* COMMON_H */

src/bin/psql/help.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,11 @@ slashUsage(unsigned short int pager)
259259

260260
fprintf(output, _("Connection\n"));
261261
if (currdb)
262-
fprintf(output, _(" \\c[onnect] [DBNAME|- USER|- HOST|- PORT|-]\n"
262+
fprintf(output, _(" \\c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}\n"
263263
" connect to new database (currently \"%s\")\n"),
264264
currdb);
265265
else
266-
fprintf(output, _(" \\c[onnect] [DBNAME|- USER|- HOST|- PORT|-]\n"
266+
fprintf(output, _(" \\c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}\n"
267267
" connect to new database (currently no connection)\n"));
268268
fprintf(output, _(" \\encoding [ENCODING] show or set client encoding\n"));
269269
fprintf(output, _(" \\password [USERNAME] securely change the password for a user\n"));

src/bin/psql/tab-complete.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3256,7 +3256,10 @@ psql_completion(char *text, int start, int end)
32563256
/* Backslash commands */
32573257
/* TODO: \dc \dd \dl */
32583258
else if (strcmp(prev_wd, "\\connect") == 0 || strcmp(prev_wd, "\\c") == 0)
3259-
COMPLETE_WITH_QUERY(Query_for_list_of_databases);
3259+
{
3260+
if (!recognized_connection_string(text))
3261+
COMPLETE_WITH_QUERY(Query_for_list_of_databases);
3262+
}
32603263

32613264
else if (strncmp(prev_wd, "\\da", strlen("\\da")) == 0)
32623265
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_aggregates, NULL);

0 commit comments

Comments
 (0)