defaults_windows.go (1930B)
1 package pgconn 2 3 import ( 4 "os" 5 "os/user" 6 "path/filepath" 7 "strings" 8 ) 9 10 func defaultSettings() map[string]string { 11 settings := make(map[string]string) 12 13 settings["host"] = defaultHost() 14 settings["port"] = "5432" 15 16 // Default to the OS user name. Purposely ignoring err getting user name from 17 // OS. The client application will simply have to specify the user in that 18 // case (which they typically will be doing anyway). 19 user, err := user.Current() 20 appData := os.Getenv("APPDATA") 21 if err == nil { 22 // Windows gives us the username here as `DOMAIN\user` or `LOCALPCNAME\user`, 23 // but the libpq default is just the `user` portion, so we strip off the first part. 24 username := user.Username 25 if strings.Contains(username, "\\") { 26 username = username[strings.LastIndex(username, "\\")+1:] 27 } 28 29 settings["user"] = username 30 settings["passfile"] = filepath.Join(appData, "postgresql", "pgpass.conf") 31 settings["servicefile"] = filepath.Join(user.HomeDir, ".pg_service.conf") 32 sslcert := filepath.Join(appData, "postgresql", "postgresql.crt") 33 sslkey := filepath.Join(appData, "postgresql", "postgresql.key") 34 if _, err := os.Stat(sslcert); err == nil { 35 if _, err := os.Stat(sslkey); err == nil { 36 // Both the cert and key must be present to use them, or do not use either 37 settings["sslcert"] = sslcert 38 settings["sslkey"] = sslkey 39 } 40 } 41 sslrootcert := filepath.Join(appData, "postgresql", "root.crt") 42 if _, err := os.Stat(sslrootcert); err == nil { 43 settings["sslrootcert"] = sslrootcert 44 } 45 } 46 47 settings["target_session_attrs"] = "any" 48 49 return settings 50 } 51 52 // defaultHost attempts to mimic libpq's default host. libpq uses the default unix socket location on *nix and localhost 53 // on Windows. The default socket location is compiled into libpq. Since pgx does not have access to that default it 54 // checks the existence of common locations. 55 func defaultHost() string { 56 return "localhost" 57 }