Skip to content

Commit 7e5e1bb

Browse files
committed
Fix recently-introduced breakage in psql's \connect command.
Through my misreading of what the existing code actually did, commits 85c5428 et al. broke psql's behavior for the case where "\c connstring" provides a password in the connstring. We should use that password in such a case, but as of 85c5428 we ignored it (and instead, prompted for a password). Commit 94929f1 fixed that in HEAD, but since I thought it was cleaning up a longstanding misbehavior and not one I'd just created, I didn't back-patch it. Hence, back-patch the portions of 94929f1 having to do with password management. In addition to fixing the introduced bug, this means that "\c -reuse-previous=on connstring" will allow re-use of an existing connection's password if the connstring doesn't change user/host/port. That didn't happen before, but it seems like a bug fix, and anyway I'm loath to have significant differences in this code across versions. Also fix an error with the same root cause about whether or not to override a connstring's setting of client_encoding. As of 85c5428 we always did so; restore the previous behavior of overriding only when stdin/stdout are a terminal and there's no environment setting of PGCLIENTENCODING. (I find that definition a bit surprising, but right now doesn't seem like the time to revisit it.) Per bug #16746 from Krzysztof Gradek. As with the previous patch, back-patch to all supported branches. Discussion: https://postgr.es/m/16746-44b30e2edf4335d4@postgresql.org
1 parent d5e2bdf commit 7e5e1bb

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/bin/psql/command.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3030,6 +3030,7 @@ do_connect(enum trivalue reuse_previous_specification,
30303030
int nconnopts = 0;
30313031
bool same_host = false;
30323032
char *password = NULL;
3033+
char *client_encoding;
30333034
bool success = true;
30343035
bool keep_password = true;
30353036
bool has_connection_string;
@@ -3239,6 +3240,16 @@ do_connect(enum trivalue reuse_previous_specification,
32393240
password = prompt_for_password(has_connection_string ? NULL : user);
32403241
}
32413242

3243+
/*
3244+
* Consider whether to force client_encoding to "auto" (overriding
3245+
* anything in the connection string). We do so if we have a terminal
3246+
* connection and there is no PGCLIENTENCODING environment setting.
3247+
*/
3248+
if (pset.notty || getenv("PGCLIENTENCODING"))
3249+
client_encoding = NULL;
3250+
else
3251+
client_encoding = "auto";
3252+
32423253
/* Loop till we have a connection or fail, which we might've already */
32433254
while (success)
32443255
{
@@ -3279,8 +3290,9 @@ do_connect(enum trivalue reuse_previous_specification,
32793290
values[paramnum++] = password;
32803291
else if (strcmp(ci->keyword, "fallback_application_name") == 0)
32813292
values[paramnum++] = pset.progname;
3282-
else if (strcmp(ci->keyword, "client_encoding") == 0)
3283-
values[paramnum++] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
3293+
else if (client_encoding &&
3294+
strcmp(ci->keyword, "client_encoding") == 0)
3295+
values[paramnum++] = client_encoding;
32843296
else if (ci->val)
32853297
values[paramnum++] = ci->val;
32863298
/* else, don't bother making libpq parse this keyword */

0 commit comments

Comments
 (0)