Skip to content

Commit

Permalink
Ensure the passfile is used on Windows
Browse files Browse the repository at this point in the history
Workaround the limitation of lib/pq that cannot use the passfile
keyword and find the default pgpass.conf file on Windows, by setting
the PGPASSFILE environment variable. It parses the file but without
expanding variables in the path given in PGPASSFILE.
  • Loading branch information
orgrim committed May 7, 2021
1 parent 3e0e420 commit cbd16a2
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"errors"
"fmt"
"github.com/lib/pq"
"os"
"strings"
"time"
)
Expand All @@ -54,7 +55,20 @@ func pgGetVersionNum(db *sql.DB) (int, error) {
}

func dbOpen(conninfo *ConnInfo) (*pg, error) {
connstr := conninfo.String()
// Workaround the limitation of lib/pq that cannot use the passfile
// keyword and find the default pgpass.conf file on Windows, by setting
// the PGPASSFILE environment variable. It parses the file but without
// expanding variables in the path given in PGPASSFILE.
if passfile, ok := conninfo.Infos["passfile"]; ok {
if err := os.Setenv("PGPASSFILE", passfile); err != nil {
l.Warnln("could not set PGPASSFILE environment variable:", err)
}
}

// lib/pq does not like the passfile keyword either.
c := conninfo.Del("passfile")

connstr := c.String()
l.Verbosef("connecting to PostgreSQL with: \"%s\"", connstr)
db, err := sql.Open("postgres", connstr)
if err != nil {
Expand Down

0 comments on commit cbd16a2

Please sign in to comment.