Start a project
All articles
AI & Data··6 min read

Adding Semantic Search to Your App with pgvector and Supabase

You already have Postgres. Here is how to add AI-powered semantic search with pgvector and Supabase, no separate vector database, no extra infrastructure.

pgvectorSupabasePostgreSQLAI

Teams reach for a dedicated vector database far too early. If your data already lives in Postgres, and with Supabase it does, you can add semantic search with the pgvector extension and skip an entire piece of infrastructure.

Store embeddings next to your data

Enable the extension, add a vector column, and store an embedding alongside each row. Keeping vectors in the same table as your content means your filters (tenant, category, published state) and your similarity search run in one query.

create extension if not exists vector;

alter table documents add column embedding vector(1536);

-- Approximate nearest-neighbour index for fast search
create index on documents
  using hnsw (embedding vector_cosine_ops);

Search with a single query

select id, title,
       1 - (embedding <=> $1) as similarity
from documents
where tenant_id = $2
order by embedding <=> $1
limit 10;

The `<=>` operator is cosine distance. Because it is plain SQL, you keep row-level security, joins, and WHERE clauses, the things a standalone vector store makes awkward.

When to graduate

pgvector comfortably handles hundreds of thousands to a few million vectors with an HNSW index. Past that, or when you need very high query throughput, a dedicated store earns its keep. Until then, keep it simple, one database is easier to run, back up, and reason about.

Want this built into your product?

I take on freelance work across AI, voice, backend and full-stack. Tell me what you're building.

Start a project