github.com/ojiry/terraform@v0.8.2-0.20161218223921-e50cec712c4a/builtin/providers/postgresql/config.go (about) 1 package postgresql 2 3 import ( 4 "database/sql" 5 "fmt" 6 "log" 7 8 _ "github.com/lib/pq" //PostgreSQL db 9 ) 10 11 // Config - provider config 12 type Config struct { 13 Host string 14 Port int 15 Database string 16 Username string 17 Password string 18 SSLMode string 19 ApplicationName string 20 Timeout int 21 ConnectTimeoutSec int 22 } 23 24 // Client struct holding connection string 25 type Client struct { 26 username string 27 connStr string 28 } 29 30 // NewClient returns new client config 31 func (c *Config) NewClient() (*Client, error) { 32 // NOTE: dbname must come before user otherwise dbname will be set to 33 // user. 34 const dsnFmt = "host=%s port=%d dbname=%s user=%s password=%s sslmode=%s fallback_application_name=%s connect_timeout=%d" 35 36 logDSN := fmt.Sprintf(dsnFmt, c.Host, c.Port, c.Database, c.Username, "<redacted>", c.SSLMode, c.ApplicationName, c.ConnectTimeoutSec) 37 log.Printf("[INFO] PostgreSQL DSN: `%s`", logDSN) 38 39 connStr := fmt.Sprintf(dsnFmt, c.Host, c.Port, c.Database, c.Username, c.Password, c.SSLMode, c.ApplicationName, c.ConnectTimeoutSec) 40 client := Client{ 41 connStr: connStr, 42 username: c.Username, 43 } 44 45 return &client, nil 46 } 47 48 // Connect will manually connect/disconnect to prevent a large 49 // number or db connections being made 50 func (c *Client) Connect() (*sql.DB, error) { 51 db, err := sql.Open("postgres", c.connStr) 52 if err != nil { 53 return nil, fmt.Errorf("Error connecting to PostgreSQL server: %s", err) 54 } 55 56 return db, nil 57 }