cyrilgdn / terraform-provider-postgresql

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

Import for postgresql_server and postgresql_user_mapping #329

Closed cyn-rus closed 4 months ago

cyn-rus commented 11 months ago

Terraform Version

1.5.2

Affected Resource(s)

Terraform Configuration Files

provider "postgresql" {
  host            = #host
  port            = #port
  database        = "warehouse"
  username        = "postgres"
  password        = #password
  superuser       = false
  connect_timeout = 15
  sslmode         = "disable"

  expected_version = "14.8"
}

resource "postgresql_server" "warehouse_server" {
  server_name  = "server"
  fdw_name     = "postgres_fdw"
  server_owner = "server"

  options = {
    host   = #host
    dbname = #db
    port   = #port
  }
}

resource "postgresql_user_mapping" "warehouse_server_dashboard" {
  server_name = "server"
  user_name   = "dashboard"
  options = {
    user     = #user
    password = #password 
  }
}

Expected Behavior

Resources imported successfully

Actual Behavior

Resources are unable to be imported with message Error: Cannot import non-existent remote object

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. Create server and user mapping manually
  2. Create resources for server and user mapping
  3. Import both server and user mapping

Important Factoids

New functions needed for importing

lancedikson commented 5 months ago

I've just stumbled upon a similar issue. @cyn-rus, did you manage to figure out how the ID is formed? ~It seems it's not that straightforward and might be something like database_name.server_name or fdw_name.server_name (though I tried these, and they didn't work either).~ I've created a new postgresql_server and the id seems to be equal to its name 🤔

lancedikson commented 5 months ago

Ok, a workaround I've applied is creating a new resource (with a new name, but similar params), and then editing the state file to match the name and id with the existing resource. After that I deleted the newly created resource, edited the terragrunt.hcl to have correct resource naming and tg plan now suggests no changes.

Though a working import functionality would be very much appreciated!

cyrilgdn commented 4 months ago

Hi @lancedikson @cyn-rus ,

You cannot import by name directly for the server?

terraform import  postgresql_server.warehouse_server server

For the user mapping it should be with:

terraform import postgresql_user_mapping.warehouse_server_dashboard dashboard.server

I'll add this to the documentation, meanwhile I close this issue but don't hesitate to open it back if the import doesn't work

henworth commented 4 weeks ago

Unfortunately importing via name doesn't seem to work. I get the dreaded Error: Cannot import non-existent remote object.