RAG with Postgres and pgvector: A Practical Guide for Production

RAG with Postgres and pgvector: A Practical Guide for Production

Most RAG tutorials start by asking you to sign up for a new vector database. You probably don’t need one. If you already run Postgres, the pgvector extension gives you vector search next to the rest of your data, with transactions, backups and access control you already understand.

The shape of a RAG system

  1. Ingest: split documents into chunks, embed each chunk, store text + vector + metadata.
  2. Retrieve: embed the user’s question, find the closest chunks.
  3. Generate: give the model the question plus the retrieved chunks, and ask it to answer with citations.

Everything interesting happens in the details of steps 1 and 2.

Set up pgvector

CREATE EXTENSION IF NOT EXISTS vector;

CREATE TABLE chunks (
  id          bigserial PRIMARY KEY,
  doc_id      text NOT NULL,
  chunk_index int  NOT NULL,
  content     text NOT NULL,
  embedding   vector(1536) NOT NULL,
  updated_at  timestamptz DEFAULT now()
);

CREATE INDEX ON chunks USING hnsw (embedding vector_cosine_ops);

Match the vector dimension to your embedding model. HNSW indexes give fast approximate search with good recall for most workloads.

Query by similarity

SELECT doc_id, content, 1 - (embedding <=> $1) AS similarity
FROM chunks
ORDER BY embedding <=> $1
LIMIT 8;

<=> is cosine distance. Filter with ordinary WHERE clauses (tenant, language, document type) which is one of the biggest wins of keeping vectors in Postgres.

Chunking decides quality

  • Split on structure (headings, paragraphs) before splitting on length.
  • Keep chunks a few hundred tokens long, with a small overlap so ideas aren’t cut in half.
  • Store the heading path (“Runbook › Database › Failover”) with each chunk; it helps both retrieval and citations.

When answers are bad, look at what was retrieved before touching the prompt. Most RAG problems are retrieval problems.

Hybrid search beats pure vectors

Embeddings are great at meaning and weak at exact terms like error codes, product names or IDs. Combine vector search with Postgres full-text search and merge the two ranked lists (reciprocal rank fusion is a simple, effective method). Users searching for ERR_CONN_RESET will thank you.

Production details tutorials skip

  • Re-embed on change. Track updated_at and re-embed only changed documents.
  • Version your embedding model. Store which model produced each vector; switching models means re-embedding everything.
  • Cite sources in answers and log which chunks were used: essential for debugging and trust.
  • Respect permissions at query time, not just at ingest. A chunk a user can’t open must never reach their prompt.
  • Evaluate retrieval separately: for a set of real questions, does the right chunk appear in the top results?

When to move off Postgres

When you have hundreds of millions of vectors, very high query rates, or need features your Postgres setup can’t provide. Many products never reach that point, and the patterns here, from SLOs for LLM features to LLM evals, stay the same either way.

FAQ

Is pgvector fast enough for production?

For many applications, yes, especially with HNSW indexes and sensible filters. Load-test with your real data size and query patterns.

What chunk size should I use for RAG?

Start with a few hundred tokens and small overlap, then tune using retrieval evals on real questions.

Do I need a separate vector database for RAG?

No. Start with pgvector if you already run Postgres; move only when measured limits require it.