Skip to content

Manage PostgreSQL Flex with Terraform

In this guide, you learn how to create a PostgreSQL Flex instance with Terraform. You will set up an environment first. After completing the guide, you’ll have basic knowledge of Terraform with PostgreSQL Flex. After completion, you can get further knowledge from the linked resources.

  • You have a service account with sufficient rights.
  • You have a key file for your service account.
  • Your client has Terraform installed.
  1. Verify that Terraform is accessible.

    Terminal window
    terraform --version

    You get something like:

    Terraform v1.12.2
    on darwin_arm64
  2. Create a folder for your Terraform project and change directory.

    Terminal window
    mkdir stackit-terraform-postgresql
    cd stackit-terraform-postgresql
  3. Copy your service account key file into the folder and rename it to service-account-key.json. This guide assumes that your downloaded key account file is in your download folder and has the name sa-key-ee76107b-8923-4661-9ae6-27417d5004f0.json. Please change the next command according to your local environment.

    Terminal window
    mv ~/Downloads/sa-key-ee76107b-8923-4661-9ae6-27417d5004f0.json ./service-account-key.json
  4. Create main.tf.

    Terminal window
    terraform {
    required_providers {
    stackit = {
    source = "stackitcloud/stackit"
    version = "~> 0.30.0" # Verwende die neueste Version
    }
    }
    }
    provider "stackit" {
    service_account_key_path = "./service-account-key.json"
    region = "eu01"
    }
    resource "stackit_postgresqlflex_instance" "example" {
    project_id = var.project_id
    name = "my-postgresql-instance"
    acl = [
    "79.206.199.114/32"
    ]
    backup_schedule = "0 2 * * *" # Täglich um 2 Uhr nachts
    flavor = {
    cpu = 1
    ram = 4
    }
    storage = {
    class = "premium-perf6-postgresql"
    size = 5
    }
    version = "17.0"
    replicas = 1
    options = {
    type = "Single"
    }
    }
    resource "stackit_postgresql_user" "example_user" {
    project_id = var.project_id
    instance_id = stackit_postgresqlflex_instance.example.instance_id
    username = "myuser"
    database = "default"
    roles = ["readWrite"]
    }
  5. Create variables.tf.

    variable "project_id" {
    description = "STACKIT Project ID"
    type = string
    }
  6. Create terraform.tfvars.

    project_id = "<PROJECT_ID>"
  7. Execute.

    Terminal window
    terraform plan
    terraform apply
  8. Check the output.

    You get something like:

    ...
    Do you want to perform these actions?
    Terraform will perform the actions described above.
    Only 'yes' will be accepted to approve.
    Enter a value: yes
    stackit_postgresqlflex_instance.example: Creating...
    stackit_postgresqlflex_instance.example: Still creating... [10s elapsed]
    stackit_postgresqlflex_instance.example: Still creating... [20s elapsed]
    stackit_postgresqlflex_instance.example: Still creating... [30s elapsed]
    stackit_postgresqlflex_instance.example: Still creating... [40s elapsed]
    stackit_postgresqlflex_instance.example: Still creating... [50s elapsed]
    stackit_postgresqlflex_instance.example: Still creating... [1m0s elapsed]
    stackit_postgresqlflex_instance.example: Still creating... [1m10s elapsed]
    stackit_postgresqlflex_instance.example: Still creating... [1m20s elapsed]
    stackit_postgresqlflex_instance.example: Still creating... [1m30s elapsed]
    stackit_postgresqlflex_instance.example: Still creating... [1m40s elapsed]
    stackit_postgresqlflex_instance.example: Creation complete after 1m42s [id=118e6780-c9d0-48d0-87a3-044fe90c17c4,4cf6f720-1524-40dc-b76f-64e6533b1453]
    stackit_postgresqlflex_user.example_user: Creating...
    stackit_postgresqlflex_user.example_user: Creation complete after 1s [id=118e6780-c9d0-48d0-87a3-044fe90c17c4,4cf6f720-1524-40dc-b76f-64e6533b1453,8f1b5a44-5e10-4a3b-86ab-5377cfb711df]
    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

You succesfully created your first PostgreSQL Flex instance and user with Terraform. You can have a look at the file terraform.tfstate to get an idea what Terraform did. You can also explore STACKIT’s Terraform provider GitHub repository to learn more about its capabilities. For more information about Terraform in general, please visit the official Terraform website.