1package pgconn
2
3import (
4	"os"
5	"os/user"
6	"path/filepath"
7	"strings"
8)
9
10func 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	}
33
34	settings["target_session_attrs"] = "any"
35
36	settings["min_read_buffer_size"] = "8192"
37
38	return settings
39}
40
41// defaultHost attempts to mimic libpq's default host. libpq uses the default unix socket location on *nix and localhost
42// on Windows. The default socket location is compiled into libpq. Since pgx does not have access to that default it
43// checks the existence of common locations.
44func defaultHost() string {
45	return "localhost"
46}
47