Menu

1 / 2

Building RAG on PostgreSQL with Pgvector and Ruby on rails

DEV Community: rubyΒ·A.TalaatΒ·about 23 hours ago
#YyXo1ymA
#devtoruby#devto#dev#vector#database
Reading 0:00
15s threshold

🧠 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.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.