github.com/erriapo/terraform@v0.6.12-0.20160203182612-0340ea72354f/builtin/providers/postgresql/config.go (about)

     1  package postgresql
     2  
     3  import (
     4  	"database/sql"
     5  	"fmt"
     6  
     7  	_ "github.com/lib/pq" //PostgreSQL db
     8  )
     9  
    10  // Config - provider config
    11  type Config struct {
    12  	Host     string
    13  	Port     int
    14  	Username string
    15  	Password string
    16  }
    17  
    18  // Client struct holding connection string
    19  type Client struct {
    20  	username string
    21  	connStr  string
    22  }
    23  
    24  //NewClient returns new client config
    25  func (c *Config) NewClient() (*Client, error) {
    26  	connStr := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=postgres", c.Host, c.Port, c.Username, c.Password)
    27  
    28  	client := Client{
    29  		connStr:  connStr,
    30  		username: c.Username,
    31  	}
    32  
    33  	return &client, nil
    34  }
    35  
    36  //Connect will manually connect/diconnect to prevent a large number or db connections being made
    37  func (c *Client) Connect() (*sql.DB, error) {
    38  	db, err := sql.Open("postgres", c.connStr)
    39  	if err != nil {
    40  		return nil, fmt.Errorf("Error connecting to postgresql server: %s", err)
    41  	}
    42  
    43  	return db, nil
    44  }