Keep the connection string when using -l/--list or --ping - #1621
Merged
j-bennet merged 4 commits intoAug 31, 2026
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
…-connection-string # Conflicts: # tests/test_main.py
j-bennet
reviewed
Aug 30, 2026
Comment on lines
+1694
to
+1701
| if not is_conn_string: | ||
| database = "postgres" | ||
| else: | ||
| try: | ||
| if not conninfo_to_dict(database).get("dbname"): | ||
| database = make_conninfo(database, dbname="postgres") | ||
| except Exception: | ||
| pass # invalid conninfo: let the connection attempt report it |
Contributor
There was a problem hiding this comment.
Perhaps we can simplify the condition here. We only want to override the database name with postgres when the user didn't provide one, right? How about this:
if not database:
database = "postgres"
elif is_conn_string and not conninfo_to_dict(database).get("dbname"):
database = make_conninfo(database, dbname="postgres")
Contributor
Author
There was a problem hiding this comment.
Agreed, and applied, with one interesting confirmation: your version is actually closer to psql than mine was. I checked against real psql: psql -l nonexistentdb fails with "database does not exist", so psql connects to the named database and lists from there rather than discarding it. The test now documents that.
Two small deltas from your snippet:
- Kept the
try/exceptaroundconninfo_to_dict, so a malformed connection string is reported by the connection attempt instead of a traceback. elif is_conn_string and not conninfo_to_dict(...)stayed as a nestedifinside theelif is_conn_string, purely for the except to wrap only the parse.
Changelog updated to match the new behavior.
Review suggestion: only fall back to "postgres" when no database was given at all. A plain database name is now kept, which is what psql does (psql -l nonexistent fails with "database does not exist", so psql connects to the named database and lists from there). The try/except around conninfo_to_dict stays: a malformed connection string is reported by the connection attempt instead of a traceback.
Contributor
|
Thanks @DiegoDAF , merging. |
DiegoDAF
added a commit
to DiegoDAF/pgcli.daf
that referenced
this pull request
Sep 1, 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.