github.com/shvar/terraform@v0.6.9-0.20151215234924-3365cd2231df/builtin/providers/postgresql/config.go (about)

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