terraform-provider-postgresql

command module
v0.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2026 License: MIT Imports: 4 Imported by: 0

README ΒΆ

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, 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"
}

The provider also accepts configuration via environment variables: PGHOST, PGPORT, PGUSER, PGPASSWORD, PGDATABASE, PGSSLMODE.

Resources

Resource Description
postgresql_role Manages roles (permission groups with inline privileges)
postgresql_user Manages users (login roles with role memberships)
postgresql_database Manages databases
postgresql_schema Manages schemas
postgresql_grant Manages GRANT privileges on objects

Data Sources

Data Source Description
postgresql_role Reads role attributes
postgresql_user Reads user attributes
postgresql_roles Lists roles with filters
postgresql_database Reads database attributes
postgresql_schemas Lists schemas with filters
postgresql_tables Lists tables with filters
postgresql_extensions Lists installed extensions
postgresql_version Reads server version info
postgresql_query Executes a SQL query and returns results

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

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 + acceptance tests (PG 14, 15, 16, 17)

See TESTING.md for more details.

Lint & Format
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
β”œβ”€β”€ 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 uses custom Claude Code skills (.claude/) to assist with development:

Skill Description
golang Go code generation following idiomatic conventions, with Terraform provider-specific patterns
terraform Terraform/OpenTofu IaC code generation following HashiCorp official style guide
explore Repository explorer for codebase analysis, dependency tracing, and gap reporting

An agent configuration (terraform-expert) is also available for advanced Terraform provider development tasks.

License

LICENSE

Documentation ΒΆ

The Go Gopher

There is no documentation for this package.

Directories ΒΆ

Path Synopsis
internal
test
mocks
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL