AI & Machine Learning

Stop Building Separate Vector Databases for AI Features, Your Postgres Already Can Do This

A

Adil Sher

Author

Aug 21, 2026
5 min read
0 views
Stop Building Separate Vector Databases for AI Features, Your Postgres Already Can Do This

Three months ago, I watched our DevOps lead spend two days debugging why our analytics team's queries kept timing out. They'd asked a simple question: "Which customer segments have the highest churn?" What should have taken five minutes turned into a back-and-forth with the data team, a hastily-written SQL script, and a Slack thread that nobody felt good about. That's when I realized we were solving the same problem our users face: the gap between what people want to know and what we can answer quickly.

The real kicker? We were already running PostgreSQL. We already had all the schema metadata sitting right there. We just didn't have a way to bridge the conversation between business language and SQL. I kept wondering if there was a better way than spinning up another service, another database, another monitoring setup. Then I read about pgvector, and everything clicked.

The Problem I Actually Recognize

Here's what's happening at most companies I talk to: someone in product asks for a report. Engineering says "we need to write a query." Three days later, the data is stale or the requirements changed. Repeat this fifty times a year and you've got friction everywhere.

The RAG approach here isn't trying to replace analysts, it's trying to close the latency gap. Instead of "wait for engineering to interpret my question and write SQL," you get "ask the database directly in English and get an answer in seconds." That's not revolutionary, but it's practical.

Why This Actually Makes Sense (Unlike Every Vector DB I've Evaluated)

Most RAG tutorials show you embeddings for documents. This is different. You're not storing blog posts or customer reviews, you're storing your actual database schema as the retrieval corpus. Table names, column definitions, query examples from your codebase. The vector search finds the relevant parts of your specific database, not some generic knowledge base.

The architecture is clean: embed your question, search for matching schema metadata, build a prompt that includes the question plus the relevant tables and columns, let an LLM write SQL in read-only mode, execute it, return the result. No document preprocessing nightmares. No hallucinated tables that don't exist. The LLM is working with real constraints.

And here's what sold me: you're doing this all in Postgres. No separate vector database. No Pinecone or Weaviate bill. No extra network hops. One backup, one monitoring stack, one connection string. That matters when you're already managing too much infrastructure.

The pgvector + Postgres Combination Is Actually Smart

I'll be honest, I was skeptical about pgvector at first. Vectors in Postgres felt like trying to do machine learning in a SQL database, which is true, but not in the way I was thinking. You're not training models in Postgres. You're just storing embeddings and doing similarity search, which it handles really well with HNSW indexes.

The embedding part happens in Python (OpenAI's text-embedding-3-small), but the retrieval is pure Postgres. Cosine similarity search with sub-10ms latency on reasonable metadata sizes. That's plenty fast for an interactive assistant.

What I'd Actually Do Differently

The tutorial assumes you're starting from scratch, but most of us inherit weird schemas. I'd add a step: let humans annotate which tables actually matter. Not everything in your database is relevant to business questions. You can embed just the schema that matters, weight the examples heavier, and improve quality without increasing latency.

I'm also cautious about read-only connections. The tutorial is right to enforce that, you don't want an LLM mutating your data, but I'd go further. I'd rate-limit by user, log every generated query, and have a staging environment where I'd run untrusted SQL first. LLMs hallucinate less with databases than with documents, but they still hallucinate.

A Quick Implementation Note

The ingest step is straightforward: query information_schema.columns, extract table and column metadata, embed it, store vectors in Postgres. The retrieval step is a cosine similarity query. The generation step is a standard LLM prompt. Nothing exotic here, which is kind of the point.

# The core retrieval query
relevant_schema = conn.execute("""
SELECT object_name, definition, description 
FROM schema_metadata 
ORDER BY embedding <=> %s 
LIMIT 5
""", (question_embedding,)).fetchall()

# Build prompt with question + relevant schema
prompt = f"""
Answer this question about our database: {user_question}

Relevant schema:
{chr(10).join([f"{row[0]}: {row[2]}" for row in relevant_schema])}

Write a SELECT query that answers the question.
"""

This is the part that actually works. It's not overthinking RAG, it's applying it pragmatically.

What I'm Still Wondering

Can you embed semantic relationships (foreign keys, join patterns) better than flat column definitions? Would query performance metadata help the LLM avoid expensive joins? How do you keep metadata fresh if your schema changes weekly?

These are the details that separate a tutorial from production. But the foundation here is solid.

Source: This post was inspired by "Build a RAG-Powered Database Assistant with PostgreSQL and pgvector" by Dev.to. Read the original article

Share this article

Written by Adil Sher

Full stack developer building high-traffic platforms, AI services, and custom web applications. Explore my portfolio, learn about my background, or get in touch.

Related Articles

I Spent 6 Months Using LLMs Without Understanding Them, Here's What Changed
AI & Machine Learning Aug 20

I Spent 6 Months Using LLMs Without Understanding Them, Here's What Changed

I remember the exact moment I felt like a fraud. I was in a code review, confidently explaining how I'd integrated GPT into our Django app using the OpenAI API, when a junior dev asked: "But how does it actually know what to do?" I stammered something about "neural networks" and...

Stop Treating LLMs Like Magic: Here's What Actually Happens Inside
AI & Machine Learning Aug 18

Stop Treating LLMs Like Magic: Here's What Actually Happens Inside

I spent three hours debugging why Claude was giving inconsistent responses to structurally identical prompts last month. Same input, different outputs. My first instinct? Blame randomness. My second instinct? Blame the API. My actual problem? I had no mental model for what was ac...