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

Issue connection on Azure with passwordless authentication #423

Open WilliamB17 opened 3 months ago

WilliamB17 commented 3 months ago

Hi,

I get an error when I try to connect to my database via passwordless authentication:

Error: Error connecting to PostgreSQL server psql-000.postgres.database.azure.com (scheme: postgres): pq: Service Principal oid mismatch for role[my_administrator_principal_name].

I use the latest provider version 1.22.0 and Terraform v1.7.5

data "azurerm_client_config" "current" {
}

resource "azurerm_postgresql_flexible_server" "pgsql" {
  # ...
  authentication {
    active_directory_auth_enabled = true
    password_auth_enabled         = true
    tenant_id                     = data.azurerm_client_config.current.tenant_id
  }
}

resource "azurerm_postgresql_flexible_server_active_directory_administrator" "administrators" {
  object_id           = var.azure_config.object_id
  principal_name      = "my_administrator_principal_name"
  principal_type      = "ServicePrincipal"
  resource_group_name = var.resource_group.name
  server_name         = azurerm_postgresql_flexible_server.pgsql.name
  tenant_id           = azurerm_client_config.current.tenant_id
}

provider "postgresql" {
  host                = var.azurerm_postgresql_flexible_server.fqdn
  port                = 5432
  database            = "postgres"
  username            = var.active_directory_administrator.principal_name
  sslmode             = "require"
  azure_identity_auth = true
  azure_tenant_id     = azurerm_client_config.current.tenant_id
}

However, I manage to connect with psql as described here : https://learn.microsoft.com/en-us/azure/postgresql/flexible-server/how-to-configure-sign-in-azure-ad-authentication