summaryrefslogtreecommitdiff
path: root/src/bin/psql/startup.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/psql/startup.c')
-rw-r--r--src/bin/psql/startup.c31
1 files changed, 21 insertions, 10 deletions
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index ec6ae45b245..be57574cd32 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -101,7 +101,6 @@ main(int argc, char *argv[])
int successResult;
bool have_password = false;
char password[100];
- char *password_prompt = NULL;
bool new_pass;
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("psql"));
@@ -205,15 +204,14 @@ main(int argc, char *argv[])
pset.popt.topt.recordSep.separator_zero = false;
}
- if (options.username == NULL)
- password_prompt = pg_strdup(_("Password: "));
- else
- password_prompt = psprintf(_("Password for user %s: "),
- options.username);
-
if (pset.getPassword == TRI_YES)
{
- simple_prompt(password_prompt, password, sizeof(password), false);
+ /*
+ * We can't be sure yet of the username that will be used, so don't
+ * offer a potentially wrong one. Typical uses of this option are
+ * noninteractive anyway.
+ */
+ simple_prompt("Password: ", password, sizeof(password), false);
have_password = true;
}
@@ -252,15 +250,28 @@ main(int argc, char *argv[])
!have_password &&
pset.getPassword != TRI_NO)
{
+ /*
+ * Before closing the old PGconn, extract the user name that was
+ * actually connected with --- it might've come out of a URI or
+ * connstring "database name" rather than options.username.
+ */
+ const char *realusername = PQuser(pset.db);
+ char *password_prompt;
+
+ if (realusername && realusername[0])
+ password_prompt = psprintf(_("Password for user %s: "),
+ realusername);
+ else
+ password_prompt = pg_strdup(_("Password: "));
PQfinish(pset.db);
+
simple_prompt(password_prompt, password, sizeof(password), false);
+ free(password_prompt);
have_password = true;
new_pass = true;
}
} while (new_pass);
- free(password_prompt);
-
if (PQstatus(pset.db) == CONNECTION_BAD)
{
fprintf(stderr, "%s: %s", pset.progname, PQerrorMessage(pset.db));