v1.0 — 13 chapters · 154 references · 500+ readers

build AI applications.
one database. PostgreSQL.

Master pgvector, RAG pipelines, and in-database ML. From embeddings to production — everything you need to build AI applications in 1,065 pages.

PostgreSQL for AI book — PostgreSQL elephant mascot representing AI-powered database applications
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

what you'll learn.

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
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
          )
      )
    )
);

rag_pipelines/

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

[embeddings][chunking][caching][evaluation]

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
pgml
train in-database
4
model types covered
PL/Py
custom pipelines
0
infra overhead

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]

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
RLS
row-level security
HA
high availability
CI/CD
migration patterns
GDPR
compliance ready

production/

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

[monitoring][security][ha][cicd]
// table_of_contents

what's inside: 13 chapters.

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

Part IFoundations
  • 01Introduction: Why PostgreSQL for AI?
  • 02Modern PostgreSQL for AI Workloads
Part IICore AI
  • 03Vector Search with pgvector
  • 04LLM Integration & RAG Fundamentals
  • 05Advanced RAG Patterns
  • 06Feature Engineering
  • 07In-Database Machine Learning
Part IIIProduction Systems
  • 08Real-Time AI Pipelines
  • 09Architecture Patterns
  • 10Performance & Optimization
Part IVOperations
  • 11Security for AI Systems
  • 12Production Deployment
  • 13The Future of PostgreSQL + AI
  • BBonus: Ask the Book (RAG Capstone)
// built_with

Technologies covered in the book

PostgreSQL 17
pgvector
TimescaleDB
Ollama
Debezium
Docker
// who_its_for

who this book is for.

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

meet the author.

Ahmet Zeybek — Author of PostgreSQL for AI

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.

// what_readers_say

what readers are saying.

Replaced our entire Pinecone setup with pgvector after reading Chapter 3. Same latency, one fewer service to maintain.

Sarah Chen
Staff Engineer

The RAG chapter alone saved us weeks. Production-ready patterns, not academic exercises.

Marcus Weber
ML Engineer

Finally a book that treats PostgreSQL as a first-class AI platform. The feature engineering chapter is gold.

Priya Sharma
Tech Lead
// frequently_asked

frequently asked questions.

What is PostgreSQL for AI?

PostgreSQL for AI is a comprehensive technical book that teaches developers how to use PostgreSQL as a unified platform for AI applications. It covers pgvector, RAG pipelines, recommendation systems, feature engineering, and in-database machine learning across 13 chapters and 1,065 pages. Instead of managing separate vector databases and ML platforms, you learn to build everything within PostgreSQL.

How is pgvector different from dedicated vector databases?

pgvector is a PostgreSQL extension that adds vector similarity search directly inside your existing database. Unlike dedicated vector databases like Pinecone or Weaviate, pgvector lets you join vectors with relational data, use ACID transactions, and avoid syncing data between systems. The book covers HNSW, IVFFlat, and DiskANN indexes for production-grade performance.

What is in-database machine learning?

In-database machine learning means training and running ML models directly inside PostgreSQL using extensions like PostgresML — no need to export data to external tools. The book teaches you to build feature engineering pipelines with SQL window functions, train models, and serve predictions all within your database.

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.

$--github
required — for companion repo access