How can I use the OOTB admin account in Azure Postgres Flex Server to create a new database?

Jack Roman 0 Reputation points
2025-06-04T20:22:32.1166667+00:00

I am struggling to create a basic deployment pipeline for an Azure PG Flex server. I use Bicep to provision the server the way I want. But I can't find any way to automate the creation of a database within the server. If I try to use the admin user specified in resource creation, I can't even use SQL to create a database - I get 'permission denied' errors. If I try to use the Bicep pqsql database child resource template, I get 'unable to find parent resource' (even though I have verified the parent reference syntax is correct).

Is there a straightforward way to do this? I feel like I'm missing something.

Azure Database for PostgreSQL
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Mahesh Kurva 4,860 Reputation points Microsoft External Staff Moderator
    2025-06-05T00:14:58.1766667+00:00

    Hi Jack Roman,

    Greetings!

    It sounds like you're encountering some common issues with permissions and resource referencing in Azure PostgreSQL Flexible Server deployments using Bicep.

    Here are a few steps to help you automate the creation of a database within the server.

    1.You can use Bicep to create the database as a child resource of the PostgreSQL server. Here's a simplified example of how you can define the server and the database in your Bicep file:

    param administratorLogin string
    @secure()
    param administratorLoginPassword string
    param ___location string = resourceGroup().___location
    param serverName string
    param databaseName string
    resource server 'Microsoft.DBforPostgreSQL/flexibleServers@2021-06-01' = {
      name: serverName
      ___location: ___location
      properties: {
        administratorLogin: administratorLogin
        administratorLoginPassword: administratorLoginPassword
        version: '12'
      }
    }
    resource database 'Microsoft.DBforPostgreSQL/flexibleServers/databases@2021-06-01' = {
      parent: server
      name: databaseName
      properties: {
        charset: 'UTF8'
        collation: 'en_US.UTF8'
      }
    }
    

    2.Deploy the Bicep file using Azure CLI to ensure the resources are created correctly:

    az group create --name exampleRG --___location centralus
    az deployment group create --resource-group exampleRG --template-file main.bicep
    
    

    3.Sometimes, creating the resources manually in the Azure Portal can help identify any missing configurations or permissions.

    For more information, please refer the document:
    https://learn.microsoft.com/en-us/azure/postgresql/flexible-server/quickstart-create-server-bicep?tabs=CLI

    You can automatically deploy your database updates to Azure Database for PostgreSQL flexible server after every successful build with Azure Pipelines. You can use Azure CLI task to update the database either with a SQL file or an inline SQL script against the database.

    For more information, please refer the document:

    https://learn.microsoft.com/en-us/azure/postgresql/flexible-server/azure-pipelines-deploy-database-task

    Hope this helps. Do let us know if you any further queries.

    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    0 comments No comments

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.