postgres

The postgres connection can be used to access a PostgreSQL database.

connection "postgres" "my_connection" {
host = "localhost"
port = 5432
db = "mydb"
username = "myuser"
password = "mypassword123"
}

Arguments

NameTypeRequired?Description
dbStringOptionalDatabase name. Defaults to $PGDATABASE, or postgres if unset.
hostStringOptionalDatabase hostname. Defaults to $PGHOST or localhost if not set.
passwordStringOptionalDatabase password. Defaults to $PGPASSWORD
portNumberOptionalDatabase port. Defaults to $PGPORT or 5432 if not set.
search_pathStringOptionalDatabase search path.
search_path_prefixStringOptionalDatabase search path prefix.
ssl_modeStringOptionalPostgreSQL SSL Mode, one of disable, allow, prefer, require, verify-ca, verify-full. The defaults to $PGSSLNEGOTIATION.
usernameStringOptionalDatabase username. Defaults to $PGUSER or postgres if not set.

All arguments are optional, and a postgres connection with no arguments will behave the same as the default connection.

Attributes (Read-Only)

AttributeTypeDescription
connection_stringStringThe connection string built from the arguments to this connection, in the format postgresql://[username[:password]@][host][:port][/db]
envMapA map of the resolved libpq environment variables (PGHOST, PGDATABASE, PGUSER, PGPASSWORD, PGPORT, PGSSLNEGOTIATION)

Default Connection

The postgres connection type includes an implicit, default connection (connection.postgres.default) that will be configured using the libpq environment variables.

connection "postgres" "default" {
db = env("PGDATABASE")
host = env("PGHOST")
password = env("PGPASSWORD")
port = env("PGPORT")
ssl_mode = env("PGSSLNEGOTIATION")
username = env("PGUSER")
}