Keep the connection string when using -l/--list or --ping - #1621
Open
DiegoDAF wants to merge 1 commit into
Open
Conversation
`pgcli "postgresql://user@host:5432/db?sslmode=verify-ca" -l` fails with `role "<osuser>" does not exist` on a local socket, while the same connection string without -l connects fine. cli() replaces the positional argument with "postgres" whenever --list/--ping is given, on the grounds that those options do not take a db name. But a connection string is not a db name: a URI or a key=value conninfo carries the whole connection (host, user, port, sslmode, ...), and discarding it leaves pgcli with no connection details at all, so it falls back to a local socket connection as the OS user. Only a plain database name is discarded now. A connection string is passed through untouched, and if it names no database, "postgres" is merged in for the listing, since libpq would otherwise default to the OS user name, which is rarely an existing database. Adds five tests covering both connection-string forms with -l and --ping, the missing-dbname fallback, and the unchanged plain-db-name behaviour.
DiegoDAF
added a commit
to DiegoDAF/pgcli.daf
that referenced
this pull request
Aug 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
A connection string is silently discarded when
-l/--listor--pingisused, and pgcli then tries a local socket connection as the OS user:
The cause is in
cli():The intent is right for a plain db name, but the positional argument may also
be a connection string, and that carries the entire connection: host, user,
port, sslmode, sslrootcert. Replacing it with
"postgres"throws all of thataway, so the branch dispatcher below falls through to
pgcli.connect("postgres", host, user, port)with empty host and user.The same applies to a
key=valueconninfo string, which is the only way topass parameters like
sslmodewhen using the-h/-Uform.Change
Discard the positional argument only when it is a plain database name. A
connection string is passed through untouched.
One extra case: if the connection string names no database,
"postgres"ismerged in for the listing. libpq would otherwise default the database to the
user name, which is rarely an existing database, so
pgcli "host=h user=u sslmode=verify-ca" -lwould fail for a different reason.Validation
Five new tests in
tests/test_main.py: URI preserved with-l,key=valueconninfo preserved with
-l, URI preserved with--ping, the missing-dbnamefallback (checking the rest of the conninfo survives), and a plain db name
still being discarded. Four of the five fail on current main.
Verified end to end against a local server for all three paths (URI, conninfo
with and without a dbname, and no connection string at all). Full suite green
locally (2735 passed).
Checklist
changelog.rst.Not a feature from my list in discussion #1603: this is one of the upstream bugs I ran into while maintaining the fork, listed in the status section at the bottom of that discussion.