v1.0 — 13 chapters · 153 references · production-tested

build AI applications.
one database. PostgreSQL.

Stop managing four services for what should be one query. Vector search, RAG pipelines, feature stores, and in-database ML — all from the database you already run.

PostgreSQL elephant
psql — ai_db
-- Find products by meaning, not keywords
WITH query AS (
    SELECT ai.ollama_embed(
        'nomic-embed-text',
        'comfortable running shoes for beginners'
    ) AS embedding
)
SELECT p.name, p.category,
       1 - (p.embedding <=> q.embedding) AS similarity
FROM products p, query q
WHERE p.embedding IS NOT NULL
ORDER BY p.embedding <=> q.embedding
LIMIT 5;
// what_you_build

everything you need.
one database.

Through a running example — RecSys, an AI-powered recommendation platform — you'll implement production patterns chapter by chapter.

vector_search/

pgvector with HNSW, IVFFlat, and DiskANN indexes. Hybrid search combining semantic similarity with full-text ranking and business filters.

[hnsw][ivfflat][diskann][hybrid_search]
psql — ai_db
$ psql ai_db

CREATE INDEX idx_products_hnsw
  ON products USING hnsw (
    embedding vector_cosine_ops
  )
  WITH (m = 16,
        ef_construction = 128);

✓ index created — 0.47s

rag_pipelines/

In-database embedding generation, chunking strategies, prompt engineering, semantic caching, and evaluation — all through SQL and pgai.

[embeddings][chunking][caching][evaluation]
psql — ai_db
-- RAG: retrieve & generate in one query
SELECT ai.ollama_chat_complete(
    'llama3',
    jsonb_build_array(
      jsonb_build_object(
        'role', 'system',
        'content', 'Answer using context:'
          || (
            SELECT string_agg(chunk, E'\n')
            FROM docs
            ORDER BY embedding <=> $query_vec
            LIMIT 5
          )
      )
    )
);

feature_engineering/

Point-in-time correct features with window functions, materialized views, continuous aggregates, and a lightweight feature registry.

[window_functions][materialized_views][caggs][registry]
< 2ms
feature lookup p99
100%
point-in-time correct
SQL
native window fns
0
external services

in_database_ml/

Train and serve models with PostgresML. Regression, classification, anomaly detection, and custom PL/Python pipelines — no external infrastructure.

[postgresml][regression][anomaly_detection][pl_python]
pgml
train in-database
4
model types covered
PL/Py
custom pipelines
0
infra overhead

realtime_ai/

LISTEN/NOTIFY, pg_cron scheduling, SKIP LOCKED job queues, and Debezium CDC for streaming AI pipelines that react to data changes.

[listen_notify][pg_cron][cdc][skip_locked]
RT
event-driven pipelines
cron
scheduled tasks
CDC
change data capture
FIFO
SKIP LOCKED queues

production/

Performance tuning, security hardening, cloud deployment, monitoring, backup strategies, and CI/CD patterns for AI-enhanced PostgreSQL.

[monitoring][security][ha][cicd]
RLS
row-level security
HA
high availability
CI/CD
migration patterns
GDPR
compliance ready
// table_of_contents

four parts. thirteen chapters.

Each chapter builds on the last. Every code example runs against a real PostgreSQL instance via Docker Compose.

Part I — Foundations
  • 01Introduction: Why PostgreSQL for AI?
  • 02Modern PostgreSQL for AI Workloads
Part II — Core AI
  • 03Vector Search with pgvector
  • 04LLM Integration & RAG Fundamentals
  • 05Advanced RAG Patterns
  • 06Feature Engineering
  • 07In-Database Machine Learning
Part III — Production Systems
  • 08Real-Time AI Pipelines
  • 09Architecture Patterns
  • 10Performance & Optimization
Part IV — Operations
  • 11Security for AI Systems
  • 12Production Deployment
  • 13The Future of PostgreSQL + AI
  • BBonus: Ask the Book (RAG Capstone)
// built_with
PostgreSQL 17
pgvector
TimescaleDB
Ollama
Debezium
Docker
// who_its_for

three paths. one destination.

Whether you're adding AI to an existing app or architecting from scratch, the book meets you where you are.

backend_engineer/

You run PostgreSQL in production and want to add semantic search, recommendations, or LLM features — without new infrastructure or vendor lock-in.

data_scientist/

You know ML but not databases. Learn how feature engineering, model serving, and vector search work inside the database that stores your data.

tech_lead/

You're weighing dedicated vector databases and ML platforms against what PostgreSQL handles natively. Get the benchmarks and decision frameworks.

// about_the_author
AZ

Ahmet Zeybek

Software engineer and technical author specializing in PostgreSQL, artificial intelligence, and the intersection of databases with modern AI systems. Every pattern in this book comes from production experience.

// frequently_asked

questions.

Do I need ML experience?

No. The book introduces every concept from scratch. You need working SQL knowledge and basic Python. All ML theory is explained inline, with code you can run immediately.

What PostgreSQL version?

PostgreSQL 17 via Docker Compose. The included stack bundles pgvector, TimescaleDB, pg_cron, and PostgresML — one command to get everything running.

Is there a print edition?

Not yet. The book ships as DRM-free PDF + EPUB. Both are optimized for reading — code blocks are syntax-highlighted and all diagrams are vector graphics.

Do I need a GPU?

No. All examples use Ollama with CPU-friendly models (nomic-embed-text, llama3.2). A laptop with 16 GB RAM is sufficient. GPU just makes things faster.

Can I use this with cloud Postgres?

Yes. The patterns work on any PostgreSQL with pgvector: AWS RDS, Google Cloud SQL, Azure Flexible Server, Supabase, Neon, or your own setup.

Are updates included?

Yes. You get free lifetime updates for the edition you purchased. When pgvector 0.9 or PostgreSQL 18 lands, the book will be updated and you’ll get the new version.

// get_the_book

start building AI
with PostgreSQL.

$ buy --format "pdf + epub" --price $49
13 chapters + bonus
complete source code
docker environment
free updates
$--github
optional — get access to the companion repo
DRM-free. PDF + EPUB, read on any device. Yours forever.