Skip to content
Visual explainer 3h to learn 5 steps

Azure SQL Database or Azure Database for PostgreSQL

Choose and connect a managed relational database with identity, pooling, high availability and point-in-time restore.

By Invicly Tech, , in Microsoft Azure Cloud Engineering. About 13 minutes to read.

Topic 15 of 33

In short

Azure SQL Database and Azure Database for PostgreSQL flexible server are managed relational databases: Azure patches them, backs them up and keeps standby copies, and you design schemas and queries. Choose by engine and skills: SQL Server and T-SQL for Azure SQL, open-source PostgreSQL and its extensions for the flexible server. Either way, connect with Microsoft Entra identities, pool connections, turn on high availability for production and know how point-in-time restore works before you need it.

Think of it like this: A managed database is a flat with a building manager: you arrange the rooms, they fix the plumbing and keep spare keys.

Explained simply

A relational database keeps data in tables with rows and columns and lets you join them with SQL. On Azure you do not have to install or patch the database server yourself. Azure SQL Database runs Microsoft's SQL Server engine as a service, and Azure Database for PostgreSQL flexible server runs open-source PostgreSQL as a service.

Choosing between them is mostly about the engine. If your team knows SQL Server, uses T-SQL or runs .NET apps that already target it, Azure SQL is the natural choice. If you want open-source PostgreSQL, its extensions, or the same database you run locally, choose the flexible server. Both scale up, back themselves up and can keep a standby copy.

Apps should sign in without a stored password. With Microsoft Entra authentication, the app's managed identity asks Entra ID for a short-lived token and uses it as the password. You create a database user for that identity and grant it only the permissions it needs.

Two habits keep databases healthy. First, reuse connections through a pool, because opening a connection is slow and servers allow only so many. Second, practise restore. Azure keeps backups automatically, and point-in-time restore can bring a database back to how it was, say, five minutes before a bad delete.

Cost note: both services are paid. Azure SQL Database has a free offer for a small number of serverless databases with a monthly allowance, and new Azure accounts get limited hours of a small PostgreSQL Burstable server. For a free local alternative, run PostgreSQL in Docker and do the same exercises against it.

Press Play on a diagram to walk through it one step at a time.

CompareAzure SQL Database or PostgreSQL flexible server
Azure SQL Database
  • SQL Server engine, T-SQL
  • vCore tiers: General Purpose, Business Critical, Hyperscale
  • Serverless option with auto-pause
  • Entra users: CREATE USER ... FROM EXTERNAL PROVIDER
  • PITR 1 to 35 days, default 7
PostgreSQL flexible server
  • Community PostgreSQL with extensions
  • Burstable, General Purpose, Memory Optimized
  • Built-in PgBouncer on port 6432
  • Entra users via pgaadauth_create_principal
  • Backups 7 to 35 days, restore to a new server
SequencePasswordless connection with a managed identity
App
Managed identity endpoint
Microsoft Entra ID
Database
  1. 1Request token for the database resource
    App to Managed identity endpoint: Request token for the database resource
  2. 2Prove the app's identity
    Managed identity endpoint to Microsoft Entra ID: Prove the app's identity
  3. 3Access token, valid about an hour
    Microsoft Entra ID to App: Access token, valid about an hour
  4. 4Connect over TLS, token as password
    App to Database: Connect over TLS, token as password
  5. 5Session as the mapped database user
    Database to App: Session as the mapped database user
CompareWhat protects you from what
High availability
  • Server or zone failure
  • Automatic failover
  • Minutes of downtime
  • Does not undo bad writes
Point-in-time restore
  • Bad delete or update, bad migration
  • Manual, creates a new database
  • Restore time grows with size
  • Within backup retention, up to 35 days
Geo-backup or geo-replica
  • Loss of a whole region
  • Restore or fail over to another region
  • May lose recent writes
  • Costs more storage or a second server

In depth

Choosing a service and tier

Azure SQL Database uses the vCore model, where you choose service tier, hardware and number of vCores, or the older DTU model with bundled sizes. General Purpose suits most apps. Business Critical uses local SSD and built-in replicas for low latency and fast failover. Hyperscale grows storage up to very large sizes and adds fast scaling and restores. The serverless compute option scales vCores automatically and can auto-pause when idle, which suits dev databases.

PostgreSQL flexible server has three compute tiers. Burstable is cheap and fine for dev and light apps but has no high availability or built-in PgBouncer. General Purpose suits most production apps. Memory Optimized suits large working sets. You choose a PostgreSQL major version and storage size; storage can grow but not shrink.

MapTiers at a glance
Managed relational on Azure
  • Azure SQL Database (vCore)
    • General Purpose
    • Business Critical
    • Hyperscale
    • Serverless compute option
  • PostgreSQL flexible server
    • Burstable: dev, no HA
    • General Purpose
    • Memory Optimized

