From 444cd1a2db3d747d68bafe18c8a773aa3c04a096 Mon Sep 17 00:00:00 2001 From: simple-minded <191973953+hakarrr@users.noreply.github.com> Date: Wed, 2 Dec 2026 17:46:47 +0530 Subject: [PATCH 1/2] fix(config): bracket an IPv6 database host in the assembled DSN Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com> --- config/dsn_host_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 config/dsn_host_test.go diff --git a/config/dsn_host_test.go b/config/dsn_host_test.go new file mode 100644 index 000000000..2424199ba --- /dev/null +++ b/config/dsn_host_test.go @@ -0,0 +1,28 @@ +package config + +import ( + "testing" + + "github.com/jackc/pgx/v5/pgconn" +) + +func TestBuildDSNHost(t *testing.T) { + cases := []struct{ host, want string }{ + {"", "127.0.0.1"}, + {"db.internal", "db.internal"}, + {"10.0.0.5", "10.0.0.5"}, + {"::1", "::1"}, + {"2001:db8::5", "2001:db8::5"}, + {"[2001:db8::5]", "2001:db8::5"}, + } + for _, tc := range cases { + dsn := Database{Host: tc.host, Port: 5433, User: "u", DBName: "artex"}.buildDSN() + cfg, err := pgconn.ParseConfig(dsn) + if err != nil { + t.Fatalf("host %q: DSN %q does not parse: %v", tc.host, dsn, err) + } + if cfg.Host != tc.want || cfg.Port != 5433 { + t.Errorf("host %q: pgx reads host %q port %d from %q, want %q 5433", tc.host, cfg.Host, cfg.Port, dsn, tc.want) + } + } +} From 7ec3149efa63291ef49e664aba45a98f6ce6097e Mon Sep 17 00:00:00 2001 From: simple-minded <191973953+hakarrr@users.noreply.github.com> Date: Wed, 2 Dec 2026 17:47:20 +0530 Subject: [PATCH 2/2] fix(config): bracket an IPv6 database host in the assembled DSN Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com> --- config/config.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index c4691f898..9e01878f0 100644 --- a/config/config.go +++ b/config/config.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "io/fs" + "net" "net/url" "os" "path/filepath" @@ -230,6 +231,9 @@ func (d Database) buildDSN() string { if host == "" { host = "127.0.0.1" } + // A bare IPv6 address needs brackets in a URL authority; JoinHostPort adds + // them, so drop any the operator already typed. + host = strings.TrimSuffix(strings.TrimPrefix(host, "["), "]") port := d.Port if port == 0 { port = 5432 @@ -240,7 +244,7 @@ func (d Database) buildDSN() string { } u := url.URL{ Scheme: "postgres", - Host: host + ":" + strconv.Itoa(port), + Host: net.JoinHostPort(host, strconv.Itoa(port)), Path: "/" + d.DBName, } if d.User != "" {