Unable to create Azure Database for Postgres Flexible Server using terraform script due to Internal Server Error.

Shubham Kumar 25 Reputation points
2025-03-07T08:50:48.3566667+00:00

Hello everyone, I'm unable to create an azurerm_postgresql_flexible_server resource using terrafrom script. I'm getting following error:

user@host:~/infra$ terraform apply tfplan.terraform

azurerm_postgresql_flexible_server.postgressrv: Creating...

azurerm_postgresql_flexible_server.postgressrv: Still creating... [10s elapsed]

azurerm_postgresql_flexible_server.postgressrv: Still creating... [20s elapsed]

azurerm_postgresql_flexible_server.postgressrv: Still creating... [30s elapsed]

azurerm_postgresql_flexible_server.postgressrv: Still creating... [40s elapsed]

azurerm_postgresql_flexible_server.postgressrv: Still creating... [50s elapsed]

azurerm_postgresql_flexible_server.postgressrv: Still creating... [1m0s elapsed]

│ Error: creating Flexible Server (Subscription: "<mysubid>"

│ Resource Group Name: "resgroup-test-postgressrv"

│ Flexible Server Name: "postgres-common-test"): polling after Create: polling failed: the Azure API returned the following error:

│ Status: "InternalServerError"

│ Code: ""

│ Message: "An unexpected error occured while processing the request. Tracking ID: '<some_tracking_id>'"

│ Activity Id: ""

│ ---

│ API Response:

│ ----[start]----

│ {"name":"<some_activity_id>","status":"Failed","startTime":"2025-03-07T08:42:00.087Z","error":{"code":"InternalServerError","message":"An unexpected error occured while processing the request. Tracking ID: '<some_tracking_id>'"}}

│ -----[end]-----

│ with azurerm_postgresql_flexible_server.postgressrv,

│ on main.tf line 6, in resource "azurerm_postgresql_flexible_server" "postgressrv":

│ 6: resource "azurerm_postgresql_flexible_server" "postgressrv" {

Here's my script:
main.tf

resource "azurerm_resource_group" "resource_group" {
  name     = local.resource_group_name
  location = var.location
}

resource "azurerm_postgresql_flexible_server" "postgressrv" {
  name                          = local.hostname
  resource_group_name           = azurerm_resource_group.resource_group.name
  location                      = azurerm_resource_group.resource_group.location
  version                       = var.pg_version
  administrator_login           = var.username
  administrator_password        = var.password
  zone                          = 3
  storage_mb                    = 32768
  storage_tier                  = "P4"
  sku_name                      = var.sku_name
  backup_retention_days         = 7
  geo_redundant_backup_enabled  = false
  public_network_access_enabled = true
  auto_grow_enabled = true

  authentication {
    password_auth_enabled = true
  }

  tags = {
    env     = var.env
    purpose = "airflow"
  }

  depends_on = [azurerm_resource_group.resource_group]
}


variables.tf

variable "env" {
  default = "test"
}

variable "pg_version" {
  default = "16"
}

variable "location" {
  default = "northeurope"
}

variable "sku_name" {
  default = "B_Standard_B1ms"
}

variable "username" {
  default = "mytestpgadmin"
}

variable "password" {
  default = "H4sh1C0rp!!"
}

providers.tf

terraform {
  required_version = ">=1.0"
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~>3.0"
    }
  }
}

provider "azurerm" {
  features {}
}


I have already tried removing or changing the zone from 3 to 2 or even 1. I've used Azure Portal to create the same resource, and I was able to create it with same configuration using Azure Portal. What am I doing wrong here, I've prepared this script by following the examples, given at https://registry.terraform.io/providers/tfproviders/azurerm/latest/docs/resources/postgresql_flexible_server
and
https://learn.microsoft.com/en-us/azure/developer/terraform/deploy-postgresql-flexible-server-database?tabs=azure-cli

Azure Database for PostgreSQL
{count} votes

