cyrilgdn / terraform-provider-postgresql

Terraform PostgreSQL provider
https://www.terraform.io/docs/providers/postgresql/
Mozilla Public License 2.0
355 stars 180 forks source link

Added a check during the `postgresql_extension` to prevent multiple resources from being created for the one extension #426

Open timtatt opened 3 months ago

timtatt commented 3 months ago

To address [#304] where users are implementing multiple extension resources per extension under the assumption one is required per schema. According to postgres documentation, schema is only the location to install the extensions objects.

Changes:

resource "postgresql_extension" "schema2" {
  name = "pg_stat_statements"
  schema = "schema2"
}

resource "postgresql_extension" "schema1" {
  name = "pg_stat_statements"
  schema = "schema1"
}

Prior: both resources created, each apply will ALTER the schema of the extension. Now: one resource will be created and the other will error eg.

Error: extension 'pg_stat_statements' already exists under schema 'schema1'

Projects which contain multiple resources for the same extension will still experience the same behaviour akin to importing a resource into two places. However, new resources created will be unable to do so if the extension exists. A note in the upgrade docs should mention to terraform state rm places where extensions have been duplicated