Skip to content

Fix empty user in connection string defaulting to OS user#2496

Merged
jackc merged 2 commits intojackc:masterfrom
shv-ng:fix-empty-user-default
Feb 2, 2026
Merged

Fix empty user in connection string defaulting to OS user#2496
jackc merged 2 commits intojackc:masterfrom
shv-ng:fix-empty-user-default

Conversation

@shv-ng
Copy link
Contributor

@shv-ng shv-ng commented Feb 1, 2026

Fixes #2495

Defaults the PostgreSQL user to the current OS user when the connection string explicitly specifies an empty user= parameter, matching libpq, psql, and asyncpg behavior.

What

Default the PostgreSQL user to the current OS user when the connection string
explicitly specifies an empty user= parameter.

Why

libpq and other drivers (e.g. psql, asyncpg) treat user= as a signal to fall
back to the current OS user. pgx currently leaves the user empty, which causes
the server to reject the startup packet with SQLSTATE 28000.

How

  • Detect when user is explicitly present but empty
  • Default Config.User using os/user.Current()
  • Ignore lookup errors to avoid introducing new failure modes

Testing

  • Added test covering explicit empty user= behavior

@jackc
Copy link
Owner

jackc commented Feb 1, 2026

We already get the OS user in defaultSettings(). The problem is it is being overridden by the explicitly set user. Instead of duplicating the logic to get the OS user, perhaps it would be better not to overwrite user with an empty string. This could be done in mergeSettings() or in parseURLSettings() and parseKeywordValueSettings().

@shv-ng
Copy link
Contributor Author

shv-ng commented Feb 1, 2026

Thanks, that makes sense.

I’ve updated the patch to avoid duplicating the OS user lookup. The fix now
ignores explicitly empty user values during parsing (both URL and
keyword/value), allowing the default user from defaultSettings() to remain.
I also added tests covering keyword and URL forms, with and without passwords.

@jackc jackc merged commit f56ca73 into jackc:master Feb 2, 2026
14 checks passed
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.

Empty user in connection string does not work

2 participants