From a200899d935f82faf80ea557266e5a5d7d714574 Mon Sep 17 00:00:00 2001 From: Sean Chittenden Date: Sun, 6 Nov 2016 01:27:51 -0700 Subject: [PATCH] Allow the PostgreSQL provider to talk to a non-default database. --- builtin/providers/postgresql/config.go | 9 ++++++--- builtin/providers/postgresql/provider.go | 7 +++++++ .../source/docs/providers/postgresql/index.html.markdown | 4 +++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/builtin/providers/postgresql/config.go b/builtin/providers/postgresql/config.go index 1021e7f28c..81ad075835 100644 --- a/builtin/providers/postgresql/config.go +++ b/builtin/providers/postgresql/config.go @@ -12,6 +12,7 @@ import ( type Config struct { Host string Port int + Database string Username string Password string SslMode string @@ -27,12 +28,14 @@ type Client struct { // NewClient returns new client config func (c *Config) NewClient() (*Client, error) { - const dsnFmt = "host=%s port=%d user=%s password=%s sslmode=%s fallback_application_name=%s connect_timeout=%d" + // NOTE: dbname must come before user otherwise dbname will be set to + // user. + const dsnFmt = "host=%s port=%d dbname=%s user=%s password=%s sslmode=%s fallback_application_name=%s connect_timeout=%d" - logDSN := fmt.Sprintf(dsnFmt, c.Host, c.Port, c.Username, "", c.SSLMode, c.ApplicationName) + logDSN := fmt.Sprintf(dsnFmt, c.Host, c.Port, c.Database, c.Username, "", c.SSLMode, c.ApplicationName) log.Printf("[INFO] PostgreSQL DSN: `%s`", logDSN) - connStr := fmt.Sprintf(dsnFmt, c.Host, c.Port, c.Username, c.Password, c.SSLMode, c.ApplicationName, c.Timeout) + connStr := fmt.Sprintf(dsnFmt, c.Host, c.Port, c.Database, c.Username, c.Password, c.SSLMode, c.ApplicationName, c.Timeout) client := Client{ connStr: connStr, username: c.Username, diff --git a/builtin/providers/postgresql/provider.go b/builtin/providers/postgresql/provider.go index 7046793c1b..62a12bcea7 100644 --- a/builtin/providers/postgresql/provider.go +++ b/builtin/providers/postgresql/provider.go @@ -26,6 +26,12 @@ func Provider() terraform.ResourceProvider { DefaultFunc: schema.EnvDefaultFunc("PGPORT", 5432), Description: "The PostgreSQL port number to connect to at the server host, or socket file name extension for Unix-domain connections", }, + "database": { + Type: schema.TypeString, + Optional: true, + Description: "The name of the database to connect to in order to conenct to (defaults to `postgres`).", + DefaultFunc: schema.EnvDefaultFunc("PGDATABASE", "postgres"), + }, "username": { Type: schema.TypeString, Optional: true, @@ -69,6 +75,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) { config := Config{ Host: d.Get("host").(string), Port: d.Get("port").(int), + Database: d.Get("database").(string), Username: d.Get("username").(string), Password: d.Get("password").(string), Timeout: d.Get("connect_timeout").(int), diff --git a/website/source/docs/providers/postgresql/index.html.markdown b/website/source/docs/providers/postgresql/index.html.markdown index f4643f7ccf..f5757efc41 100644 --- a/website/source/docs/providers/postgresql/index.html.markdown +++ b/website/source/docs/providers/postgresql/index.html.markdown @@ -18,6 +18,7 @@ Use the navigation to the left to read about the available resources. provider "postgresql" { host = "postgres_server_ip" port = 5432 + database = "postgres" username = "postgres_user" password = "postgres_password" ssl_mode = "require" @@ -61,9 +62,10 @@ The following arguments are supported: * `host` - (Required) The address for the postgresql server connection. * `port` - (Optional) The port for the postgresql server connection. The default is `5432`. +* `database` - (Optional) Database to connect to. The default is `postgres`. * `username` - (Required) Username for the server connection. * `password` - (Optional) Password for the server connection. -* `ssl_mode` - (Optional) Set the priority for an SSL connection to the server. +* `sslmode` - (Optional) Set the priority for an SSL connection to the server. * `connect_timeout` - (Optional) Maximum wait for connection, in seconds. Zero means wait indefinitely, the default is `15`. The default is `prefer`; the full set of options and their implications can be seen [in the libpq SSL guide](http://www.postgresql.org/docs/9.4/static/libpq-ssl.html#LIBPQ-SSL-PROTECTION).