Building RAG on PostgreSQL with Pgvector and Ruby on rails

Building RAG on PostgreSQL with Pgvector and Ruby on rails

●2 ●12
calendar_today • schedule2 min read

🧠 Your Database Is Already a Vector Database

Most RAG tutorials start the same way: "First, spin up a vector database."

But Stop.

If you're on Rails, you already have PostgreSQL.
With the pgvector extension it can store and search embeddings and for most applications, that's the whole answer.

🐘 Setup is one migration

`enable_extension "vector"

add_column :document_chunks, :embedding, :vector, limit: 1536`

💎 And one line in your model

Gemfile

gem "neighbor"

class DocumentChunk < ApplicationRecord has_neighbors :embedding, dimensions: 1536, normalize: true end

🔍 Searching is just ActiveRecord

DocumentChunk .where(tenant: Current.tenant) .nearest_neighbors(:embedding, query_vector, distance: "cosine") .limit(6)

Scoped, chainable, joinable. A normal Rails query.

🚀 Why this beats a separate vector DB

1️⃣ No new infrastructure
Nothing extra to deploy, monitor, back up, or upgrade.

2️⃣ Transactional consistency
Embeddings commit in the same transaction as the record. Two systems can't drift apart.

3️⃣ Authorization you already trust
This is the big one. Retrieval almost always needs scoping by tenant, project, or permission. In Postgres that's a WHERE clause. In an external store it's metadata filtering you have to keep in sync with your auth model.
Get it wrong, and one customer's documents land in another customer's LLM prompt.

4️⃣ Real joins
Chunks plus their documents, authors, and timestamps in one query.

⚠️ Three things that will bite you

❌ Index opclass mismatch
vector_cosine_ops goes with cosine distance. Mismatch them and Postgres silently skips your index and sequential-scans the table.

❌ No similarity floor
Nearest neighbours are nearest, not relevant. Without a threshold, an unrelated question still returns chunks — which the model will happily use to hallucinate.

❌ Pure vector search
Embeddings are weak at exactly what users type literally: error codes, SKUs, invoice numbers. ERR_4012 and ERR_4013 are nearly identical in vector space.
Fix: hybrid search — pgvector + Postgres full-text, fused with Reciprocal Rank Fusion.

🎯 When to actually leave Postgres

Past roughly 5–10 million chunks, or when the vector workload starves your transactional workload for memory.
Until then, a dedicated vector database buys you one more service to operate and a second place for your authorization logic to be wrong.

Your database stays the source of truth. The vector index is a lookup layer on top of it.

💬 If you've shipped RAG in Rails — what pushed you off Postgres, if anything?

Want more #ai & #rag deep dives?

Join 6,123+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

4 Comments

1 vote
1
2 votes
1

More Posts

Sovereign Intelligence: The Complete 25,000 Word Blueprint (Download)

Pocket Portfolio - Apr 1

Architecting a Local-First Hybrid RAG for Finance

Pocket Portfolio - Feb 25

Elasticsearch with Ruby on Rails

A.Talaat - Oct 9

The Privacy Gap: Why sending financial ledgers to OpenAI is broken

Pocket Portfolio - Feb 23

RAG for Small Businesses: AI That Searches Your Company Knowledge

floxolab - Nov 6
chevron_left
863 Points • 14 Badges
5Posts
2Comments
4Connections
Senior Software Engineer, Technical Team Lead, and Project Manager with 20+ years of experience deli... Show more

Related Jobs

View all jobs →

Commenters (This Week)

4 comments
3 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!