Skip to content

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

Open
DiegoDAF wants to merge 1 commit into
dbcli:mainfrom
DiegoDAF:upstream/list-keeps-connection-string
Open

Keep the connection string when using -l/--list or --ping#1621
DiegoDAF wants to merge 1 commit 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
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.

1 participant