apps/docs/content/docs/postgres/iac/terraform.mdx
Use the Prisma Postgres Terraform provider to manage projects, databases, and connections with code.
Terraform is a desired-state engine:
.tf files.terraform plan) by comparing config vs current state.terraform apply) and records the result in state.For Prisma Postgres, this gives a predictable workflow for creating projects, databases, and connections across environments.
Terraform is a strong fit when:
plan output before applying changes.The provider currently supports:
prisma-postgres_projectprisma-postgres_databaseprisma-postgres_connectionprisma-postgres_regions data source>= 1.0Set your token as an environment variable:
export PRISMA_SERVICE_TOKEN="prsc_your_token_here"
main.tfCreate the following Terraform configuration:
terraform {
required_providers {
prisma-postgres = {
source = "prisma/prisma-postgres"
}
}
}
provider "prisma-postgres" {}
resource "prisma-postgres_project" "main" {
name = "my-app"
}
resource "prisma-postgres_database" "production" {
project_id = prisma-postgres_project.main.id
name = "production"
region = "us-east-1"
}
resource "prisma-postgres_connection" "api" {
database_id = prisma-postgres_database.production.id
name = "api-key"
}
output "connection_string" {
value = prisma-postgres_connection.api.connection_string
sensitive = true
}
output "direct_url" {
value = prisma-postgres_database.production.direct_url
sensitive = true
}
Initialize your working directory:
terraform init
Review and apply:
terraform plan
terraform apply
After apply, retrieve values:
terraform output -raw connection_string
terraform output -raw direct_url
terraform destroy
If you want to select regions dynamically:
data "prisma-postgres_regions" "available" {}
output "available_regions" {
value = [
for r in data.prisma-postgres_regions.available.regions : "${r.id} (${r.name})"
if r.status == "available"
]
}
sensitive = true, secret values are still stored in state.PRISMA_SERVICE_TOKEN in your secret manager or CI secrets, not in code.dev, staging, and prod.You can import existing resources into state:
terraform import prisma-postgres_project.main <project-id>
terraform import prisma-postgres_database.production <database-id>
terraform import prisma-postgres_connection.api <database-id>,<connection-id>
Credentials are only returned at creation time and cannot be recovered after import.
If provider configuration fails with a missing token error, confirm PRISMA_SERVICE_TOKEN is set in the same shell session running Terraform.
If create fails for a region value, use prisma-postgres_regions to list currently available regions for your workspace.
If you receive authorization errors, verify your service token belongs to the expected workspace and has permission to create projects and databases.