🐘
Module 5 of 9 25–30% 3 sub-modules Β· 27 units Domain 2: Develop AI Solutions by Using Azure Data Management Services

Develop AI Solutions with Azure Database for PostgreSQL

Connect and query PostgreSQL Flexible Server with SDKs. Model schemas and implement indexing strategies. Configure resources for vector workloads. Run vector similarity search and implement RAG patterns.

Azure Database for PostgreSQLpgvectorHNSWIVFFlat

Last updated: Β· Aligned with Course AI-200T00-A

Module

Build and Query with Azure Database for PostgreSQL

units
🎬 Unit 1

Introduction

3 min

Azure Database for PostgreSQL is a fully managed PostgreSQL service β€” Microsoft handles patching, backups, HA, and connection pooling. You get full PostgreSQL compatibility including JSONB, extensions (pgvector for vector search), and transactional guarantees. Perfect for AI agents storing conversation history, task state, and structured context alongside vector embeddings.

πŸ’‘ Exam Tip
Exam pillars: 1) Compute tiers + PgBouncer availability 2) Entra auth token flow 3) TLS modes (verify-full) 4) JSONB and AI-relevant data types 5) Transactional DDL (ALTER TABLE in BEGIN/COMMIT).
πŸ“˜ Unit 2

Explore Azure Database for PostgreSQL

7 min

PostgreSQL Connection Architecture: App β†’ Entra token β†’ PgBouncer (port 6432) β†’ PostgreSQL

AI ApplicationPython / psycopg2Entra IDOAuth2 tokenport 6432PgBouncerConnection PoolGP/MO tiers onlyport 5432PostgreSQLAzure ManagedTLS verify-fullJSONB Β· pgvectorCompute TiersBurstable (dev)General Purpose βœ“Memory Optimized βœ“PgBouncer: GP + MOAuth: DefaultAzureCredential β†’ token β†’ password field (Entra) or username/password (native)

1. Compute Tiers

#TierVM SeriesCPU BurstPgBouncerUse For
1BurstableB-seriesYes❌ NoDev/test, small apps, proof-of-concept
2General PurposeD-seriesNoβœ… YesProduction APIs, steady workloads
3Memory OptimizedE-seriesNoβœ… YesComplex queries, large AI working sets, caching
⚠️ Common Gotcha
PgBouncer is only available on General Purpose and Memory Optimized β€” NOT Burstable. This is a classic exam trap. If the question mentions connection pooling with PostgreSQL β†’ answer cannot be Burstable.

2. Managed Capabilities

  1. Automatic backups β€” 7–35 day retention, point-in-time restore to any second within the window. AES-256 encrypted at rest.
  2. High Availability β€” zone-redundant HA with automatic failover. Standby replica in a different AZ.
  3. Extensions β€” enable with CREATE EXTENSION:
    1. pgvector β€” vector similarity search (cosine, L2, inner product)
    2. pg_trgm β€” fuzzy text matching for AI document search
    3. uuid-ossp β€” UUID generation
    4. hstore β€” key-value pairs in a column
πŸ“˜ Unit 3

Connect to PostgreSQL

10 min

1. Connection Parameters

  1. Endpoint: servername.postgres.database.azure.com
  2. Direct port: 5432
  3. PgBouncer port: 6432
  4. Username format (Entra): username@servername

2. Entra ID Authentication (Recommended)

Short-lived OAuth2 tokens instead of passwords. Works with managed identities. No stored credentials.

from azure.identity import DefaultAzureCredential

credential = DefaultAzureCredential()
token = credential.get_token("https://ossrdbms-aad.database.windows.net/.default")
# Use token.token as the password in your psycopg2 connection string
πŸ’‘ Exam Tip
Entra auth token resource = https://ossrdbms-aad.database.windows.net/.default. Memorize this URL β€” the exam tests it. Tokens auto-refresh; no manual rotation needed.

3. TLS / SSL Modes

  1. disable β€” no encryption. Azure rejects this for managed PostgreSQL.
  2. require β€” encrypts connection but does NOT validate server certificate.
  3. verify-ca β€” validates the certificate authority.
  4. verify-full β€” validates CA AND hostname. Recommended for production.
