Source File
defaults.go
Belonging Package
github.com/jackc/pgx/v5/pgconn
//go:build !windowspackage pgconnimport ()// currentOSUser resolves the current OS user account. It is a seam so tests// can simulate os/user.Current failing -- including the unrecoverable// crashes reported in some restricted/broken-NSS container environments --// without needing such an environment.var currentOSUser = user.Current// userHomeDir resolves the current user's home directory. It is a seam so// tests can simulate os.UserHomeDir failing without needing to unset $HOME// for the whole process.//// os.UserHomeDir only reads the $HOME environment variable on this platform;// it never calls into cgo or NSS, so unlike os/user.Current it cannot crash// in restricted/broken container environments. That also means it can// resolve a different directory than the OS account's passwd-file home// directory if $HOME has been overridden -- this is an accepted tradeoff so// that home-directory-derived defaults (pgpass, pg_service.conf, client SSL// certificate/key/root files) keep resolving even when the OS user account// lookup itself is unavailable or unsafe to call.var userHomeDir = os.UserHomeDirfunc () map[string]string {:= make(map[string]string)["host"] = defaultHost()["port"] = defaultPort["target_session_attrs"] = "any"// The home-directory-derived defaults (~/.pgpass, ~/.pg_service.conf,// and the client SSL certificate/key/root files under ~/.postgresql)// only need the user's home directory, not the full OS user account, so// they are resolved unconditionally here via os.UserHomeDir. This keeps// them available even when looking up the OS user account (needed only// for the default PostgreSQL user name, see osUserSettings) is slow,// fails, or -- in some restricted container environments with a broken// NSS/CGO setup -- would crash the process.if , := userHomeDir(); == nil {["passfile"] = filepath.Join(, ".pgpass")["servicefile"] = filepath.Join(, ".pg_service.conf"):= filepath.Join(, ".postgresql", "postgresql.crt"):= filepath.Join(, ".postgresql", "postgresql.key")if , := os.Stat(); == nil {if , := os.Stat(); == nil {// Both the cert and key must be present to use them, or do not use either["sslcert"] =["sslkey"] =}}:= filepath.Join(, ".postgresql", "root.crt")if , := os.Stat(); == nil {["sslrootcert"] =}}return}// osUserSettings returns the default PostgreSQL user name derived from the// current OS user account.//// Resolving this requires looking up the OS user account, which can be slow// or, in some restricted container environments (e.g. distroless images// with a broken NSS/CGO setup), crash the process. Callers must only call// osUserSettings when the default user name is not already supplied by the// connection string, environment, or service file.func () map[string]string {:= make(map[string]string)// Default to the OS user name. Purposely ignoring err getting user name from// OS. The client application will simply have to specify the user in that// case (which they typically will be doing anyway)., := currentOSUser()if == nil {["user"] = .Username}return}// defaultHost attempts to mimic libpq's default host. libpq uses the default unix socket location on *nix and localhost// on Windows. The default socket location is compiled into libpq. Since pgx does not have access to that default it// checks the existence of common locations.func () string {:= []string{"/var/run/postgresql", // Debian"/private/tmp", // OSX - homebrew"/tmp", // standard PostgreSQL}for , := range {if , := os.Stat(); == nil {return}}return "localhost"}
The pages are generated with Golds v0.8.7. (GOOS=linux GOARCH=amd64)