From 8175745cfe305017f38fdedb8e8abd4bc7afe094 Mon Sep 17 00:00:00 2001 From: Matthew Wraith Date: Wed, 4 Aug 2021 15:22:21 -0700 Subject: [PATCH] Postgres defaults to unix socket with no host #229 Closes #229 When there's no host given in a postgres connection url, use the unix domain default socket. Previously, the default was a localhost TCP connection. This default behavior is more in line with standard postgres clients out there, like psql. --- pkg/driver/postgres/postgres.go | 2 +- pkg/driver/postgres/postgres_test.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/driver/postgres/postgres.go b/pkg/driver/postgres/postgres.go index 1917cb1..71b16e3 100644 --- a/pkg/driver/postgres/postgres.go +++ b/pkg/driver/postgres/postgres.go @@ -48,7 +48,7 @@ func connectionString(u *url.URL) string { // default hostname if hostname == "" { - hostname = "localhost" + query.Set("host", "/var/run/postgresql") } // host param overrides url hostname diff --git a/pkg/driver/postgres/postgres_test.go b/pkg/driver/postgres/postgres_test.go index f3f8793..50fb398 100644 --- a/pkg/driver/postgres/postgres_test.go +++ b/pkg/driver/postgres/postgres_test.go @@ -56,7 +56,7 @@ func TestConnectionString(t *testing.T) { expected string }{ // defaults - {"postgres:///foo", "postgres://localhost:5432/foo"}, + {"postgres:///foo", "postgres://:5432/foo?host=%2Fvar%2Frun%2Fpostgresql"}, // support custom url params {"postgres://bob:secret@myhost:1234/foo?bar=baz", "postgres://bob:secret@myhost:1234/foo?bar=baz"}, // support `host` and `port` via url params @@ -85,11 +85,11 @@ func TestConnectionArgsForDump(t *testing.T) { expected []string }{ // defaults - {"postgres:///foo", []string{"postgres://localhost:5432/foo"}}, + {"postgres:///foo", []string{"postgres://:5432/foo?host=%2Fvar%2Frun%2Fpostgresql"}}, // support single schema - {"postgres:///foo?search_path=foo", []string{"--schema", "foo", "postgres://localhost:5432/foo"}}, + {"postgres:///foo?search_path=foo", []string{"--schema", "foo", "postgres://:5432/foo?host=%2Fvar%2Frun%2Fpostgresql"}}, // support multiple schemas - {"postgres:///foo?search_path=foo,public", []string{"--schema", "foo", "--schema", "public", "postgres://localhost:5432/foo"}}, + {"postgres:///foo?search_path=foo,public", []string{"--schema", "foo", "--schema", "public", "postgres://:5432/foo?host=%2Fvar%2Frun%2Fpostgresql"}}, } for _, c := range cases {