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	}
26
27	settings["target_session_attrs"] = "any"
28
29	settings["min_read_buffer_size"] = "8192"
30
31	return settings
32}
33
34// defaultHost attempts to mimic libpq's default host. libpq uses the default unix socket location on *nix and localhost
35// on Windows. The default socket location is compiled into libpq. Since pgx does not have access to that default it
36// checks the existence of common locations.
37func defaultHost() string {
38	candidatePaths := []string{
39		"/var/run/postgresql", // Debian
40		"/private/tmp",        // OSX - homebrew
41		"/tmp",                // standard PostgreSQL
42	}
43
44	for _, path := range candidatePaths {
45		if _, err := os.Stat(path); err == nil {
46			return path
47		}
48	}
49
50	return "localhost"
51}
52