1// +build !windows 2 3package pgconn 4 5import ( 6 "os" 7 "os/user" 8 "path/filepath" 9) 10 11func defaultSettings() map[string]string { 12 settings := make(map[string]string) 13 14 settings["host"] = defaultHost() 15 settings["port"] = "5432" 16 17 // Default to the OS user name. Purposely ignoring err getting user name from 18 // OS. The client application will simply have to specify the user in that 19 // case (which they typically will be doing anyway). 20 user, err := user.Current() 21 if err == nil { 22 settings["user"] = user.Username 23 settings["passfile"] = filepath.Join(user.HomeDir, ".pgpass") 24 settings["servicefile"] = filepath.Join(user.HomeDir, ".pg_service.conf") 25 sslcert := filepath.Join(user.HomeDir, ".postgresql", "postgresql.crt") 26 sslkey := filepath.Join(user.HomeDir, ".postgresql", "postgresql.key") 27 if _, err := os.Stat(sslcert); err == nil { 28 if _, err := os.Stat(sslkey); err == nil { 29 // Both the cert and key must be present to use them, or do not use either 30 settings["sslcert"] = sslcert 31 settings["sslkey"] = sslkey 32 } 33 } 34 sslrootcert := filepath.Join(user.HomeDir, ".postgresql", "root.crt") 35 if _, err := os.Stat(sslrootcert); err == nil { 36 settings["sslrootcert"] = sslrootcert 37 } 38 } 39 40 settings["target_session_attrs"] = "any" 41 42 settings["min_read_buffer_size"] = "8192" 43 44 return settings 45} 46 47// defaultHost attempts to mimic libpq's default host. libpq uses the default unix socket location on *nix and localhost 48// on Windows. The default socket location is compiled into libpq. Since pgx does not have access to that default it 49// checks the existence of common locations. 50func defaultHost() string { 51 candidatePaths := []string{ 52 "/var/run/postgresql", // Debian 53 "/private/tmp", // OSX - homebrew 54 "/tmp", // standard PostgreSQL 55 } 56 57 for _, path := range candidatePaths { 58 if _, err := os.Stat(path); err == nil { 59 return path 60 } 61 } 62 63 return "localhost" 64} 65