### Terraform Import Command Example Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_schema.html.markdown Shows the command-line syntax for importing a `postgresql_schema` resource into Terraform state, specifying the database and schema names. ```bash $ terraform import postgresql_schema.schema_foo my_database.my_schema ``` -------------------------------- ### Basic PostgreSQL Subscription Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_subscription.markdown Example of creating a basic PostgreSQL subscription. Ensure the 'publications' listed exist on the publisher. ```hcl resource "postgresql_subscription" "subscription" { name = "subscription" conninfo = "host=localhost port=5432 dbname=mydb user=postgres password=postgres" publications = ["publication"] } ``` -------------------------------- ### Basic PostgreSQL Schema Creation Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_schema.html.markdown Example of creating a PostgreSQL schema with specified owner and access policies for different roles. ```hcl resource "postgresql_role" "app_www" { name = "app_www" } resource "postgresql_role" "app_dba" { name = "app_dba" } resource "postgresql_role" "app_releng" { name = "app_releng" } resource "postgresql_schema" "my_schema" { name = "my_schema" owner = "postgres" policy { usage = true role = "${postgresql_role.app_www.name}" } # app_releng can create new objects in the schema. This is the role that # migrations are executed as. policy { create = true usage = true role = "${postgresql_role.app_releng.name}" } policy { create_with_grant = true usage_with_grant = true role = "${postgresql_role.app_dba.name}" } } ``` -------------------------------- ### PostgreSQL Replication Role with Connection Limit Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_role.html.markdown This example shows how to create a replication role with specific connection limits and a pre-hashed password. ```hcl resource "postgresql_role" "my_replication_role" { name = "replication_role" replication = true login = true connection_limit = 5 password = "md5c98cbfeb6a347a47eb8e96cfb4c4b890" } ``` -------------------------------- ### Basic postgresql_user_mapping Resource Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_user_mapping.html.markdown This example demonstrates how to create a user mapping for a remote PostgreSQL server. It requires a PostgreSQL extension, server, and role to be defined first. ```hcl resource "postgresql_extension" "ext_postgres_fdw" { name = "postgres_fdw" } resource "postgresql_server" "myserver_postgres" { server_name = "myserver_postgres" fdw_name = "postgres_fdw" options = { host = "foo" dbname = "foodb" port = "5432" } depends_on = [postgresql_extension.ext_postgres_fdw] } resource "postgresql_role" "remote" { name = "remote" } resource "postgresql_user_mapping" "remote" { server_name = postgresql_server.myserver_postgres.server_name user_name = postgresql_role.remote.name options = { user = "admin" password = "pass" } } ``` -------------------------------- ### Setup Local Docker PostgreSQL for Acceptance Tests Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/README.md Set up a local Docker PostgreSQL container to run acceptance tests. This command is part of the local development workflow for acceptance tests. ```sh # spins up a local docker postgres container make testacc_setup ``` -------------------------------- ### Import an Existing PostgreSQL Database Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_database.html.markdown This example demonstrates how to import an existing PostgreSQL database into Terraform management. The database name is provided as an argument to the `terraform import` command. ```bash $ terraform import postgresql_database.db1 testdb1 ``` -------------------------------- ### Create a PostgreSQL Extension Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_extension.html.markdown This example shows how to create a PostgreSQL extension named 'pg_trgm'. Ensure the extension is available on your PostgreSQL server. ```hcl resource "postgresql_extension" "my_extension" { name = "pg_trgm" } ``` -------------------------------- ### Import a PostgreSQL Publication Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_publication.markdown This example demonstrates the format for importing an existing PostgreSQL publication into Terraform state. The format requires the database name and publication name. ```bash terraform import postgresql_publication.publication {{database_name}}.{{publication_name}} ``` -------------------------------- ### Import a PostgreSQL Extension Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_extension.html.markdown This example demonstrates how to import an existing PostgreSQL extension into Terraform state. The format requires the database name and the extension's resource name, separated by a dot. ```bash terraform import postgresql_extension.uuid_ossp example-database.uuid-ossp ``` -------------------------------- ### PostgreSQL Role with Write-Only Password Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_role.html.markdown Example of setting up an initial PostgreSQL role with a write-only password and version. Use this for secure password management where the password should not be stored in Terraform state. ```hcl # Initial password setup resource "postgresql_role" "app_user" { name = "app_user" login = true password_wo = "initial_password_123" password_wo_version = "1" } # To rotate the password, update both attributes: # password_wo = "new_password_456" # password_wo_version = "2" ``` -------------------------------- ### Create a PostgreSQL Function Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_function.html.markdown Example of creating a simple PostgreSQL function named 'increment' that adds 1 to an integer input. This snippet demonstrates the basic usage of the `postgresql_function` resource, including defining arguments, return type, language, and function body. ```hcl resource "postgresql_function" "increment" { name = "increment" arg { name = "i" type = "integer" } returns = "integer" language = "plpgsql" body = <<-EOF BEGIN RETURN i + 1; END; EOF } ``` -------------------------------- ### Grant Default Table Privileges Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_default_privileges.html.markdown This example grants default SELECT, INSERT, and UPDATE privileges on new tables created by 'owner_role' to the 'current_role' within the 'public' schema of a dynamically named database. This ensures 'current_role' automatically receives these permissions on any new tables created by 'owner_role'. ```hcl resource "postgresql_default_privileges" "grant_table_privileges" { database = postgresql_database.example_db.name role = "current_role" owner = "owner_role" schema = "public" object_type = "table" privileges = ["SELECT", "INSERT", "UPDATE"] } ``` -------------------------------- ### Create a PostgreSQL Database Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_database.html.markdown This snippet shows the basic configuration for creating a new PostgreSQL database with specified name, owner, template, and collation settings. ```hcl resource "postgresql_database" "my_db" { name = "my_db" owner = "my_role" template = "template0" lc_collate = "C" connection_limit = -1 allow_connections = true alter_object_ownership = true } ``` -------------------------------- ### Build Terraform Provider Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/README.md Build the Terraform provider plugin. This command compiles the provider and places the binary in the $GOPATH/bin directory. ```sh $ cd $GOPATH/src/github.com/cyrilgdn/terraform-provider-postgresql $ make build ``` -------------------------------- ### Build and Run Terraform Provider Binary Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/README.md Build the provider and then run the provider binary. This is part of the development process. ```sh $ make build ... $ $GOPATH/bin/terraform-provider-postgresql ... ``` -------------------------------- ### Create a PostgreSQL Foreign Server with Options Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_server.html.markdown This snippet demonstrates how to create a PostgreSQL foreign server named 'myserver_postgres' using the 'postgres_fdw' foreign-data wrapper. It includes connection options for host, database name, and port. The creation depends on the 'postgres_fdw' extension being enabled. ```hcl resource "postgresql_extension" "ext_postgres_fdw" { name = "postgres_fdw" } resource "postgresql_server" "myserver_postgres" { server_name = "myserver_postgres" fdw_name = "postgres_fdw" options = { host = "foo" dbname = "foodb" port = "5432" } depends_on = [postgresql_extension.ext_postgres_fdw] } ``` -------------------------------- ### Basic PostgreSQL Role Creation Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_role.html.markdown This snippet demonstrates the basic creation of a PostgreSQL role with a specified name, login privilege, and password. ```hcl resource "postgresql_role" "my_role" { name = "my_role" login = true password = "mypass" } ``` -------------------------------- ### Create a PostgreSQL Foreign Server without Options Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_server.html.markdown This snippet shows how to create a PostgreSQL foreign server named 'myserver_file' using the 'file_fdw' foreign-data wrapper. It does not specify any connection options. The creation depends on the 'file_fdw' extension being enabled. ```hcl resource "postgresql_extension" "ext_file_fdw" { name = "file_fdw" } resource "postgresql_server" "myserver_file" { server_name = "myserver_file" fdw_name = "file_fdw" depends_on = [postgresql_extension.ext_file_fdw] } ``` -------------------------------- ### Configuring Multiple PostgreSQL Servers with Aliases Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/index.html.markdown Set up and manage connections to multiple PostgreSQL servers by assigning unique aliases to each provider configuration. This allows specifying which provider to use for specific resources. ```hcl provider "postgresql" { alias = "pg1" host = "postgres_server_ip1" username = "postgres_user1" password = "postgres_password1" } provider "postgresql" { alias = "pg2" host = "postgres_server_ip2" username = "postgres_user2" password = "postgres_password2" } resource "postgresql_database" "my_db1" { provider = "postgresql.pg1" name = "my_db1" } resource "postgresql_database" "my_db2" { provider = "postgresql.pg2" name = "my_db2" } ``` -------------------------------- ### Basic PostgreSQL Provider Configuration Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/index.html.markdown Configure the PostgreSQL provider with essential connection details like host, port, database, username, password, and SSL mode. ```hcl provider "postgresql" { host = "postgres_server_ip" port = 5432 database = "postgres" username = "postgres_user" password = "postgres_password" sslmode = "require" connect_timeout = 15 } ``` -------------------------------- ### Create a PostgreSQL Publication Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_publication.markdown This snippet shows how to create a basic PostgreSQL publication, specifying its name and the tables to be included. Ensure tables are listed in alphabetical order. ```hcl resource "postgresql_publication" "publication" { name = "publication" tables = ["public.test","another_schema.test"] } ``` -------------------------------- ### Basic Usage of postgresql_tables Data Source Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/d/postgresql_tables.html.markdown Use this data source to retrieve a list of table names from a specified PostgreSQL database. Ensure the 'database' argument is provided. ```hcl data "postgresql_tables" "my_tables" { database = "my_database" } ``` -------------------------------- ### Run Terraform Provider Acceptance Tests Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/README.md Execute the full suite of acceptance tests for the Terraform provider. Note that these tests create real resources and may incur costs. ```sh $ make testacc ``` -------------------------------- ### Basic Usage of postgresql_physical_replication_slot Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_physical_replication_slot.markdown Creates a physical replication slot with a specified name. This is the most basic configuration. ```hcl resource "postgresql_physical_replication_slot" "my_slot" { name = "my_slot" } ``` -------------------------------- ### Basic postgresql_grant_role Usage Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_grant_role.html.markdown This snippet demonstrates the basic usage of the postgresql_grant_role resource to grant membership with admin option. ```hcl resource "postgresql_grant_role" "grant_root" { role = "root" grant_role = "application" with_admin_option = true } ``` -------------------------------- ### Run Specific Acceptance Test Locally Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/README.md Run a specific acceptance test locally with INFO level logging. This is useful for debugging and iterative development. ```sh # Run the test(s) that you're working on as often as you want TF_LOG=INFO go test -v ./postgresql -run ^TestAccPostgresqlRole_Basic$ ``` -------------------------------- ### Run Terraform Provider Tests Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/README.md Run the unit tests for the Terraform provider. This command checks the basic functionality of the provider. ```sh $ make test ``` -------------------------------- ### Create a PostgreSQL Replication Slot Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_replication_slot.markdown This snippet shows how to create a basic replication slot named 'my_slot' using the 'test_decoding' plugin. Ensure the plugin is available on your PostgreSQL server. ```hcl resource "postgresql_replication_slot" "my_slot" { name = "my_slot" plugin = "test_decoding" } ``` -------------------------------- ### Load Environment Variables for Acceptance Tests Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/README.md Load necessary environment variables for running acceptance tests. This script is typically sourced before running individual tests. ```sh # Load the needed environment variables for the tests source tests/switch_superuser.sh ``` -------------------------------- ### Importing a PostgreSQL Schema Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_schema.html.markdown Demonstrates how to import an existing PostgreSQL schema into Terraform state. This is useful for managing pre-existing database objects. ```hcl resource "postgresql_schema" "public" { name = "public" } resource "postgresql_schema" "schema_foo" { name = "my_schema" owner = "postgres" policy { usage = true } } ``` -------------------------------- ### Configure PostgreSQL Provider with Terraform Variables Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/index.html.markdown Define and use Terraform input variables for provider configuration. These variables can be set via .tfvars files, TF_VAR environment variables, or other Terraform methods. ```hcl variable "host" { default = "localhost" } variable "password" { default = "adm" } variable "port" { default = 55432 } provider "postgresql" { host = var.host port = var.port password = var.password sslmode = "disable" } resource postgresql_database "test" { name = "test" } ``` -------------------------------- ### Clone Terraform Provider Repository Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/README.md Clone the Terraform provider repository to your GOPATH. This is the first step in building the provider. ```sh $ mkdir -p $GOPATH/src/github.com/cyrilgdn; cd $GOPATH/src/github.com/cyrilgdn $ git clone git@github.com:cyrilgdn/terraform-provider-postgresql ``` -------------------------------- ### Basic Usage of postgresql_schemas Data Source Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/d/postgresql_schemas.html.markdown This snippet shows the basic configuration for the postgresql_schemas data source to retrieve schemas from a specific database. Ensure the 'database' argument is set to your target PostgreSQL database name. ```hcl data "postgresql_schemas" "my_schemas" { database = "my_database" } ``` -------------------------------- ### Basic Usage of postgresql_sequences Data Source Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/d/postgresql_sequences.html.markdown This snippet shows the basic configuration for the postgresql_sequences data source to retrieve sequence names from a specific database. Ensure the 'database' argument is set to your target PostgreSQL database. ```hcl data "postgresql_sequences" "my_sequences" { database = "my_database" } ``` -------------------------------- ### GCP SQL Connection with GoCloud (Standard) Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/index.html.markdown Configure the provider for GCP SQL using GoCloud with standard username and password authentication. Ensure the Cloud SQL API is enabled. ```hcl provider "postgresql" { scheme = "gcppostgres" host = "test-project/europe-west3/test-instance" username = "postgres" port = 5432 password = "test1234" superuser = false } ``` -------------------------------- ### Basic Default Privileges for Tables Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_default_privileges.html.markdown This snippet demonstrates how to set default SELECT privileges on new tables created by 'db_owner' for the 'test_role' in the 'public' schema of 'test_db'. ```hcl resource "postgresql_default_privileges" "read_only_tables" { role = "test_role" database = "test_db" schema = "public" owner = "db_owner" object_type = "table" privileges = ["SELECT"] } ``` -------------------------------- ### Set PostgreSQL Credentials via Environment Variables Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/index.html.markdown Use environment variables to configure PostgreSQL connection details for the provider. Ensure these variables are set before running Terraform. ```shell export PGHOST=localhost export PGPORT=5432 export PGUSER=postgres export PGPASSWORD=postgres ``` -------------------------------- ### Import an Existing PostgreSQL Function Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_function.html.markdown Demonstrates how to import an existing PostgreSQL function into Terraform state. This is useful for managing functions that were created outside of Terraform. The import command requires the database name, schema name, function name, and its argument signature. ```bash $ terraform import postgresql_function.function_foo "my_database.my_schema.my_function_name(arguments)" ``` -------------------------------- ### GCP SQL Connection using Terraform Resource Outputs Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/index.html.markdown Configure the provider to connect to a GCP SQL instance using outputs from Terraform resource definitions for the instance and user. ```hcl resource "google_sql_database_instance" "test" { project = "test-project" name = "test-instance" database_version = "POSTGRES_13" region = "europe-west3" settings { tier = "db-f1-micro" } } resource "google_sql_user" "postgres" { project = "test-project" name = "postgres" instance = google_sql_database_instance.test.name password = "xxxxxxxx" } provider "postgresql" { scheme = "gcppostgres" host = google_sql_database_instance.test.connection_name username = google_sql_user.postgres.name password = google_sql_user.postgres.password } resource postgresql_database "test_db" { name = "test_db" } ``` -------------------------------- ### PostgreSQL Provider Configuration with SSL Client Certificate Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/index.html.markdown Configure the PostgreSQL provider to use an SSL client certificate for secure connections. Ensure the `cert` and `key` paths are correctly set. ```hcl provider "postgresql" { host = "postgres_server_ip" port = 5432 database = "postgres" username = "postgres_user" password = "postgres_password" sslmode = "require" clientcert { cert = "/path/to/public-certificate.pem" key = "/path/to/private-key.pem" } } ``` -------------------------------- ### GCP SQL Connection with GoCloud and Service Account Impersonation Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/index.html.markdown Configure the provider for GCP SQL using GoCloud, specifying the connection name and enabling service account impersonation. Ensure GOOGLE_APPLICATION_CREDENTIALS is set and IAM permissions are configured. ```hcl provider "postgresql" { scheme = "gcppostgres" host = "test-project/europe-west3/test-instance" port = 5432 username = "service_account_id@$project_id.iam" gcp_iam_impersonate_service_account = "service_account_id@$project_id.iam.gserviceaccount.com" superuser = false } ``` -------------------------------- ### Basic PostgreSQL Security Label Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_security_label.html.markdown This snippet demonstrates how to create a security label for a PostgreSQL role using the postgresql_security_label resource. Ensure the PostgreSQL version is 11 or higher. ```hcl resource "postgresql_role" "my_role" { name = "my_role" login = true } resource "postgresql_security_label" "workload" { object_type = "role" object_name = postgresql_role.my_role.name label_provider = "pgaadauth" label = "aadauth,oid=00000000-0000-0000-0000-000000000000,type=service" } ``` -------------------------------- ### Cleanup Docker PostgreSQL Environment Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/README.md Clean up the environment and tear down the Docker PostgreSQL container after running acceptance tests. This command ensures resources are released. ```sh # cleans the env and tears down the postgres container make testacc_cleanup ``` -------------------------------- ### AWS RDS Connection with GoCloud Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/index.html.markdown Configure the provider to connect to AWS RDS instances using the GoCloud library by setting the scheme to 'awspostgres' and providing the RDS endpoint. ```hcl provider "postgresql" { scheme = "awspostgres" host = "test-instance.cvvrsv6scpgd.eu-central-1.rds.amazonaws.com" username = "postgres" port = 5432 password = "test1234" superuser = false } ``` -------------------------------- ### Import PostgreSQL Role Resource Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_role.html.markdown Imports an existing PostgreSQL role into Terraform state. The role name must match the resource name in Terraform configuration. ```bash $ terraform import postgresql_role.replication_role replication_name ``` -------------------------------- ### Azure PostgreSQL Flexible Server with Passwordless Authentication Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/index.html.markdown Configure the provider for Azure PostgreSQL Flexible Server using passwordless authentication via Azure AD. Requires setting 'azure_identity_auth' to true and providing 'azure_tenant_id'. ```hcl data "azurerm_client_config" "current" { } # https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/postgresql_flexible_server resource "azurerm_postgresql_flexible_server" "pgsql" { # ... authentication { active_directory_auth_enabled = true password_auth_enabled = false tenant_id = data.azurerm_client_config.current.tenant_id } } # https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/postgresql_flexible_server_active_directory_administrator resource "azurerm_postgresql_flexible_server_active_directory_administrator" "administrators" { object_id = "00000000-0000-0000-0000-000000000000" principal_name = "Azure AD Admin Group" principal_type = "Group" resource_group_name = var.rg_name server_name = azurerm_postgresql_flexible_server.pgsql.name tenant_id = data.azurerm_client_config.current.tenant_id } provider "postgresql" { host = azurerm_postgresql_flexible_server.pgsql.fqdn port = 5432 database = "postgres" username = azurerm_postgresql_flexible_server_active_directory_administrator.administrators.principal_name sslmode = "require" azure_identity_auth = true azure_tenant_id = data.azurerm_client_config.current.tenant_id } ``` -------------------------------- ### Define PostgreSQL Role Resource Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_role.html.markdown Defines a PostgreSQL role resource named 'replication_role' with the name 'replication_name' using the 'admindb' provider alias. ```hcl provider "postgresql" { alias = "admindb" } resource "postgresql_role" "replication_role" { provider = "postgresql.admindb" name = "replication_name" } ``` -------------------------------- ### Secure PostgreSQL Role with Write-Only Password Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_role.html.markdown This snippet illustrates creating a role using write-only password attributes (`password_wo` and `password_wo_version`) to prevent the password from being stored in the Terraform state file. ```hcl # Example using write-only password (password not stored in state) resource "postgresql_role" "secure_role" { name = "secure_role" login = true password_wo = "secure_password_123" password_wo_version = "1" } ``` -------------------------------- ### Ignoring Changes for Managed Roles Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_grant_role.html.markdown When managing a role with both `postgresql_role` and `postgresql_grant_role`, ignore changes to the `roles` attribute in the `postgresql_role` resource to prevent conflicts. ```hcl resource "postgresql_role" "bob" { name = "bob" lifecycle { ignore_changes = [ roles, ] } } resource "postgresql_grant_role" "bob_admin" { role = "bob" grant_role = "admin" } ``` -------------------------------- ### Grant SELECT Privileges on Tables Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_grant.html.markdown Grants SELECT privileges on multiple tables to a specific role within a database and schema. ```hcl resource "postgresql_grant" "readonly_tables" { database = "test_db" role = "test_role" schema = "public" object_type = "table" objects = ["table1", "table2"] privileges = ["SELECT"] } ``` -------------------------------- ### Inject PostgreSQL Password from AWS Secrets Manager Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/index.html.markdown Reference credentials from external secret stores like AWS Secrets Manager using Terraform data sources. The password is extracted from the secret string. ```hcl data "aws_secretsmanager_secret" "postgres_password" { name = "postgres_password" } data "aws_secretsmanager_secret_version" "postgres_password" { secret_id = data.aws_secretsmanager_secret.postgres_password.id } provider "postgresql" { [...] password = jsondecode(data.aws_secretsmanager_secret_version.postgres_password.secret_string)["password"] } ``` -------------------------------- ### Grant UPDATE and INSERT Privileges on Columns Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_grant.html.markdown Grants UPDATE and INSERT privileges on specific columns of a table to a role. This is useful for fine-grained access control. ```hcl resource "postgresql_grant" "read_insert_column" { database = "test_db" role = "test_role" schema = "public" object_type = "column" objects = ["table1"] columns = ["col1", "col2"] privileges = ["UPDATE", "INSERT"] } ``` -------------------------------- ### Revoke All Privileges on Schema for Public Role Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_grant.html.markdown Revokes all default privileges for the 'public' role on a specific schema. This is often used to reset or restrict default access. ```hcl resource "postgresql_grant" "revoke_public" { database = "test_db" role = "public" schema = "public" object_type = "schema" privileges = [] } ``` -------------------------------- ### Revoke Default Function Privileges Source: https://github.com/cyrilgdn/terraform-provider-postgresql/blob/main/website/docs/r/postgresql_default_privileges.html.markdown This snippet revokes all default privileges for functions for the 'public' role when new functions are created by 'object_owner' in a specified database. An empty privileges list effectively removes any previously granted default permissions. ```hcl resource "postgresql_default_privileges" "revoke_public" { database = postgresql_database.example_db.name role = "public" owner = "object_owner" object_type = "function" privileges = [] } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.