Accepted answer
  1. Oury Ba-MSFT 20,911 Reputation points Microsoft Employee Moderator
    2025-03-20T17:39:10.0166667+00:00

    Shubham Kumar (iDEAS-Group)

    Thank you for reaching out.

    I am sorry to hear about the issue you are facing.

    There's a known issue where when a non-unique name is created in a different resource group, subscription or tenant, than the already existing one, the deployment fails with an Internal Server Error. Our team is actively working on it. As a workaround we would suggest choosing a different until this is completely fixed.

    Regards,

    Oury 


1 additional answer

Sort by: Most helpful
  1. PratikLad 1,825 Reputation points Microsoft External Staff Moderator
    2025-03-13T09:24:54.7233333+00:00

    As we discussed in comment you are able to deploy the Azure Database for Postgres Flexible Server when you change the name.

    I tried to run the example and it worked with the name "example-psqlflexibleserver-shubham"

    The error you are encountering is due to a previously created resource with the same name that was deleted. When you attempt to create a new resource with the same name, the cached data from the earlier resource is causing the issue.

    The name which you are using is postgres-common-test is most common name.

    • To resolve this you can use, the resource random_string that generates a random permutation of alphanumeric characters and optionally special characters as below example:
    
    resource "random_string" "server_name_suffix" {
    
    length = 6
    
    special = false
    
    upper = false
    
    numeric = true
    
    }
    
    

    My terraform code:

        terraform {
      required_version = ">=1.0"
      required_providers {
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "~>3.0"
        }
      }
    }
    
    provider "azurerm" {
      features {}
    }
    
    variable "env" {
      default = "test"
    }
    
    variable "location" {
      default = "northeurope"
    }
    
    variable "pg_version" {
      default = "16"
    }
    
    variable "sku_name" {
      default = "B_Standard_B1ms"
    }
    
    variable "admin_username" {
      default = "pgadmin"
    }
    
    variable "admin_password" {
      default = "H4sh1C0rp!!"
    }
    
    variable "allowed_ip_range" {
      default = ""
    }
    
    resource "azurerm_resource_group" "rg" {
      name     = "rg-${var.env}-postgres"
      location = var.location
    }
    
    resource "random_string" "server_name_suffix" {
      length  = 6
      special = false
      upper   = false
      numeric = true
    }
    
    resource "azurerm_postgresql_flexible_server" "postgres" {
      name                          = "psqlflexible-${random_string.server_name_suffix.result}"
      resource_group_name           = azurerm_resource_group.rg.name
      location                      = azurerm_resource_group.rg.location
      version                       = var.pg_version
      administrator_login           = var.admin_username
      administrator_password        = var.admin_password
      storage_mb                    = 32768
      storage_tier                  = "P4"
      sku_name                      = var.sku_name
      geo_redundant_backup_enabled  = false
      public_network_access_enabled = true
      auto_grow_enabled             = true
      zone                   = "1"
    
      authentication {
        password_auth_enabled = true
      }
      tags = {
        environment = var.env
      }
    }
    
    resource "azurerm_postgresql_flexible_server_database" "database" {
      name      = "mytestdb"
      server_id = azurerm_postgresql_flexible_server.postgres.id
      collation = "en_US.utf8"
      charset   = "UTF8"
    }
     
    resource "azurerm_postgresql_flexible_server_firewall_rule" "allow_range" {
      name      = "allow-ip-range"
      server_id = azurerm_postgresql_flexible_server.postgres.id
      start_ip_address = var.allowed_ip_range
      end_ip_address   = "your IP" 
    }
    

    Here is the above terraform code deployed the resource with the name where it contains random characters.enter image description here

    Updated:

    The Azure Database for Postgres Flexible Server name you are trying to use should be globally unique across Azure. Azure Database for Postgres Flexible Server names must be unique not only within your subscription but also across all Azure tenants.

    So even if the previous Azure Database for Postgres Flexible server was deleted in tenant #1, it might still be reserved across Azure.

    You can also try the following steps and see if that helps.

    • Clear “Cookies and Cached data” of your browser.
    • Private Mode (New InPrivate Window).     
    • Try in different browser.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.