Shut down Azure PostgreSQL without Azure restarting it automatically

Pablo Schor 0 Reputation points
2025-03-10T21:58:24.8033333+00:00

I created an Azure PostgreSQL database and when I shut it down I don't want it to restart automatically, how to fix that? This is what I get every time I shut down the database:

posgres 7 days

Azure Database for PostgreSQL
{count} votes

1 answer

Sort by: Most helpful
  1. Prasad Chaganti 770 Reputation points Microsoft External Staff Moderator
    2025-03-11T19:19:50.76+00:00

    Hi Pablo Schor,

    I understand that you are using an Azure Database for PostgreSQL – Flexible Server. It states that the server will automatically restart after 7 days if not manually started before then.

    You can create an Azure Automation runbook or use Azure CLI to automatically stop the server every 7 days:

    az postgres flexible-server stop --name <server-name> --resource-group <resource-group>

    If you want to reduce costs instead of stopping it, scale the server down to the smallest size.

    If you don't need the database, you can back up the data and delete the server to stop billing entirely.

    Please follow the below steps to Create an Azure Automation Runbook to Stop the Server Automatically:

    To automate stopping your Azure PostgreSQL Flexible Server every 7 days (or on a custom schedule), you can create an Azure Automation Runbook using the Azure CLI or PowerShell.

    1.Need to create an Automation Account:

    Open the Azure Portal and search for Automation Accounts.

    Click the Create and fill the details

    Subscription, Resource Group, Region, Name(StopPostgresRunbook)

    2.Create a Runbook

    Go to your Automation Account → Runbooks → Create a Runbook.

    please set the following details

    Name: StopPostgresFlexibleServer

    Runbook Type: PowerShell

    Description: "Stops PostgreSQL Flexible Server every 7 days."

    3.Please follow the PowerShell script into the runbook:

    param (
        [string]$ResourceGroupName = "<your-resource-group>",
        [string]$ServerName = "<your-server-name>"
    )
    # Connect to Azure
    Connect-AzAccount -Identity
    # Stop the PostgreSQL Flexible Server
    Stop-AzPostgreSqlFlexibleServer -ResourceGroupName $ResourceGroupName -Name $ServerName
    Write-Output "Stopped PostgreSQL server $ServerName in resource group $ResourceGroupName"
    

    4.Save the script → Publish the runbook.

    5.Create a Schedule to Run Every 7 Days

    Go to the Runbook → Schedules → Add a Schedule.

    Please Set the following:

    Frequency: Recurring

    Every: 7 days

    Start Time: Set your preferred start time

    6.Assign Permissions

    Go to Automation Account → Identity → System Assigned → Turn it ON.

    Go to Azure Role Assignments → Add role assignment

    Role: Contributor

    Scope: Resource Group

    This will automatically stop the PostgreSQL Flexible Server every 7 days. If you want to modify the schedule or settings, you can adjust it in the Automation Account.

    I hope this information helps. Please do let us know if you have 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.


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.