Skip to content

Keep the connection string when using -l/--list or --ping - #1621

Merged
j-bennet merged 4 commits into
dbcli:mainfrom
DiegoDAF:upstream/list-keeps-connection-string
Aug 31, 2026
Merged

Keep the connection string when using -l/--list or --ping#1621
j-bennet merged 4 commits into
dbcli:mainfrom
DiegoDAF:upstream/list-keeps-connection-string

Conversation

@DiegoDAF

@DiegoDAF DiegoDAF commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Description

A connection string is silently discarded when -l/--list or --ping is
used, and pgcli then tries a local socket connection as the OS user:

$ pgcli "postgresql://someuser@somehost:5432/somedb?sslmode=verify-ca"
someuser@somehost:somedb>            # connects fine

$ pgcli "postgresql://someuser@somehost:5432/somedb?sslmode=verify-ca" -l
connection failed: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432"
failed: FATAL:  role "<osuser>" does not exist

The cause is in cli():

# because option --ping, --list or -l are not supposed to have a db name
if list_databases or ping_database:
    database = "postgres"

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 that
away, 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=value conninfo string, which is the only way to
pass parameters like sslmode when using the -h/-U form.

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" is
merged 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" -l would fail for a different reason.

Validation

Five new tests in tests/test_main.py: URI preserved with -l, key=value
conninfo preserved with -l, URI preserved with --ping, the missing-dbname
fallback (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

  • I've added this contribution to the 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.

`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
Comment thread pgcli/main.py Outdated
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/except around conninfo_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 nested if inside the elif 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.
@j-bennet

Copy link
Copy Markdown
Contributor

Thanks @DiegoDAF , merging.

@j-bennet
j-bennet merged commit 9da9c8d into dbcli:main Aug 31, 2026
7 checks passed
DiegoDAF added a commit to DiegoDAF/pgcli.daf that referenced this pull request Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants