惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Cloudflare Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
C
Check Point Blog
有赞技术团队
有赞技术团队
H
Help Net Security
V
Vulnerabilities – Threatpost
N
News | PayPal Newsroom
Hacker News - Newest:
Hacker News - Newest: "LLM"
L
LINUX DO - 最新话题
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 【当耐特】
爱范儿
爱范儿
I
InfoQ
V
Visual Studio Blog
O
OpenAI News
Google DeepMind News
Google DeepMind News
S
Security Affairs
T
Troy Hunt's Blog
P
Palo Alto Networks Blog
Spread Privacy
Spread Privacy
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
N
Netflix TechBlog - Medium
Latest news
Latest news
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Webroot Blog
Webroot Blog
S
Schneier on Security
MongoDB | Blog
MongoDB | Blog
T
Tor Project blog
V2EX - 技术
V2EX - 技术
Security Latest
Security Latest
Cloudbric
Cloudbric
The GitHub Blog
The GitHub Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
B
Blog RSS Feed
C
CERT Recently Published Vulnerability Notes
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
S
Securelist
C
Cisco Blogs
博客园_首页
TaoSecurity Blog
TaoSecurity Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Security Archives - TechRepublic
Security Archives - TechRepublic
P
Proofpoint News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Threat Research - Cisco Blogs
阮一峰的网络日志
阮一峰的网络日志
S
Secure Thoughts

TerminalBytes

Reverse Engineering the iPod Classic Sanctuary: the browser-based Kindle jailbreak | TerminalBytes Steam Machine alternatives: mini PCs that game for less | TerminalBytes Oracle Cloud free tier 2026: 4 OCPU/24GB cut to 2 OCPU/12GB | TerminalBytes Run Gemma 4 on a mini PC, no GPU required My Arduino spins faster when Claude burns more tokens Kindle dashboard: 3 ways to build one in 2026 Best mini PC for local LLMs in 2026 (Strix Halo era) You Don't Need a Mac Mini to Run OpenClaw The Self-Hosting Revolution Powered by Mini PCs SoundLeaf: Beautiful iOS Client App for Audiobookshelf My iPhone 8 Refuses to Die: Now It's a Solar-Powered Vision OCR Server Homelab: Hosting multiple Game Servers on a Single Mini PC Reviving an Old Kindle Paperwhite 7th Gen The Ultimate Guide to Running a Minecraft Server on a Mini PC Best Mini PCs for Home Lab 2025: NUC vs Beelink vs ThinkCentre How to Create Your Own Free VPN Server Using Oracle Cloud (2025 Guide) Set Up a Free Minecraft PE Server Using Oracle Cloud in 2025 Kubernetes at Home - From Docker Compose to K3s Prompt Engineering for ChatGPT and GPT-4 (Practical Guide) Create Your Own Minecraft PE Server for Free Scalable Node.js Backend Without Express, Koa, or Hapi How to Use GeForce Now in India (VPN and Cloud Workarounds) Azure AD as an Identity Provider for AWS Cognito Free WordPress Blog with Custom Domain on Google Cloud sshuttle - A Free VPN Over SSH for Cheap, Private Browsing Raspberry Pi Temperature and Humidity Monitor with Grafana Raspberry Pi Setup Without a Monitor, Keyboard, or Mouse 6 Common Linux Commands for System Monitoring Basic Linux Commands for File Manipulation and Compression Basic Linux Commands For Text Manipulation Top 10 Linux Commands Of All Time
Setting Up a Robust PostgreSQL High-Availability Cluster on Azure
Hemant Kumar · 2025-01-30 · via TerminalBytes
On this page

Setting Up a Robust PostgreSQL High-Availability Cluster on Azure

Are you looking to deploy a production-ready PostgreSQL cluster on Azure? This guide will walk you through setting up a highly available PostgreSQL environment. I’ve spent countless hours testing these configurations, and I’m excited to share what actually works in the real world.

Understanding Your Options

When setting up PostgreSQL on Azure, you have several deployment options:

Production Features

  • High availability with automatic failover
  • Zone-redundant or same-zone replicas
  • Advanced monitoring and alerting
  • Flexible compute resources
  • Comprehensive storage options

1. Getting Started with Azure PostgreSQL

Let’s start with the basic setup. Later sections will cover scaling to HA when you’re ready.

Before we dive into the nitty-gritty of setting up our cluster, let’s understand what we’re working with. With the right configuration, you can build a robust production setup.

What Azure Offers for PostgreSQL

Azure provides several deployment options for PostgreSQL. We’ll be working with Azure Database for PostgreSQL - Flexible Server, which gives us:

  • Flexible compute resources with configurable vCores and memory
  • Scalable storage for production workloads
  • Configurable number of connections
  • Comprehensive backup options

Prerequisites You’ll Need

Before we start, make sure you have:

  1. An active Azure account
  2. Azure CLI installed on your machine
  3. Basic familiarity with PostgreSQL
  4. A text editor for configuration files

Pro tip: Set up Azure CLI authentication early - it’ll save you tons of headaches later!

2. Planning Your High-Availability Architecture

HA Cluster Architecture

This is where things get interesting. In my experience, the key to a successful HA setup is making smart architectural choices.

Important: High Availability with zone-redundant deployment is only available in General Purpose and Memory Optimized tiers, and only in regions with availability zone support. The standby server is automatically deployed in a different availability zone.

Network Planning

Here’s a practical network setup that I’ve found works well:

# Create your virtual network
az network vnet create \
    --resource-group postgresql-ha-rg \
    --name postgresql-ha-vnet \
    --address-prefix 10.0.0.0/16

# Create a subnet for your PostgreSQL servers
az network vnet subnet create \
    --resource-group postgresql-ha-rg \
    --vnet-name postgresql-ha-vnet \
    --name postgresql-subnet \
    --address-prefix 10.0.1.0/24

3. Step-by-Step Cluster Deployment

Now for the fun part - actually setting up our cluster! I’ll share some battle-tested commands that have worked reliably for me.

Creating the Primary Instance

az postgres flexible-server create \
    --resource-group postgresql-ha-rg \
    --name primary-pg-server \
    --location eastus \
    --admin-user adminuser \
    --admin-password <your-secure-password> \
    --sku-name Standard_B1ms \
    --tier Burstable \
    --version 14 \
    --high-availability Enabled \
    --zone 1

Configuring Replication

Here’s where many tutorials fall short. You need to set these critical parameters:

-- On primary server
ALTER SYSTEM SET wal_level = replica;
ALTER SYSTEM SET max_wal_senders = 10;
ALTER SYSTEM SET max_replication_slots = 10;

Pro tip: Don’t forget to restart your server after these changes!

4. Optimizing Your PostgreSQL Cluster

Performance Tuning

Here are some optimal settings I’ve discovered through trial and error:

-- Memory settings (adjust based on your service tier)
effective_cache_size = '75%' of available RAM
work_mem = '16MB'         -- Start conservative, adjust based on workload
maintenance_work_mem = '256MB'

-- Connection settings
max_connections = 100     -- Varies by tier:
                         -- Basic: up to 50
                         -- General Purpose: up to 5000
                         -- Memory Optimized: up to 7500

Important: Many parameters like shared_buffers are managed automatically by Azure and cannot be modified. Focus on application-level parameters that you can control.

Monitoring Setup

I can’t stress this enough - monitoring is crucial! Set up Azure Monitor with these key metrics:

Note: Some monitoring features are only available in General Purpose and Memory Optimized tiers. Basic tier users will have access to fundamental metrics only.

  1. CPU utilization
  2. Memory usage
  3. Connection count
  4. Replication lag
  5. Transaction logs generation rate

Here’s a quick Azure CLI command to set up basic monitoring:

az monitor metrics alert create \
    --name "high-cpu-alert" \
    --resource-group postgresql-ha-rg \
    --scopes "/subscriptions/<your-sub-id>/resourceGroups/postgresql-ha-rg/providers/Microsoft.DBforPostgreSQL/flexibleServers/primary-pg-server" \
    --condition "avg cpu_percent > 80" \
    --window-size 5m \
    --evaluation-frequency 1m

Advanced Monitoring (Paid Tier)

Additional monitoring features in paid tiers:

  • Query performance insights
  • Replication lag monitoring
  • Advanced metrics and logging
  • Custom dashboard creation

5. Testing and Validating High Availability

This is where the rubber meets the road. Here’s my testing checklist:

  1. Failover Testing
# Trigger a manual failover
az postgres flexible-server failover \
    --name primary-pg-server \
    --resource-group postgresql-ha-rg
  1. Connection Testing
import psycopg2
import time

def test_connection():
    while True:
        try:
            conn = psycopg2.connect(
                "host=primary-pg-server.postgres.database.azure.com " +
                "dbname=postgres user=adminuser password=<your-password>"
            )
            print("Connected successfully!")
            conn.close()
            break
        except psycopg2.Error as e:
            print("Connection failed:", e)
            time.sleep(1)

6. Maintaining Your Cluster

Regular maintenance is key to long-term stability. Here’s my monthly maintenance checklist:

  1. Check and apply PostgreSQL updates
  2. Review and optimize slow queries
  3. Validate backup integrity
  4. Test failover procedures
  5. Clean up old logs and temporary files

Automation Script

Here’s a helpful maintenance script I use:

#!/bin/bash

# Check PostgreSQL version
current_version=$(az postgres flexible-server show \
    --resource-group postgresql-ha-rg \
    --name primary-pg-server \
    --query version -o tsv)

# Check available updates
available_updates=$(az postgres flexible-server list-versions \
    --location eastus \
    --query "[?version > '$current_version']")

# Backup validation
az postgres flexible-server backup list \
    --resource-group postgresql-ha-rg \
    --server-name primary-pg-server

7. Real-World Performance Tips

After running several clusters in production, here are my top tips:

  1. Connection Pooling

    • Use PgBouncer for connection pooling
    • Set max_connections conservatively
    • Monitor connection usage patterns
  2. Query Optimization

    • Regularly review and update statistics
    • Use appropriate indexes
    • Implement query caching where possible
  3. Resource Management

-- Set statement timeout to prevent long-running queries
SET statement_timeout = '30s';

-- Enable query plan caching
SET plan_cache_mode = 'force_custom_plan';

Cost Management

It’s essential to implement good cost monitoring practices:

  1. Keep an eye on backup storage usage
    • Default backup retention is 7 days
    • Additional backup storage is billed separately
  2. Monitor data transfer costs
    • Ingress is free
    • Egress is charged based on zones and regions
  3. Set up cost alerts
  4. Regularly review resource utilization
  5. Consider reserved capacity for long-term workloads
    • Can save up to 80% compared to pay-as-you-go pricing
  6. Monitor IOPS usage
    • Exceeding included IOPS results in additional charges

Wrapping Up

We’ve covered how to set up a robust PostgreSQL environment on Azure, from basic configuration to production-ready features. Understanding the available tiers and their capabilities will help you make informed decisions as your needs evolve.

Key takeaways:

  1. Start with appropriate sizing for your workload
  2. High availability requires General Purpose or Memory Optimized tiers
  3. Plan your architecture with scaling in mind
  4. Implement comprehensive monitoring from the start
  5. Choose the right tier based on your performance and availability requirements

Have you set up PostgreSQL on Azure? I’d love to hear about your experiences and any tips you’ve discovered.

Happy developing! 🚀

Last updated: February 2025