⚠️ Common Gotcha
Azure Database for PostgreSQL requires TLS. The strongest mode that validates both CA and hostname is verify-full. The exam asks "which mode ensures the client is connecting to the correct server?" β€” answer: verify-full.

4. PgBouncer β€” Connection Pooling

AI applications make many short-lived DB calls (one per inference request). Each new TCP connection is expensive. PgBouncer pools connections β€” the app creates a connection to PgBouncer, which reuses existing DB connections.

az postgres flexible-server parameter set \\
  --resource-group rg --server-name myserver \\
  --name pgbouncer.enabled --value true

# Connect via PgBouncer on port 6432
postgresql://[email protected]:6432/mydb?sslmode=require
πŸ“˜ Unit 4

Create and Manage Schemas

10 min

1. Hierarchy: Server β†’ Database β†’ Schema β†’ Table

Default schema is public. Use separate databases for full isolation; separate schemas for logical grouping with cross-schema JOINs possible.

2. AI-Relevant Data Types

  1. JSONB β€” binary JSON with GIN indexing. Use for flexible metadata, model parameters, nested structures.
  2. TIMESTAMPTZ β€” always use over TIMESTAMP for global apps. Stores UTC, displays per session timezone.
  3. BIGSERIAL β€” auto-incrementing 64-bit integer PK. Use over SERIAL (32-bit overflows on high-volume tables).
  4. TEXT β€” unbounded string. Same performance as VARCHAR in PostgreSQL. No reason to use VARCHAR unless enforcing max length.
  5. UUID β€” universally unique ID. Use DEFAULT gen_random_uuid(). Good for distributed, globally-unique identifiers.

3. Conversation History Table Example

CREATE TABLE conversations (
    id          BIGSERIAL PRIMARY KEY,
    session_id  UUID NOT NULL DEFAULT gen_random_uuid(),
    user_id     VARCHAR(255) NOT NULL,
    started_at  TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    metadata    JSONB DEFAULT '{}'::jsonb
);

CREATE TABLE messages (
    id              BIGSERIAL PRIMARY KEY,
    conversation_id BIGINT NOT NULL REFERENCES conversations(id) ON DELETE CASCADE,
    role            VARCHAR(50) NOT NULL CHECK (role IN ('user', 'assistant', 'system')),
    content         TEXT NOT NULL,
    created_at      TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
);

4. Indexes

CREATE INDEX idx_messages_conversation_id ON messages(conversation_id);
CREATE INDEX idx_messages_conv_created ON messages(conversation_id, created_at);

Indexes speed reads but slow writes. Add indexes when query profiling reveals slow queries β€” not preemptively on every column.

5. Transactional DDL (PostgreSQL Superpower)

BEGIN;
ALTER TABLE conversations ADD COLUMN category VARCHAR(100);
CREATE INDEX idx_conversations_category ON conversations(category);
COMMIT;   -- or ROLLBACK on failure
πŸ’‘ Exam Tip
PostgreSQL DDL is transactional (unlike MySQL). Wrap related schema changes in BEGIN/COMMIT β€” if one fails, all roll back. This is a key differentiator the exam tests.
πŸ“˜ Unit 5

Query Data

10 min

1. JSONB Queries

-- Access JSONB field
SELECT metadata->>'model' AS model_name FROM conversations;

-- Filter on JSONB (containment operator @>)
SELECT * FROM conversations WHERE metadata @> '{"tier": "premium"}'::jsonb;

-- Check JSONB key existence
SELECT * FROM conversations WHERE metadata ? 'user_preferences';

2. Upserts with ON CONFLICT

INSERT INTO conversations (session_id, user_id) VALUES ($1, $2)
ON CONFLICT (session_id)
DO UPDATE SET user_id = EXCLUDED.user_id, started_at = CURRENT_TIMESTAMP;

3. Keyset Pagination (Avoid OFFSET)

OFFSET gets slower as the offset grows. Keyset pagination is O(log n) with the right index:

-- Get next 50 messages after last seen id
SELECT * FROM messages
WHERE conversation_id = $1 AND id > $last_id
ORDER BY id ASC LIMIT 50;

4. INSERT RETURNING (Avoid Extra SELECT)

INSERT INTO messages (conversation_id, role, content)
VALUES ($1, 'user', $2)
RETURNING id, created_at;   -- Get generated values without extra round-trip

⚑ PostgreSQL Master Cheatsheet

PgBouncer port6432 (vs 5432 direct)
Entra token resourcehttps://ossrdbms-aad.database.windows.net/.default
Best SSL modeverify-full (validates CA + hostname)
Flexible metadata typeJSONB (with GIN index support)
Auto-increment PKBIGSERIAL PRIMARY KEY
TimestampsTIMESTAMPTZ (always, not TIMESTAMP)
Tier for PgBouncerGeneral Purpose or Memory Optimized (NOT Burstable)
Transactional DDLWrap ALTER TABLE in BEGIN...COMMIT
Vector search extensionpgvector (CREATE EXTENSION vector)
Pagination (large sets)Keyset (id > last_id) not OFFSET
πŸ§ͺ Unit 6

Exercise β€” Conversation History Store

30 min
  1. Create a PostgreSQL server (General Purpose tier)
  2. Enable PgBouncer and verify port 6432
  3. Obtain Entra token and connect with psycopg2
  4. Create conversations and messages tables with JSONB metadata
  5. Insert messages, query with JSONB filter, implement keyset pagination
  6. Wrap an ALTER TABLE + CREATE INDEX in a transaction
βœ… Unit 7

Knowledge Check

5 min
  1. Q: Which tier supports PgBouncer? A: General Purpose and Memory Optimized (not Burstable)
  2. Q: SSL mode that validates both CA and hostname? A: verify-full
  3. Q: AI app needs flexible metadata alongside relational data. Data type? A: JSONB
  4. Q: Why keyset pagination instead of OFFSET? A: OFFSET performance degrades as offset grows; keyset is O(log n) with the right index
  5. Q: Entra token resource URL for PostgreSQL? A: https://ossrdbms-aad.database.windows.net/.default
🏁 Unit 8

Summary

2 min

Choose General Purpose or higher for production (PgBouncer access). Use Entra authentication with token-based access. Always enforce verify-full TLS. Design schemas using TIMESTAMPTZ and BIGSERIAL. Use JSONB for flexible AI metadata. Enable PgBouncer on port 6432 for high-concurrency AI services. Wrap schema changes in transactions.

🧠 Memory Tricks

"BuGM" β€” Burstable (no PgBouncer), General Purpose (PgBouncer βœ…), Memory Optimized (PgBouncer βœ…). Want Bouncer? Go General or above.

TLS modes order: disable (blocked) β†’ require (encrypt only) β†’ verify-ca (+ CA check) β†’ verify-full (+ hostname check = best)

🏁 Unit 9

Exam Summary Card

2 min
ScenarioAnswer
Connection pooling neededGeneral Purpose or Memory Optimized tier (port 6432)
Secure Entra auth token resourcehttps://ossrdbms-aad.database.windows.net/.default
Validates CA AND hostnamesslmode=verify-full
Flexible metadata typeJSONB
Vector similarity searchpgvector extension
Large dataset paginationKeyset (WHERE id > last_id LIMIT N)
Schema change that auto-rollbacks on failureALTER TABLE inside BEGIN/COMMIT transaction
UUID primary keyDEFAULT gen_random_uuid()
🐘
Module Cheatsheet

Azure Database for PostgreSQL

25–30% PDF

πŸ”‘ Key Facts

  • PgBouncer tier β€” General Purpose or Memory Optimized ONLY (not Burstable)
  • PgBouncer port β€” 6432 (vs 5432 direct)
  • Entra token resource β€” https://ossrdbms-aad.database.windows.net/.default
  • Best TLS mode β€” verify-full β€” validates CA AND hostname
  • JSONB β€” Binary JSON with GIN indexing β€” use for flexible AI metadata
  • BIGSERIAL β€” Auto-increment 64-bit PK (use over SERIAL which overflows)
  • TIMESTAMPTZ β€” Always over TIMESTAMP β€” stores UTC, timezone-aware
  • Transactional DDL β€” Wrap ALTER TABLE in BEGIN...COMMIT β€” rolls back on failure

πŸ’» Commands & Patterns

-- Create AI messages table
CREATE TABLE messages (
  id         BIGSERIAL PRIMARY KEY,
  session_id UUID NOT NULL DEFAULT gen_random_uuid(),
  role       VARCHAR(50) CHECK (role IN ('user','assistant')),
  content    TEXT NOT NULL,
  metadata   JSONB DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
);
-- Keyset pagination (not OFFSET)
SELECT * FROM messages
WHERE session_id=$1 AND id > $last_id
ORDER BY id ASC LIMIT 50;
-- Transactional DDL
BEGIN;
ALTER TABLE messages ADD COLUMN tokens INT;
CREATE INDEX idx ON messages(tokens);
COMMIT;
Module

Vector Search with pgvector and RAG Patterns

units
🎬 Unit 1

Introduction to pgvector

3 min

pgvector is a PostgreSQL extension that adds a vector data type and vector similarity operators. Azure Database for PostgreSQL Flexible Server ships with pgvector pre-installed β€” enabling RAG (Retrieval-Augmented Generation) directly in your existing Postgres database.

πŸ’‘ Exam Tip
pgvector exam pillars: 1) CREATE EXTENSION vector 2) vector(1536) column type 3) <=> cosine / <-> L2 / <#> inner product operators 4) HNSW vs IVFFlat indexes 5) HNSW = better recall, IVFFlat = lower memory.
πŸ“˜ Unit 2

Setup and Store Embeddings

8 min

Enable pgvector and Create Table

-- Enable extension (once per database)
CREATE EXTENSION IF NOT EXISTS vector;

-- Create table with embedding column
CREATE TABLE documents (
    id          SERIAL PRIMARY KEY,
    title       TEXT NOT NULL,
    content     TEXT NOT NULL,
    embedding   vector(1536),  -- matches text-embedding-3-small
    category    TEXT,
    created_at  TIMESTAMPTZ DEFAULT NOW()
);

-- HNSW index for fast approximate search
CREATE INDEX ON documents
    USING hnsw (embedding vector_cosine_ops)
    WITH (m=16, ef_construction=64);

-- Insert with Python
import psycopg2
cur.execute(
    "INSERT INTO documents (title, content, embedding) VALUES (%s, %s, %s)",
    (title, content, embedding)  # embedding = list of floats
)
πŸ“˜ Unit 4

HNSW vs IVFFlat Indexes

6 min
IndexRecallBuild SpeedMemoryUse When
HNSWHigherSlowerHigherProduction, few million rows
IVFFlatLowerFasterLowerLarge datasets, memory constrained
⚠️ Common Gotcha
HNSW can be built on empty table. IVFFlat requires data first (needs to cluster). Exam default: HNSW with vector_cosine_ops for text embeddings.
🏁 Unit 5

Summary

2 min

pgvector: CREATE EXTENSION vector β†’ vector(1536) column β†’ HNSW index with vector_cosine_ops β†’ <=> cosine distance in queries β†’ RAG pipeline: embed question, find top-K, pass context to LLM. Azure PostgreSQL Flexible Server includes pgvector pre-installed. HNSW = better recall; IVFFlat = lower memory. Always use 1 - distance for similarity score.

🧠

Quick Quiz

5 questions β€” test your understanding before moving on

Finished reading this module? Mark it complete to track your progress.

Related Modules β€” Develop AI Solutions by Using Azure Data Management Services

Frequently Asked Questions

What percentage of the AI-200 exam covers Develop AI Solutions by Using Azure Data Management Services? +

Domain 2 (Develop AI Solutions by Using Azure Data Management Services) accounts for 25–30% of the AI-200 exam. Develop AI Solutions with Azure Database for PostgreSQL topics like Azure Database for PostgreSQL and pgvector are actively tested. Study all official skill objectives listed in the module header above.

Is PostgreSQL & pgvector on the AI-200 exam? +

Yes. Develop AI Solutions with Azure Database for PostgreSQL is part of Domain 2 in the official AI-200 skill outline, weighted at 25–30%. The key services tested are Azure Database for PostgreSQL, pgvector, HNSW, IVFFlat. Review the code examples and exam tips in this module for targeted prep.

How do I practice PostgreSQL & pgvector hands-on? +

The best approach is to create a free Azure account and follow the code examples in this module step-by-step. The official Microsoft Learn sandbox for Course AI-200T00-A also provides free lab environments for Azure Database for PostgreSQL and related services.