Skip to content

Commit dc8ff5d

Browse files
committed
Implement connection service file
Allow reading connection from a service file if "service=name" is used. Based on #868 Co-authored-by: Feike Steenbergen <[email protected]> Fixes #538 Closes #868
1 parent b36c912 commit dc8ff5d

8 files changed

Lines changed: 241 additions & 21 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ unreleased
2323

2424
- Support `ssl_min_protocol_version` and `ssl_max_protocol_version` ([#1277]).
2525

26+
- Support connection service file to load connection details ([#1285]).
27+
2628
- Support `sslrootcert=system` and use `~/.postgresql/root.crt` as the default
2729
value of sslrootcert ([#1280], [#1281]).
2830

@@ -53,6 +55,7 @@ unreleased
5355
[#1281]: https://github.com/lib/pq/pull/1281
5456
[#1282]: https://github.com/lib/pq/pull/1282
5557
[#1283]: https://github.com/lib/pq/pull/1283
58+
[#1285]: https://github.com/lib/pq/pull/1285
5659

5760
v1.11.2 (2026-02-10)
5861
--------------------

‎connector.go‎

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ import (
1212
"os"
1313
"path/filepath"
1414
"reflect"
15+
"runtime"
1516
"slices"
1617
"sort"
1718
"strconv"
1819
"strings"
1920
"time"
2021
"unicode"
2122

23+
"github.com/lib/pq/internal/pgservice"
2224
"github.com/lib/pq/internal/pqutil"
2325
"github.com/lib/pq/internal/proto"
2426
)
@@ -412,6 +414,23 @@ type Config struct {
412414
// Maximum PostgreSQL protocol version to request from the server. Defaults to "3.0".
413415
MaxProtocolVersion ProtocolVersion `postgres:"max_protocol_version" env:"PGMAXPROTOCOLVERSION"`
414416

417+
// Load connection parameters from the service file at ~/.pg_service.conf
418+
// (which can be configured with PGSERVICEFILE).
419+
//
420+
// The service file is a INI-like file to configure connection parameters:
421+
//
422+
// [servicename]
423+
// # Comment
424+
// dbname=foo
425+
//
426+
// Unlike libpq, this does not look at the system-wide service file, as the
427+
// location of this is a compile-time value that is not easy for pq to
428+
// retrieve.
429+
Service string `postgres:"service" env:"PGSERVICE"`
430+
431+
// Path to connection service file. Defaults to ~/.pg_service.conf.
432+
ServiceFile string `postgres:"-" env:"PGSERVICEFILE"`
433+
415434
// Runtime parameters: any unrecognized parameter in the DSN will be added
416435
// to this and sent to PostgreSQL during startup.
417436
Runtime map[string]string `postgres:"-" env:"-"`
@@ -440,7 +459,10 @@ type ConfigMultihost struct {
440459
Port uint16
441460
}
442461

443-
// NewConfig creates a new [Config] from the defaults, environment, and DSN.
462+
// NewConfig creates a new [Config] from the defaults, environment, service
463+
// file, and DSN, in that order. That is: a service overrides any value from the
464+
// environment, which in turn gets overridden by the same parameter in the
465+
// connection string.
444466
//
445467
// Most connection parameters supported by PostgreSQL are supported; see the
446468
// [Config] struct for supported parameters. pq also lets you specify any
@@ -534,6 +556,9 @@ func newConfig(dsn string, env []string) (Config, error) {
534556
if err := cfg.fromDSN(dsn); err != nil {
535557
return Config{}, err
536558
}
559+
if err := cfg.fromService(); err != nil {
560+
return Config{}, err
561+
}
537562

538563
// Need to have exactly the same number of host and hostaddr, or only specify one.
539564
if cfg.isset("host") && cfg.Host != "" && cfg.Hostaddr != (netip.Addr{}) && len(cfg.multiHost) != len(cfg.multiHostaddr) {
@@ -650,7 +675,7 @@ func (cfg *Config) fromEnv(env []string) error {
650675
switch k {
651676
case "PGREQUIRESSL", "PGSSLCOMPRESSION", // Deprecated.
652677
"PGREALM", "PGGSSENCMODE", "PGGSSDELEGATION", "PGGSSLIB", // krb stuff
653-
"PGREQUIREAUTH", "PGCHANNELBINDING", "PGSERVICE", "PGSERVICEFILE",
678+
"PGREQUIREAUTH", "PGCHANNELBINDING",
654679
"PGSSLCERTMODE", "PGSSLCRL", "PGSSLCRLDIR", "PGREQUIREPEER":
655680
return fmt.Errorf("pq: environment variable $%s is not supported", k)
656681
case "PGKRBSRVNAME":
@@ -660,7 +685,7 @@ func (cfg *Config) fromEnv(env []string) error {
660685
}
661686
e[k] = v
662687
}
663-
return cfg.setFromTag(e, "env")
688+
return cfg.setFromTag(e, "env", false)
664689
}
665690

666691
// parseOpts parses the options from name and adds them to the values.
@@ -766,10 +791,31 @@ func (cfg *Config) fromDSN(dsn string) error {
766791
opt[string(keyRunes)] = string(valRunes)
767792
}
768793

769-
return cfg.setFromTag(opt, "postgres")
794+
return cfg.setFromTag(opt, "postgres", false)
795+
}
796+
797+
func (cfg *Config) fromService() error {
798+
if cfg.Service == "" {
799+
return nil
800+
}
801+
802+
if !cfg.isset("PGSERVICEFILE") {
803+
if home := pqutil.Home(); home != "" {
804+
if runtime.GOOS != "windows" {
805+
home = filepath.Dir(home) // Unlike other files this uses ~/ and not ~/.postgresql
806+
}
807+
cfg.ServiceFile = filepath.Join(home, ".pg_service.conf")
808+
}
809+
}
810+
811+
opts, err := pgservice.FindService(cfg.ServiceFile, cfg.Service)
812+
if err != nil {
813+
return fmt.Errorf("pq: %w", err)
814+
}
815+
return cfg.setFromTag(opts, "postgres", true)
770816
}
771817

772-
func (cfg *Config) setFromTag(o map[string]string, tag string) error {
818+
func (cfg *Config) setFromTag(o map[string]string, tag string, service bool) error {
773819
f := "pq: wrong value for %q: "
774820
if tag == "env" {
775821
f = "pq: wrong value for $%s: "
@@ -803,7 +849,11 @@ func (cfg *Config) setFromTag(o map[string]string, tag string) error {
803849
v, ok := o[k]
804850
delete(o, k)
805851
if ok {
806-
if t, ok := rt.Tag.Lookup("postgres"); ok && t != "" && t != "-" {
852+
t, ok := rt.Tag.Lookup("postgres")
853+
if !ok || t == "" || t == "-" { // For PGSERVICEFILE, which can only be from env
854+
t, ok = rt.Tag.Lookup("env")
855+
}
856+
if ok && t != "" && t != "-" {
807857
cfg.set = append(cfg.set, t)
808858
}
809859
switch rt.Type.Kind() {
@@ -903,8 +953,18 @@ func (cfg *Config) setFromTag(o map[string]string, tag string) error {
903953
}
904954
}
905955

956+
if service && len(o) > 0 {
957+
// TODO(go1.23): use maps.Keys once we require Go 1.23.
958+
var key string
959+
for k := range o {
960+
key = k
961+
break
962+
}
963+
return fmt.Errorf("pq: unknown setting %q in service file for service %q", key, cfg.Service)
964+
}
965+
906966
// Set run-time; we delete map keys as they're set in the struct.
907-
if tag == "postgres" {
967+
if !service && tag == "postgres" {
908968
// Make sure database= sets dbname=, as that previously worked (kind of
909969
// by accident).
910970
// TODO(v2): remove

‎connector_test.go‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
"fmt"
99
"net"
1010
"os"
11+
"path/filepath"
1112
"reflect"
13+
"runtime"
1214
"slices"
1315
"strings"
1416
"testing"
@@ -849,3 +851,81 @@ func TestProtocolVersion(t *testing.T) {
849851
})
850852
}
851853
}
854+
855+
func TestService(t *testing.T) {
856+
h := pqtest.Home(t)
857+
if runtime.GOOS != "windows" {
858+
h = filepath.Dir(h)
859+
}
860+
861+
// Test without ~/.pg_service.conf existing
862+
t.Run("default file doesn't exist", func(t *testing.T) {
863+
cfg, err := NewConfig("service=svc1")
864+
if wantErr := fmt.Sprintf(`pq: service file "%s/.pg_service.conf" not found`, h); !pqtest.ErrorContains(err, wantErr) {
865+
t.Fatalf("wrong error\nhave: %v\nwant: %v", err, wantErr)
866+
}
867+
if have := cfg.string(); have != "" {
868+
t.Errorf("\nhave: %q\nwant: %q", have, "")
869+
}
870+
})
871+
872+
tests := []struct {
873+
connect string
874+
env map[string]string
875+
want string
876+
wantErr string
877+
}{
878+
{"service=doesntexist", nil, ``, `definition of service "doesntexist" not found`},
879+
{"service=svc1", nil, `connect_timeout=20 dbname=xyz host=firsthost port=1234 service=svc1 sslmode=disable user=pqgo`, ``},
880+
{"service=svc2", nil, `connect_timeout=20 dbname='with space' host=localhost service=svc2 sslmode=disable user=''`, ``},
881+
{"service=svc3", nil, ``, `pq: unknown setting "unknown" in service file for service "svc3"`},
882+
{"service=svc4", nil, `connect_timeout=20 dbname=pqgo host=localhost service=svc4 sslmode=disable user=pqgo`, ``},
883+
{"service=svc5", nil, `connect_timeout=20 dbname=pqgo host=localhost service=svc5 sslmode=disable user=pqgo`, ``},
884+
885+
{"service=svc5", map[string]string{"PGSERVICEFILE": "none"}, ``, `service file "none" not found`},
886+
{"service=svc1", map[string]string{"PGSERVICEFILE": filepath.Join(h, "other")}, ``, `definition of service "svc1" not found`},
887+
{"service=other", map[string]string{"PGSERVICEFILE": filepath.Join(h, "other")},
888+
`connect_timeout=20 dbname=other host=localhost service=other sslmode=disable user=pqgo`, ``},
889+
}
890+
891+
pqtest.Write(t, []byte("[other]\ndbname=other"), h, "other")
892+
pqtest.Write(t, []byte(`
893+
[svc1]
894+
# Connect to this instead.
895+
host=firsthost
896+
dbname = xyz
897+
port=1234
898+
899+
[svc2]
900+
user=
901+
dbname=with space
902+
903+
[svc3]
904+
unknown=wut
905+
906+
# Empty
907+
[svc4]
908+
[svc5]
909+
`), h, ".pg_service.conf")
910+
911+
// pgbouncer sets PGPORT; not really used to just unset.
912+
// TODO: might want to add pqtest.Clearenv() or the like.
913+
t.Setenv("PGPORT", "")
914+
os.Unsetenv("PGPORT")
915+
916+
for _, tt := range tests {
917+
t.Run("", func(t *testing.T) {
918+
for k, v := range tt.env {
919+
t.Setenv(k, v)
920+
}
921+
922+
cfg, err := NewConfig(tt.connect)
923+
if !pqtest.ErrorContains(err, tt.wantErr) {
924+
t.Fatalf("wrong error\nhave: %v\nwant: %v", err, tt.wantErr)
925+
}
926+
if have := cfg.string(); have != tt.want {
927+
t.Errorf("\nhave: %q\nwant: %q", have, tt.want)
928+
}
929+
})
930+
}
931+
}

‎internal/pgservice/pgservice.go‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package pgservice
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"strings"
8+
9+
"github.com/lib/pq/internal/pqutil"
10+
)
11+
12+
func FindService(path string, service string) (map[string]string, error) {
13+
fp, err := os.Open(path)
14+
if err != nil {
15+
if pqutil.ErrNotExists(err) {
16+
// libpq just returns "definition of service not found" if the
17+
// default file doesn't exist, but IMO that's confusing.
18+
return nil, fmt.Errorf("service file %q not found", path)
19+
}
20+
return nil, err
21+
}
22+
defer fp.Close()
23+
24+
var (
25+
scan = bufio.NewScanner(fp)
26+
i int
27+
)
28+
for scan.Scan() {
29+
i++
30+
line := strings.TrimSpace(scan.Text())
31+
if line == "" || line[0] == '#' {
32+
continue
33+
}
34+
35+
// [service] header that we want.
36+
if line[0] == '[' && line[len(line)-1] == ']' && strings.TrimSpace(line[1:len(line)-1]) == service {
37+
opts := make(map[string]string)
38+
for scan.Scan() {
39+
i++
40+
line := strings.TrimSpace(scan.Text())
41+
if line == "" || line[0] == '#' {
42+
continue
43+
}
44+
// Next header: our work here is done.
45+
if line[0] == '[' && line[len(line)-1] == ']' {
46+
return opts, nil
47+
}
48+
49+
k, v, ok := strings.Cut(line, "=")
50+
if !ok {
51+
return nil, fmt.Errorf("line %d: missing '=' in %q", i, line)
52+
}
53+
k, v = strings.TrimSpace(k), strings.TrimSpace(v)
54+
if k == "" {
55+
return nil, fmt.Errorf("line %d: no value before '=' in %q", i, line)
56+
}
57+
opts[k] = v
58+
}
59+
if scan.Err() != nil {
60+
return nil, scan.Err()
61+
}
62+
return opts, nil
63+
}
64+
}
65+
if scan.Err() != nil {
66+
return nil, scan.Err()
67+
}
68+
69+
return nil, fmt.Errorf("definition of service %q not found", service)
70+
}

‎internal/pqtest/pqtest.go‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"strings"
99
"sync"
1010
"testing"
11+
12+
"github.com/lib/pq/internal/pqutil"
1113
)
1214

1315
func Pgbouncer() bool { return os.Getenv("PGPORT") == "6432" }
@@ -53,7 +55,7 @@ func InvalidCertificate(err error) bool {
5355

5456
// Ptr gets a pointer to any value.
5557
//
56-
// TODO: replace with new(..) once pq requires Go 1.26.
58+
// TODO(go1.26): replace with new(..) once pq requires Go 1.26.
5759
func Ptr[T any](t T) *T {
5860
return &t
5961
}
@@ -82,6 +84,17 @@ func DSN(conninfo string) string {
8284
return conninfo
8385
}
8486

87+
// Home sets the HOME directory to a temporary directly and makes sure the
88+
// .postgresql directory exists.
89+
func Home(t *testing.T) string {
90+
t.Helper()
91+
t.Setenv("HOME", t.TempDir())
92+
if err := os.MkdirAll(pqutil.Home(), 0o777); err != nil {
93+
t.Fatal(err)
94+
}
95+
return pqutil.Home()
96+
}
97+
8598
// DB connects to the test database and returns the Ping error. The connection
8699
// is closed in t.Cleanup().
87100
func DB(t testing.TB, conninfo ...string) (*sql.DB, error) {

‎internal/pqutil/perm.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ func SSLKeyPermissions(sslkey string) error {
2929
return err
3030
}
3131

32-
return checkPermissions(fi)
32+
return CheckPermissions(fi)
3333
}
3434

35-
func checkPermissions(fi os.FileInfo) error {
35+
func CheckPermissions(fi os.FileInfo) error {
3636
// The maximum permissions that a private key file owned by a regular user
3737
// is allowed to have. This translates to u=rw. Regardless of if we're
3838
// running as root or not, 0600 is acceptable, so we return if no bits

0 commit comments

Comments
 (0)