• Tom Lane's avatar
    Avoid misleading psql password prompt when username is multiply specified. · 15be2746
    Tom Lane authored
    When a password is needed, cases such as
    	psql -d "postgresql://alice@localhost/testdb" -U bob
    would incorrectly prompt for "Password for user bob: ", when actually the
    connection will be attempted with username alice.  The priority order of
    which name to use isn't that important here, but the misleading prompt is.
    
    When we are prompting for a password after initial connection failure,
    we can fix this reliably by looking at PQuser(conn) to see how libpq
    interpreted the connection arguments.  But when we're doing a forced
    password prompt because of a -W switch, we can't use that solution.
    Fortunately, because the main use of -W is for noninteractive situations,
    it's less critical to produce a helpful prompt in such cases.  I made
    the startup prompt for -W just say "Password: " all the time, rather
    than expending extra code on trying to identify which username to use.
    In the case of a \c command (after -W has been given), there's already
    logic in do_connect that determines whether the "dbname" is a connstring
    or URI, so we can avoid lobotomizing the prompt except in cases that
    are actually dubious.  (We could do similarly in startup.c if anyone
    complains, but for now it seems not worthwhile, especially since that
    would still be only a partial solution.)
    
    Per bug #15025 from Akos Vandra.  Although this is arguably a bug fix,
    it doesn't seem worth back-patching.  The case where it matters seems
    like a very corner-case usage, and someone might complain that we'd
    changed the behavior of -W in a minor release.
    
    Discussion: https://postgr.es/m/20180123130013.7407.24749@wrigleys.postgresql.org
    15be2746
startup.c 27.7 KB