Terraform Provider PostgreSQL π§
π§ This provider is under active development. Some features may change before the first stable release.
Terraform provider for managing PostgreSQL resources: roles, databases, schemas, grants, and default privileges.
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"
}
The provider also accepts configuration via environment variables: PGHOST, PGPORT, PGUSER, PGPASSWORD, PGDATABASE, PGSSLMODE.
Resources
Data Sources
Examples
Create a role and database
resource "postgresql_role" "app" {
name = "app_user"
login = true
password = "secret"
create_database = false
}
resource "postgresql_database" "app" {
name = "app_db"
owner = postgresql_role.app.name
encoding = "UTF8"
}
Grant privileges
resource "postgresql_schema" "app" {
name = "app_schema"
owner = postgresql_role.app.name
}
resource "postgresql_grant" "app_schema" {
role = postgresql_role.app.name
object_type = "schema"
schema = postgresql_schema.app.name
privileges = ["USAGE", "CREATE"]
}
resource "postgresql_default_privileges" "tables" {
owner = postgresql_role.app.name
role = postgresql_role.app.name
database = postgresql_database.app.name
schema = postgresql_schema.app.name
object_type = "table"
privileges = ["SELECT", "INSERT", "UPDATE", "DELETE"]
}
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
Full documentation for each resource and data source is available in the docs/ directory 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. All Go tools (golangci-lint, goimports, tfplugindocs) are declared in go.mod and resolved automatically via go tool β no manual installation needed.
Build
make build
Tests
Tests use testcontainers-go to automatically spin up a PostgreSQL instance via Docker. Requires Docker running.
make test # Unit tests only (no Docker needed)
make testacc # Acceptance tests (PG 14, 15, 16, 17)
make testacc-cover # With coverage
make cover-html # Generate HTML coverage report
See TESTING.md for more details.
make lint # golangci-lint (via go tool)
make fmt # gofmt + goimports (via go tool)
Generate documentation
make docs
Documentation is generated from templates in templates/ and examples in examples/. Do not edit files in docs/ directly.
Project structure
.
βββ main.go # Provider entrypoint
βββ internal/
β βββ provider/provider.go # Provider configuration
β βββ resource/ # Resources
β βββ datasource/ # Data Sources
β βββ common/ # Shared helpers
βββ 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
License
LICENSE