cyrilgdn / terraform-provider-postgresql

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

Feature Request: Flag to disable capabilities check during plan #268

Open Cakasim opened 1 year ago

Cakasim commented 1 year ago

Hi there,

we are using this provider to initialize an AWS RDS database after creation. Recently we needed to rename a database. This action invalidates the connection details of the aws_db_instance resource and the postgresql fails during plan because it tries to connect to the database using those invalid credentials. The credentials will change and be valid again after the apply but we don't get that far.

Terraform Version

Terraform v1.2.6 on linux_amd64

Terraform Configuration Files

resource "aws_db_instance" "abc" {
  db_name = "<changed>"
  # ...
}

provider "postgresql" {
  host            = aws_db_instance.abc.address
  port            = aws_db_instance.abc.port
  database        = aws_db_instance.abc.name
  username        = aws_db_instance.abc.username
  password        = aws_db_instance.abc.password
  superuser       = false
  connect_timeout = 15
}

Actual Behavior

Changing certain attributes like aws_db_instance.db_name invalidates the RDS instance resource and the exported attributes become invalid, i.e. the attributes do not refer to a valid database connection. The postgresql provider uses the invalid database credentials during plan to check capabilities and fails.

Possible Solution

The postgresql provider supports a flag (e.g. skip_capabilities_check = true) that instructs the provider to not connect to the database during plan phase. After the RDS instance has been changed, the provider can use valid connection details.

So basically whenever there is a change (or potentially other problem with the database) that makes the connection details invalid, there should be a way to ignore this during the plan and get to the actual apply to fix this.

Steps to Reproduce

  1. Create a setup like mentioned in the Terraform code above
  2. Invalidate the database connection details (for example, delete the database outside of TF)
  3. Run the plan
  4. It should fail because the provider attempts to connect to this database during plan. Usually an apply would fix the problem because it recreates the database.

References