From 84cc10595ce8e4735c5943e2fe54aac41a911962 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Tue, 22 Apr 2014 21:00:13 -0500 Subject: [PATCH] Connect as OS user if no user specified --- connection.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/connection.go b/connection.go index c0faa067..36699844 100644 --- a/connection.go +++ b/connection.go @@ -14,6 +14,7 @@ import ( "fmt" "io" "net" + "os/user" "time" ) @@ -23,7 +24,7 @@ type ConnectionParameters struct { Host string // url (e.g. localhost) Port uint16 // default: 5432 Database string - User string + User string // default: OS user name Password string MsgBufSize int // Size of work buffer used for transcoding messages. For optimal performance, it should be large enough to store a single row from any result set. Default: 1024 TLSConfig *tls.Config // config for TLS connection -- nil disables TLS @@ -90,8 +91,8 @@ func (e ProtocolError) Error() string { } // Connect establishes a connection with a PostgreSQL server using parameters. One -// of parameters.Socket or parameters.Host must be specified. parameters.User must -// also the included. Other parameters fields are optional. +// of parameters.Socket or parameters.Host must be specified. parameters.User +// will default to the OS user name. Other parameters fields are optional. func Connect(parameters ConnectionParameters) (c *Connection, err error) { c = new(Connection) @@ -102,6 +103,15 @@ func Connect(parameters ConnectionParameters) (c *Connection, err error) { c.logger = nullLogger("null") } + if c.parameters.User == "" { + user, err := user.Current() + if err != nil { + return nil, err + } + c.logger.Debug("Using default User " + user.Username) + c.parameters.User = user.Username + } + if c.parameters.Port == 0 { c.logger.Debug("Using default Port") c.parameters.Port = 5432