github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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  	SslMode  string
    17  }
    18  
    19  // Client struct holding connection string
    20  type Client struct {
    21  	username string
    22  	connStr  string
    23  }
    24  
    25  // NewClient returns new client config
    26  func (c *Config) NewClient() (*Client, error) {
    27  	connStr := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=postgres sslmode=%s", c.Host, c.Port, c.Username, c.Password, c.SslMode)
    28  
    29  	client := Client{
    30  		connStr:  connStr,
    31  		username: c.Username,
    32  	}
    33  
    34  	return &client, nil
    35  }
    36  
    37  // Connect will manually connect/disconnect to prevent a large
    38  // number or db connections being made
    39  func (c *Client) Connect() (*sql.DB, error) {
    40  	db, err := sql.Open("postgres", c.connStr)
    41  	if err != nil {
    42  		return nil, fmt.Errorf("Error connecting to PostgreSQL server: %s", err)
    43  	}
    44  
    45  	return db, nil
    46  }