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.