Setting up Microsoft Entra authentication

Start by making an Entra user or group the database administrator. Then connect as that admin, using a token instead of a password, and create a database user for each app identity. In Azure SQL you run CREATE USER [id-api] FROM EXTERNAL PROVIDER and add it to roles such as db_datareader and db_datawriter. In PostgreSQL you connect to the postgres database and run SELECT * FROM pgaadauth_create_principal('id-api', false, false), then GRANT the privileges the app needs in its own database.

In code, DefaultAzureCredential finds the managed identity in Azure and your developer sign-in on your laptop. The token audience differs: https://database.windows.net/.default for Azure SQL and https://ossrdbms-aad.database.windows.net/.default for PostgreSQL. Once every client uses Entra ID, you can turn password authentication off.

  • Make an Entra group the database admin, not one person
  • One database user per app identity
  • Grant least privilege: read, write, no DDL for the app
  • Turn off password auth when nothing needs it

Transactions, pooling and retries

Transactions work as they do anywhere: group related writes, commit once, keep them short. What changes in the cloud is the connection. Opening a TLS connection and signing in takes tens of milliseconds, and every server has a maximum connection count that grows with its size. So each app instance keeps a pool, and the total is instances times pool size. With ten Container App replicas and a pool of 20, you need room for 200 connections.

PostgreSQL flexible server has PgBouncer built in on port 6432 for General Purpose and Memory Optimized tiers. By default it runs in transaction pooling mode, so session state such as SET commands or session-level advisory locks does not carry across transactions. Finally, expect brief disconnects during failover or maintenance. Use the driver's retry options or your own retry with backoff for transient errors, and make retried writes safe to repeat.

FlowFrom many app replicas to few server connections
  1. Step 1
    App replicas

    10 replicas, each with a pool of 20.

  2. Step 2
    PgBouncer on 6432

    Accepts up to 200 client connections.

  3. Step 3
    Server connections

    Shares about 40 real connections.

  4. Step 4
    PostgreSQL

    Stays well under max_connections.

High availability and failover

High availability keeps a standby ready so the database survives a server or zone failure. In PostgreSQL flexible server you enable it on General Purpose or Memory Optimized tiers, either zone-redundant, with the standby in another availability zone, or same-zone. Changes replicate synchronously, so a committed transaction is not lost, and failover usually completes in about one to two minutes. The server name stays the same; connections drop and must reconnect.

Azure SQL Database has high availability built in. Business Critical and Hyperscale keep replicas, and General Purpose can be made zone-redundant in supported regions. HA protects against infrastructure failure, not against a bad UPDATE, which replicates to the standby instantly. That is what point-in-time restore is for.

  • HA: survive server or zone failure, minutes of downtime
  • Standby is synchronous, no committed data lost
  • Apps must reconnect and retry
  • HA does not undo human or app errors

Backups and point-in-time restore

Both services back up automatically. Azure SQL takes full, differential and frequent log backups and keeps them for 1 to 35 days, 7 by default; long-term retention can keep weekly, monthly or yearly copies for years. PostgreSQL flexible server takes daily snapshots plus write-ahead log backups and keeps them for 7 to 35 days, with an optional geo-redundant backup chosen when the server is created.

Point-in-time restore never overwrites the live database. Azure SQL creates a new database on the same logical server; PostgreSQL creates a new server. You then either repoint the app at the restored copy or copy the lost rows back. Write the restore steps down and time a drill: restore duration grows with database size and log volume, and that time is part of your recovery time objective.

Learn it in 5 short steps

About 35m each. Track them with a timer in your plan.

  1. 1
    Compare Azure SQL and PostgreSQL~35m
  2. 2
    Connect with Entra authentication~35m
  3. 3
    Pool connections~35m
  4. 4
    Configure high availability~35m
  5. 5
    Restore to a point in time~35m

Key points

  • Both services handle patching, automated backups and high availability options; schema, indexes and queries stay yours.
  • Use Microsoft Entra authentication so the app's managed identity signs in with a short-lived token, not a stored password.
  • Reuse connections through a pool in the app or PgBouncer; opening a new connection per request wastes time and hits limits.
  • Zone-redundant high availability keeps a standby in another zone and fails over automatically, usually within a minute or two.
  • Point-in-time restore always creates a new database or server; you then repoint the app or copy data back.

Key terms

Tap a card to flip it. 0/6 known

Examples

python
from azure.identity import DefaultAzureCredential
import psycopg

SCOPE = "https://ossrdbms-aad.database.windows.net/.default"
token = DefaultAzureCredential().get_token(SCOPE).token

with psycopg.connect(
    host="pg-contoso-lab.postgres.database.azure.com",
    dbname="appdb", user="id-api", password=token,
    sslmode="require",
) as conn:
    print(conn.execute("select current_user, now()").fetchone())

The app's identity id-api signs in with an Entra token instead of a password.

A PostgreSQL server that only accepts Entra sign-in

Prerequisites: a lab resource group with a budget, your Entra object ID and a managed identity called id-api. Step 1: create a Burstable server with Entra auth on and password auth off, with you as admin. Step 2: get a token and connect with psql as yourself. Step 3: create the principal for id-api and grant it rights. Evidence: the app's connection prints current_user as id-api. Cleanup: delete the resource group. Free alternative: run postgres:16 in Docker and practise the GRANT part.

bash
az postgres flexible-server create -g rg-lab-data -n pg-contoso-lab -l westeurope \
  --tier Burstable --sku-name Standard_B1ms --version 16 \
  --microsoft-entra-auth Enabled --password-auth Disabled \
  --admin-object-id <your-object-id> --admin-display-name you@contoso.com --admin-type User \
  --public-access <your-ip>
az postgres flexible-server db create -g rg-lab-data -s pg-contoso-lab -d appdb
export PGPASSWORD=$(az account get-access-token \
  --resource-type oss-rdbms --query accessToken -o tsv)
psql "host=pg-contoso-lab.postgres.database.azure.com dbname=postgres user=you@contoso.com sslmode=require" \
  -c "select * from pgaadauth_create_principal('id-api', false, false);"
psql "host=pg-contoso-lab.postgres.database.azure.com dbname=appdb user=you@contoso.com sslmode=require" \
  -c "grant select, insert, update, delete on all tables in schema public to \"id-api\";"

Restore drill after a bad DELETE in Azure SQL

Scenario: at 09:14 UTC a script deleted all rows from orders. Step 1: note the time just before the mistake, 09:13. Step 2: restore the database to that time under a new name. Step 3: check row counts in the restored copy. Step 4: copy the missing rows back, or repoint the app if the whole database is wrong. Evidence: the restored database shows the expected count. Cleanup: drop the restored copy when done, because it is billed.

bash
az sql db restore -g rg-lab-data -s sql-contoso-lab -n appdb \
  --dest-name appdb-restore-0913 --time "2026-11-30T09:13:00Z"
az sql db show -g rg-lab-data -s sql-contoso-lab -n appdb-restore-0913 \
  --query "{status:status, created:creationDate}" -o table
# then in the restored database:
#   SELECT COUNT(*) FROM orders;
# copy rows back, verify, and finally:
az sql db delete -g rg-lab-data -s sql-contoso-lab -n appdb-restore-0913 --yes

Quick reference

  • Token audience: Azure SQL https://database.windows.net/.default
  • Token audience: PostgreSQL https://ossrdbms-aad.database.windows.net/.default
  • Azure SQL Entra user: CREATE USER [id-api] FROM EXTERNAL PROVIDER
  • PostgreSQL Entra user: SELECT * FROM pgaadauth_create_principal('id-api', false, false)
  • Built-in PgBouncer: port 6432, not on Burstable tier
  • Backup retention: 1 to 35 days (SQL) or 7 to 35 days (PostgreSQL); 7 is the default
  • Point-in-time restore always creates a new database or server
  • Total connections = app instances x pool size; size the server for it

Common mistakes

  • Caching one token forever. Entra tokens expire after about an hour; get a fresh one when the pool opens new connections.
  • Expecting restore to roll back the live database. It always creates a new server or database alongside the old one.
  • Sizing max connections for each app instance separately. Ten instances with a pool of 20 means 200 connections.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1An engineer runs UPDATE without WHERE on a zone-redundant database. What recovers the data?

Question 2

2Twelve replicas each keep a pool of 25 connections. How many connections must the database accept?

Question 3

3Which PostgreSQL compute tier does not support high availability?

Question 4

4What does an app use as the password with Entra authentication?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Show the tier, version, HA mode and backup retention of your PostgreSQL flexible server.

  2. Exercise 2medium

    Turn on the built-in PgBouncer for a General Purpose server and connect through it.

  3. Exercise 3hard

    Run a timed PostgreSQL restore drill: create a table, insert rows, note the time, delete the rows, restore to the noted time, and record how long the restore took.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Should I choose Azure SQL Database or PostgreSQL?
Pick the engine your team and app already know. Both are mature managed services; the bigger costs come from rewriting queries and retraining people.
Does high availability mean I do not need backups?
No. HA copies every change to the standby, including mistakes. Backups and point-in-time restore are what undo bad writes.
Why do I get too many connections errors when I scale out?
Every new app instance opens its own pool. Lower the pool size, add PgBouncer, or use a larger server with a higher connection limit.
Can I use Entra ID on my laptop to connect?
Yes. Sign in with az login, get a token with az account get-access-token, and use it as the password; DefaultAzureCredential does the same in code.

Go deeper

ShareXLinkedInWhatsApp