Skip to content

Commit 42ab0ff

Browse files
committed
Change default sslmode from "require" to "prefer"
Now that sslmode=prefer is supported, we can use that as the default. "prefer" is the default used by libpq, pgx, and pretty much anything else connecting to PostgreSQL. I can't really find anything else that uses "require" as a default. You could argue that "require" is a better default, but: 1. "require" doesn't actually verify the certificates and accepts any certificate. I wouldn't say it's completely useless for production systems, but it does seem useless-adjacent. If you want SSL, you almost always want "verify-ca" or "verify-full". 2. Copy/pasting a connection string used by pq to something else will mean you silently drop the ssl so it's a very brittle default. 3. It's an annoying default because copy pasting a connection string from anything else to pq doesn't work. pq has used "require" as a default for a long time, but because it's such a brittle default that's easy to lose by using $anything_else I think it's okay to change. I will leave this open for a bit before merging, so if anyone objects it can be re-considered.
1 parent 6d40f13 commit 42ab0ff

8 files changed

Lines changed: 30 additions & 22 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
unreleased
2+
----------
3+
4+
- This release changes the default `sslmode` from `require` to `prefer`, which
5+
is the default used by libpq and the rest of the PostgreSQL ecosystem. See
6+
[#1271] for some background.
7+
18
v1.12.0 (2026-03-18)
29
--------------------
310

‎conn.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ restartAll:
260260
)
261261
for _, cfg := range c.cfg.hosts() {
262262
mode := cfg.SSLMode
263+
if mode == "" {
264+
mode = SSLModePrefer
265+
}
263266
restartHost:
264267
if debugProto {
265268
fmt.Fprintln(os.Stderr, "CONNECT ", cfg.string())

‎connector.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ func newConfig(dsn string, env []string) (Config, error) {
547547
Host: "localhost",
548548
Port: 5432,
549549
SSLSNI: true,
550+
SSLMode: SSLModePrefer,
550551
MinProtocolVersion: "3.0",
551552
MaxProtocolVersion: "3.0",
552553
}
@@ -1040,7 +1041,7 @@ func (cfg Config) string() string {
10401041
switch k {
10411042
case "datestyle", "client_encoding":
10421043
continue
1043-
case "host", "port", "user", "sslsni", "min_protocol_version", "max_protocol_version":
1044+
case "host", "port", "user", "sslsni", "sslmode", "min_protocol_version", "max_protocol_version":
10441045
if !cfg.isset(k) {
10451046
continue
10461047
}

‎connector_test.go‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -354,14 +354,14 @@ func TestNewConfig(t *testing.T) {
354354
}{
355355
// Override defaults
356356
{"", nil, "", ""},
357-
{"user=u port=1 host=example.com", nil,
358-
"host=example.com port=1 user=u", ""},
359-
{"", []string{"PGUSER=u", "PGPORT=1", "PGHOST=example.com"},
360-
"host=example.com port=1 user=u", ""},
357+
{"user=u port=1 host=example.com sslmode=verify-ca", nil,
358+
"host=example.com port=1 sslmode=verify-ca user=u", ""},
359+
{"", []string{"PGUSER=u", "PGPORT=1", "PGHOST=example.com", "PGSSLMODE=verify-ca"},
360+
"host=example.com port=1 sslmode=verify-ca user=u", ""},
361361

362362
// Socket
363-
{"host=/var/run/psql", nil, "host=/var/run/psql sslmode=disable", ""},
364-
{"host=@/var/run/psql", nil, "host=@/var/run/psql sslmode=disable", ""},
363+
{"host=/var/run/psql", nil, "host=/var/run/psql", ""},
364+
{"host=@/var/run/psql", nil, "host=@/var/run/psql", ""},
365365
{"host=/var/run/psql sslmode=require", nil, "host=/var/run/psql sslmode=disable", ""},
366366

367367
// Empty value, value with space, and value with escaped \'
@@ -875,16 +875,16 @@ func TestService(t *testing.T) {
875875
wantErr string
876876
}{
877877
{"service=doesntexist", nil, ``, `definition of service "doesntexist" not found`},
878-
{"service=svc1", nil, `connect_timeout=20 dbname=xyz host=firsthost port=1234 service=svc1 sslmode=disable user=pqgo`, ``},
879-
{"service=svc2", nil, `connect_timeout=20 dbname='with space' host=localhost service=svc2 sslmode=disable user=''`, ``},
878+
{"service=svc1", nil, `connect_timeout=20 dbname=xyz host=firsthost port=1234 service=svc1 user=pqgo`, ``},
879+
{"service=svc2", nil, `connect_timeout=20 dbname='with space' host=localhost service=svc2 user=''`, ``},
880880
{"service=svc3", nil, ``, `pq: unknown setting "unknown" in service file for service "svc3"`},
881-
{"service=svc4", nil, `connect_timeout=20 dbname=pqgo host=localhost service=svc4 sslmode=disable user=pqgo`, ``},
882-
{"service=svc5", nil, `connect_timeout=20 dbname=pqgo host=localhost service=svc5 sslmode=disable user=pqgo`, ``},
881+
{"service=svc4", nil, `connect_timeout=20 dbname=pqgo host=localhost service=svc4 user=pqgo`, ``},
882+
{"service=svc5", nil, `connect_timeout=20 dbname=pqgo host=localhost service=svc5 user=pqgo`, ``},
883883

884884
{"service=svc5", map[string]string{"PGSERVICEFILE": "none"}, ``, `service file "none" not found`},
885885
{"service=svc1", map[string]string{"PGSERVICEFILE": filepath.Join(h, "other")}, ``, `definition of service "svc1" not found`},
886886
{"service=other", map[string]string{"PGSERVICEFILE": filepath.Join(h, "other")},
887-
`connect_timeout=20 dbname=other host=localhost service=other sslmode=disable user=pqgo`, ``},
887+
`connect_timeout=20 dbname=other host=localhost service=other user=pqgo`, ``},
888888
}
889889

890890
pqtest.Write(t, []byte("[other]\ndbname=other"), h, "other")

‎error_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func TestRetryError(t *testing.T) {
232232
}
233233

234234
func TestNetworkError(t *testing.T) {
235-
c, err := NewConnector("")
235+
c, err := NewConnector("sslmode=disable")
236236
if err != nil {
237237
t.Fatal(err)
238238
}

‎internal/pqtest/pqtest.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ func DSN(conninfo string) string {
7272
defaultTo("PGHOST", "localhost")
7373
defaultTo("PGDATABASE", "pqgo")
7474
defaultTo("PGUSER", "pqgo")
75-
defaultTo("PGSSLMODE", "disable")
7675
defaultTo("PGCONNECT_TIMEOUT", "20")
7776
})
7877

‎ssl.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func ssl(cfg Config, mode SSLMode) (func(net.Conn) (net.Conn, error), error) {
8282
case mode == SSLModeDisable || mode == SSLModeAllow:
8383
return nil, nil
8484

85-
case mode == "" || mode == SSLModeRequire || mode == SSLModePrefer:
85+
case mode == SSLModeRequire || mode == SSLModePrefer:
8686
// Skip TLS's own verification since it requires full verification.
8787
tlsConf.InsecureSkipVerify = true
8888

‎ssl_test.go‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"fmt"
1010
"io"
1111
"net"
12-
"os"
1312
"testing"
1413
"time"
1514

@@ -59,6 +58,10 @@ func TestSSLMode(t *testing.T) {
5958
connect string
6059
wantErr string
6160
}{
61+
// Default (prefer) should work both with and without ssl.
62+
{"user=pqgossl", ""},
63+
{f.DSN(), ""},
64+
6265
// sslmode=require: require SSL, but don't verify certificate.
6366
{"sslmode=require user=pqgossl", ""},
6467
{"sslmode=require " + f.DSN(), "pq: SSL is not enabled on the server"},
@@ -404,12 +407,7 @@ func TestSSLDefaults(t *testing.T) {
404407
func TestSSLRootCA(t *testing.T) {
405408
startSSLTest(t, "pqgossl")
406409

407-
// TODO: can remove this once https://github.com/lib/pq/pull/1271 is merged.
408-
os.Unsetenv("PGSSLMODE")
409-
t.Cleanup(func() {
410-
os.Setenv("PGSSLMODE", "disable")
411-
testSystemRoots = nil
412-
})
410+
t.Cleanup(func() { testSystemRoots = nil })
413411
testSystemRoots = x509.NewCertPool()
414412
if !testSystemRoots.AppendCertsFromPEM(pqtest.Read(t, "testdata/init/root.crt")) {
415413
t.Fatal()

0 commit comments

Comments
 (0)