diff --git a/builtin/providers/postgresql/config.go b/builtin/providers/postgresql/config.go index 2d14baba8..f7e82ad82 100644 --- a/builtin/providers/postgresql/config.go +++ b/builtin/providers/postgresql/config.go @@ -10,14 +10,15 @@ import ( // Config - provider config type Config struct { - Host string - Port int - Database string - Username string - Password string - SSLMode string - Timeout int - ApplicationName string + Host string + Port int + Database string + Username string + Password string + SSLMode string + ApplicationName string + Timeout int + ConnectTimeoutSec int } // Client struct holding connection string @@ -32,10 +33,10 @@ func (c *Config) NewClient() (*Client, error) { // 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.Database, c.Username, "", c.SSLMode, c.ApplicationName) + logDSN := fmt.Sprintf(dsnFmt, c.Host, c.Port, c.Database, c.Username, "", c.SSLMode, c.ApplicationName, c.ConnectTimeoutSec) log.Printf("[INFO] PostgreSQL DSN: `%s`", logDSN) - connStr := fmt.Sprintf(dsnFmt, c.Host, c.Port, c.Database, 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.ConnectTimeoutSec) client := Client{ connStr: connStr, username: c.Username, diff --git a/builtin/providers/postgresql/provider.go b/builtin/providers/postgresql/provider.go index 9c6dc949c..65aa0ba7c 100644 --- a/builtin/providers/postgresql/provider.go +++ b/builtin/providers/postgresql/provider.go @@ -64,6 +64,13 @@ func Provider() terraform.ResourceProvider { Optional: true, Deprecated: "Rename PostgreSQL provider `ssl_mode` attribute to `sslmode`", }, + "connect_timeout": { + Type: schema.TypeInt, + Optional: true, + DefaultFunc: schema.EnvDefaultFunc("PGCONNECT_TIMEOUT", 180), + Description: "Maximum wait for connection, in seconds. Zero or not specified means wait indefinitely.", + ValidateFunc: validateConnTimeout, + }, }, ResourcesMap: map[string]*schema.Resource{ @@ -76,6 +83,14 @@ func Provider() terraform.ResourceProvider { } } +func validateConnTimeout(v interface{}, key string) (warnings []string, errors []error) { + value := v.(int) + if value < 0 { + errors = append(errors, fmt.Errorf("%d can not be less than 0", key)) + } + return +} + func providerConfigure(d *schema.ResourceData) (interface{}, error) { var sslMode string var ok bool @@ -83,14 +98,14 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) { sslMode = d.Get("ssl_mode").(string) } 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), - SSLMode: sslMode, - Timeout: d.Get("connect_timeout").(int), - ApplicationName: tfAppName(), + 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), + SSLMode: sslMode, + ApplicationName: tfAppName(), + ConnectTimeoutSec: d.Get("connect_timeout").(int), } client, err := config.NewClient() diff --git a/website/source/docs/providers/postgresql/index.html.markdown b/website/source/docs/providers/postgresql/index.html.markdown index 3f30d6574..3cd9aee8f 100644 --- a/website/source/docs/providers/postgresql/index.html.markdown +++ b/website/source/docs/providers/postgresql/index.html.markdown @@ -74,6 +74,5 @@ The following arguments are supported: * verify-full - Always SSL (verify that the certification presented by the server was signed by a trusted CA and the server host name matches the one in the certificate) Additional information on the options and their implications can be seen [in the `libpq(3)` SSL guide](http://www.postgresql.org/docs/current/static/libpq-ssl.html#LIBPQ-SSL-PROTECTION). -* `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). +* `connect_timeout` - (Optional) Maximum wait for connection, in seconds. The + default is `180s`. Zero or not specified means wait indefinitely.