terraform-provider-postgresql

command module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 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, databases, schemas, grants, and default privileges.

Requirements

  • Terraform >= 1.0
  • Go >= 1.25 (for building)
  • PostgreSQL >= 12

Installation

terraform {
  required_providers {
    postgresql = {
      source  = "DiegoBulhoes/postgresql"
      version = "~> 0.1"
    }
  }
}

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 (users/groups)
postgresql_database Manages databases
postgresql_schema Manages schemas
postgresql_grant Manages GRANT privileges on objects
postgresql_default_privileges Manages ALTER DEFAULT PRIVILEGES

Data Sources

Data Source Description
postgresql_role Reads role attributes
postgresql_database Reads database attributes
postgresql_schemas Lists schemas with filters
postgresql_query Executes a SELECT query and returns results

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

Build
make build
Tests

Tests use testcontainers-go to automatically spin up a PostgreSQL instance via Docker.

# Run all acceptance tests
make testacc

# With coverage
make testacc-cover

# Generate coverage HTML report
make cover-html

See TESTING.md for more details.

Generate documentation
# Install tfplugindocs
go install github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs@latest

# Generate docs from templates and schema
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

Documentation ΒΆ

The Go Gopher

There is no documentation for this package.

Directories ΒΆ

Path Synopsis
internal

Jump to

Keyboard shortcuts

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