From c0725c8d0b7e9c4e0706157b9d9ec00e1d4cb00e Mon Sep 17 00:00:00 2001 From: ShivangSrivastava Date: Sun, 1 Feb 2026 16:41:49 +0530 Subject: [PATCH 1/2] pgconn: default empty user to current OS user --- pgconn/config.go | 8 ++++++++ pgconn/config_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/pgconn/config.go b/pgconn/config.go index a4cee7675..f2383d06a 100644 --- a/pgconn/config.go +++ b/pgconn/config.go @@ -13,6 +13,7 @@ import ( "net" "net/url" "os" + "os/user" "path/filepath" "strconv" "strings" @@ -304,6 +305,13 @@ func ParseConfigWithOptions(connString string, options ParseConfigOptions) (*Con }, } + if _, present := settings["user"]; present && config.User == "" { + currentUser, err := user.Current() + if err == nil { + config.User = currentUser.Username + } + } + if connectTimeoutSetting, present := settings["connect_timeout"]; present { connectTimeout, err := parseConnectTimeoutSetting(connectTimeoutSetting) if err != nil { diff --git a/pgconn/config_test.go b/pgconn/config_test.go index 6ac6bec15..f6c4f68ba 100644 --- a/pgconn/config_test.go +++ b/pgconn/config_test.go @@ -1145,3 +1145,41 @@ application_name = spaced string assertConfigsEqual(t, tt.config, config, fmt.Sprintf("Test %d (%s)", i, tt.name)) } } + +func TestParseConfigExplicitEmptyUserDefaultsToOSUser(t *testing.T) { + skipOnWindows(t) + clearPgEnvvars(t) + + currentUser, err := user.Current() + if err != nil { + t.Skip("cannot determine current OS user") + } + + tests := []struct { + name string + connString string + expected string + }{ + { + name: "explicit empty user parameter", + connString: "host=localhost dbname=test user=", + expected: currentUser.Username, + }, + } + + for i, tt := range tests { + config, err := pgconn.ParseConfig(tt.connString) + if !assert.NoErrorf(t, err, "Test %d (%s)", i, tt.name) { + continue + } + + assert.Equalf( + t, + tt.expected, + config.User, + "Test %d (%s): unexpected user", + i, + tt.name, + ) + } +} From 17b628da64e0e1e110ef01a7bd64e3c06696dca1 Mon Sep 17 00:00:00 2001 From: ShivangSrivastava Date: Sun, 1 Feb 2026 21:28:02 +0530 Subject: [PATCH 2/2] pgconn: ignore explicit empty user in connection string --- pgconn/config.go | 15 ++++++--------- pgconn/config_test.go | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/pgconn/config.go b/pgconn/config.go index f2383d06a..42bc2e92b 100644 --- a/pgconn/config.go +++ b/pgconn/config.go @@ -13,7 +13,6 @@ import ( "net" "net/url" "os" - "os/user" "path/filepath" "strconv" "strings" @@ -305,13 +304,6 @@ func ParseConfigWithOptions(connString string, options ParseConfigOptions) (*Con }, } - if _, present := settings["user"]; present && config.User == "" { - currentUser, err := user.Current() - if err == nil { - config.User = currentUser.Username - } - } - if connectTimeoutSetting, present := settings["connect_timeout"]; present { connectTimeout, err := parseConnectTimeoutSetting(connectTimeoutSetting) if err != nil { @@ -499,7 +491,9 @@ func parseURLSettings(connString string) (map[string]string, error) { } if parsedURL.User != nil { - settings["user"] = parsedURL.User.Username() + if u := parsedURL.User.Username(); u != "" { + settings["user"] = u + } if password, present := parsedURL.User.Password(); present { settings["password"] = password } @@ -626,6 +620,9 @@ func parseKeywordValueSettings(s string) (map[string]string, error) { return nil, errors.New("invalid keyword/value") } + if key == "user" && val == "" { + continue + } settings[key] = val } diff --git a/pgconn/config_test.go b/pgconn/config_test.go index f6c4f68ba..9665a6e15 100644 --- a/pgconn/config_test.go +++ b/pgconn/config_test.go @@ -1161,10 +1161,25 @@ func TestParseConfigExplicitEmptyUserDefaultsToOSUser(t *testing.T) { expected string }{ { - name: "explicit empty user parameter", + name: "keyword value explicit empty user", connString: "host=localhost dbname=test user=", expected: currentUser.Username, }, + { + name: "keyword value quoted empty user", + connString: "host=localhost dbname=test user=''", + expected: currentUser.Username, + }, + { + name: "url explicit empty user without password", + connString: "postgres://@localhost/test", + expected: currentUser.Username, + }, + { + name: "url explicit empty user with password", + connString: "postgres://:secret@localhost/test", + expected: currentUser.Username, + }, } for i, tt := range tests {