How to create a database in postgres managed service of azure using terraform?

Below is my terrafom file.


resource "azurerm_private_dns_zone" "example" {
  name                = "example.postgres.database.azure.com"
  resource_group_name = azurerm_resource_group.postgres_rg.name
}

resource "azurerm_private_dns_zone_virtual_network_link" "example" {
  name                  = "exampleVnetZone.com"
  private_dns_zone_name = azurerm_private_dns_zone.example.name
  virtual_network_id    = azurerm_virtual_network.aksvnet.id #var.virtual_network_id
  resource_group_name   = azurerm_resource_group.postgres_rg.name
}

resource "azurerm_postgresql_flexible_server" "example" {
  name                   = "${azurerm_resource_group.postgres_rg.name}-${var.environment}"
  resource_group_name    = azurerm_resource_group.postgres_rg.name
  location               = azurerm_resource_group.postgres_rg.location
  version                = "13"
  delegated_subnet_id    = azurerm_subnet.example.id
  private_dns_zone_id    = azurerm_private_dns_zone.example.id
  administrator_login    = var.administrator_login
  administrator_password = var.administrator_password
  zone                   = "1"

  storage_mb = 131072

  sku_name   = var.postgres_sku #"GP_Standard_D2s_v3"
  depends_on = [azurerm_private_dns_zone_virtual_network_link.example]

}

resource "azurerm_postgresql_database" "example" {
  name                = "mydatabase"
  resource_group_name = azurerm_resource_group.postgres_rg.name
  server_name         = azurerm_postgresql_flexible_server.example.name
  charset             = "UTF8"
  collation           = "English_United States.1252"
  depends_on          = [azurerm_postgresql_flexible_server.example]
}

When I ran this, I am getting error as below.

Error: creating/updating Database: (Name “mydatabase” / Server Name “testaks-dev-postgres-dev” / Resource Group “testaks-dev-postgres”): postgresql.DatabasesClient#CreateOrUpdate: Failure sending request: StatusCode=404 – Original Error: Code=“ResourceNotFound” Message=“The Resource ‘Microsoft.DBforPostgreSQL/servers/testaks-dev-postgres-dev’ under resource group ‘testaks-dev-postgres’ was not found. For more details please go to Resource not found errors - Azure Resource Manager | Microsoft Docs

│ with azurerm_postgresql_database.example,
│ on 12-postgres.tf line 33, in resource “azurerm_postgresql_database” “example”:
│ 33: resource “azurerm_postgresql_database” “example” {

But when I observe in the azure portal, I can see the flexible server is already created from this terraform file and getting this error at the database creation

Does it complete after running terraform apply twice?

no, it is trying to create postgres server again instead of using the existing one.

no it didn’t create even after apply second time