Terraform Provider PostgreSQL π§
π§ This provider is under active development. Some features may change before the first stable release.
Terraform provider for PostgreSQL. It manages roles, users, databases, schemas, and grants.
Requirements
- Terraform >= 1.0
- Go >= 1.26 (for building)
- PostgreSQL >= 14
Installation
terraform {
required_providers {
postgresql = {
source = "DiegoBulhoes/postgresql"
version = "~> 0.2"
}
}
}
Usage
provider "postgresql" {
host = "localhost"
port = 5432
username = "postgres"
password = "postgres"
database = "postgres"
sslmode = "disable"
}
You can also configure the provider with environment variables: PGHOST, PGPORT, PGUSER, PGPASSWORD, PGDATABASE, PGSSLMODE.
Resources
Data Sources
Examples
Create a role, user, and database
resource "postgresql_role" "app_role" {
name = "app_role"
privilege {
privileges = ["SELECT", "INSERT", "UPDATE", "DELETE"]
object_type = "table"
schema = "public"
}
}
resource "postgresql_user" "app" {
name = "app_user"
password = "secret"
roles = [postgresql_role.app_role.name]
}
resource "postgresql_database" "app" {
name = "app_db"
owner = postgresql_user.app.name
encoding = "UTF8"
}
Grant privileges
resource "postgresql_schema" "app" {
name = "app_schema"
owner = postgresql_user.app.name
}
resource "postgresql_grant" "app_schema" {
role = postgresql_user.app.name
object_type = "schema"
schema = postgresql_schema.app.name
privileges = ["USAGE", "CREATE"]
}
Query data
data "postgresql_role" "current" {
name = "postgres"
}
data "postgresql_schemas" "app" {
like_pattern = "app_%"
include_system_schemas = false
}
data "postgresql_query" "version" {
database = "postgres"
query = "SELECT version() AS pg_version"
}
Documentation
Every resource and data source is documented in docs/ and on the Terraform Registry.
Development
Setup
git clone https://github.com/DiegoBulhoes/terraform-provider-postgresql.git
cd terraform-provider-postgresql
make build
That's it. The Go tools (golangci-lint, goimports, tfplugindocs) are declared in go.mod and resolved by go tool, so there is nothing to install by hand.
Build
make build
Tests
Tests use testcontainers-go to start a PostgreSQL container. Docker must be running.
make test # Unit + acceptance tests (PG 14, 15, 16, 17)
See TESTING.md for the details.
make lint # golangci-lint (via go tool)
make fmt # gofmt + goimports (via go tool)
Generate documentation
make docs
The docs are generated from templates/ and examples/. Never edit files in docs/ by hand.
Project structure
.
βββ main.go # Provider entrypoint
βββ internal/
β βββ provider/provider.go # Provider configuration
β βββ resource/ # Resources
β βββ datasource/ # Data Sources
β βββ common/ # Shared helpers
βββ test/
β βββ acctest/ # Acceptance test infrastructure
β βββ mocks/ # Generated GoMock mocks
β βββ unit/ # Unit tests (common, resource, datasource, provider)
β βββ integration/ # Acceptance tests (resource, datasource, provider)
βββ docs/ # Generated documentation (do not edit)
βββ templates/ # Documentation templates
βββ examples/ # HCL examples used in docs
βββ .github/workflows/
βββ test.yml # CI: lint + tests
βββ release.yml # CD: GoReleaser
Claude Code Skills
This project ships custom Claude Code skills in .claude/:
| Skill |
Description |
golang |
Writes idiomatic Go, with Terraform provider patterns |
terraform |
Writes Terraform and OpenTofu code in the official HashiCorp style |
explore |
Explores the repository: analyses code, traces dependencies, reports gaps |
There is also a terraform-expert agent for harder provider work.
License
LICENSE