---
title: "Terraform"
description: "Manage PgBeam projects, databases, replicas, custom domains, cache rules, spend limits, agent credentials, policy profiles, webhook endpoints, self host enrollments, honeytokens, anomaly rules, and approval rules as infrastructure using Terraform and the pgbeam provider."
canonical: "https://pgbeam.com/docs/terraform"
last-updated: "2026-10-08T14:03:05.000Z"
---

# Terraform

> Manage PgBeam projects, databases, replicas, custom domains, cache rules, spend limits, agent credentials, policy profiles, webhook endpoints, self host enrollments, honeytokens, anomaly rules, and approval rules as infrastructure using Terraform and the pgbeam provider.

URL: https://pgbeam.com/docs/terraform

Manage your PgBeam infrastructure as code with Terraform. The `pgbeam` provider offers native resources for projects, databases, replicas, custom domains, cache rules, spend limits, agent credentials, policy profiles, webhook endpoints, self host enrollments, honeytokens, anomaly rules, and approval rules.

## Setup

## Configure the provider

The Terraform provider is coming soon. Registry publishing is on the roadmap.

## Configure credentials

Set your PgBeam API key via provider config or environment variable:

## Create a project

## Deploy

Terraform creates the PgBeam project and its primary database atomically. The `proxy_host` output gives you the PgBeam proxy endpoint to use in your application connection string.

## Resources

**Every policy rule type is managed as code.** Policy profiles, honeytokens,
anomaly rules and approval rules are managed resources, so the enforcement
rules, the decoys, the detection sensitivity and what needs a human all live
in your reviewed IaC flow and are covered by drift detection. Approving or
rejecting an individual held statement is an operational decision rather than
configuration, so it stays in the dashboard, the CLI and the API.

## pgbeam_project

Manages a PgBeam project with a primary database.

**Computed:** `proxy_host`, `queries_per_second`, `burst_size`, `max_connections`, `database_count`, `active_connections`, `created_at`, `updated_at`, `primary_database_id`

**Import:** `terraform import pgbeam_project.example <id>`

## pgbeam_database

Manages an upstream database connection within a PgBeam project.

**Computed:** `connection_string`, `created_at`, `updated_at`

**Import:** `terraform import pgbeam_database.example <project_id>/<id>`

## pgbeam_replica

Manages a read replica for a PgBeam database.

Replicas are immutable; any property change triggers replacement.

**Computed:** `created_at`, `updated_at`

**Import:** `terraform import pgbeam_replica.example <database_id>/<id>`

## pgbeam_custom_domain

Manages a custom domain for a PgBeam project.

CustomDomains are immutable; any property change triggers replacement.

**Computed:** `verified`, `verified_at`, `tls_cert_expiry`, `dns_verification_token`, `dns_instructions`, `created_at`, `updated_at`

**Import:** `terraform import pgbeam_custom_domain.example <project_id>/<id>`

## pgbeam_cache_rule

Manages a per-query cache rule. Deletion disables caching (soft-delete).

**Computed:** `query_hash`, `normalized_sql`, `query_type`, `call_count`, `avg_latency_ms`, `p95_latency_ms`, `avg_response_bytes`, `stability_rate`, `recommendation`, `first_seen_at`, `last_seen_at`

**Import:** `terraform import pgbeam_cache_rule.example <project_id>/<database_id>/<query_hash>`

## pgbeam_spend_limit

Manages the monthly spend limit for an organization.

**Computed:** `org_id`, `plan`, `billing_provider`, `subscription_status`, `current_period_end`, `enabled`, `custom_pricing`, `spend_capped`, `spend_capped_at`, `limits`, `created_at`, `updated_at`

**Import:** `terraform import pgbeam_spend_limit.example <org_id>`

## pgbeam_agent_credential

Manages a scoped agent credential (a PgBeam-issued Postgres login plus a hosted MCP token) for an AI agent. The connection string and MCP token are one-time secrets returned only at creation and exposed as sensitive computed outputs; they cannot be retrieved again. To rotate the secrets, taint/replace the resource (or use the rotate endpoint out of band).

**Computed:** `pg_username`, `auth_method`, `last_used_at`, `created_at`, `updated_at`, `connection_string`, `mcp_url`, `mcp_token`

**Import:** `terraform import pgbeam_agent_credential.example <project_id>/<id>`

## pgbeam_policy_profile

