Cannot delete an instance of module in Terraform which contains a provider
I think the only solution is a two-step solution, but I think it's still clean enough.What I would do is have two files per database (name them how you want).
db-1-infra.tfdb-1-pgsql.tf
Put everything except your postgres resources in db-1-infra.tf
resource "azurerm_postgresql_server" "postgresserver" { name = "${var.db_name}-server" location = "${var.location}" resource_group_name = "${var.resource_group}" sku = ["${var.vmSize}"] storage_profile = ["${var.storage}"] administrator_login = "psqladminun" administrator_login_password = "${random_string.db-password.result}" version = "${var.postgres_version}" ssl_enforcement = "Disabled"}provider "postgresql" { version = "0.1.0" host = "${azurerm_postgresql_server.postgresserver.fqdn}" port = 5432 database = "postgres" username = "${azurerm_postgresql_server.postgresserver.administrator_login}@${azurerm_postgresql_server.postgresserver.name}". password = "${azurerm_postgresql_server.postgresserver.administrator_login_password}" }resource "azurerm_postgresql_database" "db" { name = "${var.db_name}" resource_group_name = "${var.resource_group}" server_name = "${azurerm_postgresql_server.postgresserver.name}" charset = "UTF8" collation = "English_United States.1252"}
Put your PostgreSQL resources in db-1-pgsql.tf
resource "postgresql_role" "role" { name = "${random_string.user.result}" login = true connection_limit = 100 password = "${random_string.pass.result}" create_role = true create_database = true depends_on = ["azurerm_postgresql_database.db"]
}
When you want to get rid of your database, first delete the file db-1-pgsql.tf and apply. Next, delete db-1-infra.tf and apply again.
The first step will destroy all postgres resources and free you up to run the second step, which will remove the postgres provider for that database.