Skip to content

Manage a Service with Terraform

Declare a Managed PostgreSQL service, its databases, users and connection pools as code with the Exoscale Terraform provider.

This page covers what is specific to PostgreSQL. The provider setup and the resources of the other DBaaS engines are on Use Terraform for DBaaS. The examples were run with provider version 0.77.0.

Configure the Provider

terraform {
  required_providers {
    exoscale = {
      source  = "exoscale/exoscale"
      version = "~> 0.77"
    }
  }
}

variable "exoscale_api_key" {
  type      = string
  sensitive = true
}

variable "exoscale_api_secret" {
  type      = string
  sensitive = true
}

provider "exoscale" {
  key    = var.exoscale_api_key
  secret = var.exoscale_api_secret
}

Pass the values with the environment variables TF_VAR_exoscale_api_key and TF_VAR_exoscale_api_secret rather than writing them in a file. You can also leave the provider block empty, provider "exoscale" {}, and export EXOSCALE_API_KEY and EXOSCALE_API_SECRET. The zone is set on each resource, not on the provider.

Create a PostgreSQL Service

The service is an exoscale_dbaas resource with type = "pg":

variable "admin_username" {
  type = string
}

variable "admin_password" {
  type      = string
  sensitive = true
}

resource "exoscale_dbaas" "my_postgresql" {
  name = "my-postgresql"
  type = "pg"
  plan = "startup-4"
  zone = "ch-gva-2"

  termination_protection = true

  maintenance_dow  = "sunday"
  maintenance_time = "03:00:00"

  pg = {
    version         = "18"
    admin_username  = var.admin_username
    admin_password  = var.admin_password
    ip_filter       = ["203.0.113.0/24"]
    backup_schedule = "04:00"

    pg_settings = jsonencode({
      timezone                   = "Europe/Zurich"
      log_min_duration_statement = 1000
    })
  }
}

terraform apply returns once the service is running, after two to three minutes for a small plan.

What to know before you change this resource later:

  • Changing name or pg.version destroys the service and creates a new, empty one: terraform plan marks the attribute with # forces replacement. Read the plan before every apply. To move to a newer major version, run the major version upgrade through the API, then set pg.version to the new major version before the next apply. The state stores the full version, such as 18.6, and a configuration that still names the old major version plans a replacement.
  • pg.admin_username and pg.admin_password apply only at creation. Without them, the administrator is avnadmin with a generated password.
  • pg.ip_filter takes CIDR blocks: a single address is x.x.x.x/32. See Restrict connections.
  • List the valid plans with exo dbaas type show pg --plans and the versions with exo dbaas type show pg.

Caution

With termination_protection = true, terraform destroy fails on the service, but only after it has already deleted the databases, users and connection pools declared next to it: the service survives, the database app and its data do not. Never run destroy against a protected production configuration. To delete the service on purpose, set termination_protection = false, apply, then destroy.

Service Settings as JSON

pg_settings, pgbouncer_settings, pglookout_settings and timescaledb_settings are JSON strings, written with jsonencode(). Only keys of the settings catalog are accepted. List them with:

exo dbaas type show pg --settings pg

Replace pg with pgbouncer or pglookout for those namespaces. For timescaledb, read the catalog from the API with exo api get-dbaas-settings-pg. Types, ranges and the settings that cannot be changed are described in Configure PostgreSQL settings.

The full list of attributes of the pg object, including shared_buffers_percentage, work_mem, variant and recovery_backup_time, is in the provider documentation.

Manage Databases, Users and Connection Pools

Three resources attach to the service by its name and zone:

resource "exoscale_dbaas_pg_database" "app" {
  database_name = "app"
  service       = exoscale_dbaas.my_postgresql.name
  zone          = exoscale_dbaas.my_postgresql.zone
}

resource "exoscale_dbaas_pg_user" "app" {
  username = "app_user"
  service  = exoscale_dbaas.my_postgresql.name
  zone     = exoscale_dbaas.my_postgresql.zone
}

resource "exoscale_dbaas_pg_connection_pool" "app" {
  name          = "app-pool"
  database_name = exoscale_dbaas_pg_database.app.database_name
  username      = exoscale_dbaas_pg_user.app.username
  service       = exoscale_dbaas.my_postgresql.name
  zone          = exoscale_dbaas.my_postgresql.zone
  mode          = "transaction"
  size          = 10
}
  • exoscale_dbaas_pg_user has no password argument: the service generates the password and the resource exposes it in the sensitive attribute password. The optional allow_replication argument sets the replication attribute of the user.
  • exoscale_dbaas_pg_database optionally takes lc_collate and lc_ctype. Owners, grants and extensions are managed in SQL: see Manage users and databases and Manage extensions.
  • exoscale_dbaas_pg_connection_pool takes mode (transaction, statement or session) and size, and exposes the sensitive connection_uri. See Manage connection pools.

The provider has no resource for read replicas: create them with the API, as described in Use read replicas.

Read Connection Details

The uri attribute of the service has no credentials, and ca_certificate holds the CA certificate. For a URI with the administrator’s credentials, use the exoscale_database_uri data source:

data "exoscale_database_uri" "my_postgresql" {
  name = exoscale_dbaas.my_postgresql.name
  type = "pg"
  zone = exoscale_dbaas.my_postgresql.zone
}

It returns uri and password, both sensitive, and host, port, db_name and username.

Warning

The Terraform state holds every sensitive value of these resources in clear text: the administrator password, the generated user passwords, the pool URI and the CA certificate. Store the state in a backend with restricted access and encryption.

Import an Existing Service

An existing service is imported as NAME@ZONE, its databases, users and pools as NAME/OBJECT@ZONE:

terraform import exoscale_dbaas.my_postgresql my-postgresql@ch-gva-2
terraform import exoscale_dbaas_pg_database.app my-postgresql/app@ch-gva-2
terraform import exoscale_dbaas_pg_user.app my-postgresql/app_user@ch-gva-2
terraform import exoscale_dbaas_pg_connection_pool.app my-postgresql/app-pool@ch-gva-2

With a wrong identifier, the provider prints the format it expects, for example Expected import identifier with format: service/username@zone.

The administrator credentials are not read back from the service. Remove admin_username and admin_password from the configuration of an imported service: with them, every plan shows a change, and the apply fails with Provider produced inconsistent result after apply.

Run terraform plan after the import and make sure it shows no change before you apply anything.

Related Pages

Last updated on