Manages a policy profile: a named bundle of agent-gateway enforcement rules (access mode, table allow/deny lists, statement-kind rules, PII masking rules, per-relation row filters, query/egress budgets, write mode, approvals, and migration safety) attached to agent credentials and enforced in the PG wire protocol. Nested-list fields (masking_rules, row_filters) and the nested statement_rules object are expressed as structured config.

**Computed:** `created_at`, `updated_at`

**Import:** `terraform import pgbeam_policy_profile.example <project_id>/<id>`

## pgbeam_webhook_endpoint

Manages a webhook endpoint that receives project audit and anomaly event deliveries. The signing secret is write-only and never returned by the API.

**Computed:** `created_at`, `updated_at`

**Import:** `terraform import pgbeam_webhook_endpoint.example <project_id>/<id>`

## pgbeam_self_host_enrollment

Manages a self-host (BYOC) enrollment: a token a self-hosted proxy uses to authenticate to the control plane's config/audit stream. The token is a one-time secret returned only at creation and exposed as a sensitive computed output; it cannot be retrieved again. To rotate the token, replace the resource. Deletion revokes the enrollment.

SelfHostEnrollments are immutable; any property change triggers replacement.

**Computed:** `created_by`, `created_at`, `last_seen_at`, `revoked_at`, `token`

**Import:** `terraform import pgbeam_self_host_enrollment.example <org_id>/<id>`

## pgbeam_honeytoken

Manages a honeytoken: a decoy (canary) relation that no legitimate query should ever touch. Any agent statement referencing it is blocked and recorded as a canary_tripped audit event; the kill action additionally disables the tripping credential via the kill-switch. The relation does not have to exist in the upstream database: enforcement is by name, in the wire protocol, before the statement reaches Postgres.

**Computed:** `created_at`, `updated_at`

**Import:** `terraform import pgbeam_honeytoken.example <project_id>/<id>`

## pgbeam_anomaly_rule

Manages an anomaly rule: the sensitivity of one detection metric for one project, or for one agent credential in it. Without a rule every metric resolves to the deployment default, so a credential that legitimately runs at 3am cannot be exempted and a noisy one cannot be loosened. Note the polarity of enabled: the rule IS the switch, so enabled=false silences the metric for that scope rather than deactivating the rule. One rule exists per (project, credential, metric); a second one for the same scope is a conflict. Detection runs in the control plane and re-reads the rules on every pass, so a write takes effect at the next detection pass rather than being pushed to the proxy.

**Computed:** `created_at`, `updated_at`

**Import:** `terraform import pgbeam_anomaly_rule.example <project_id>/<id>`

## pgbeam_approval_rule

Manages an approval rule: a named, project-scoped rule that sends the agent statements it matches to a human for approval, whatever the policy profile's approval_mode says. A rule can only add approval; no rule can exempt a statement from approval_mode. Match on statement kinds, a schema, a relation, and optionally a minimum affected-row count. The name is shown to the reviewer on every approval request the rule holds and must be unique within the project. enabled=false switches the rule off. A write is pushed to connected data planes immediately.

**Computed:** `created_at`, `updated_at`

**Import:** `terraform import pgbeam_approval_rule.example <project_id>/<id>`

## Configuration

Setting

Source

Description

`api_key`

Provider block

API key (sensitive, recommended: use a variable)

`base_url`

Provider block

API base URL (default: `https://api.pgbeam.com`)

`PGBEAM_API_KEY`

Environment

Fallback API key

`PGBEAM_API_URL`

Environment

Fallback base URL

Config resolution order: provider block > environment variables.

## Replacement vs update

Some property changes trigger resource replacement (destroy + create) rather than in-place updates:

Resource

Replacement triggers

Project

`org_id`, `cloud`, `self_hosted`

Database

`project_id`

Replica

Any property change (immutable)

CustomDomain

Any property change (immutable)

CacheRule

`project_id`, `database_id`, `query_hash`

SpendLimit

`org_id`

AgentCredential

`project_id`, `policy_profile_id`, `name`, `principal_type`, `expires_at`

PolicyProfile

`project_id`

WebhookEndpoint

`project_id`

SelfHostEnrollment

Any property change (immutable)

Honeytoken

`project_id`

AnomalyRule

`project_id`

ApprovalRule

`project_id`

## Further reading

Connection Pooling: pool modes and sizing

Caching: query caching and SWR

Read Replicas: replica routing

Custom Domains: DNS setup and verification

API Keys: managing API credentials

Plans: plan limits and pricing