How to Build an AI App with Supabase and pgvector

Auth, DB, and vector search — all in one Supabase project

Ad placeholder (leaderboard)

What you are building

This tutorial builds a full-stack AI app on a single Supabase project. You use Supabase Auth for users, Postgres with pgvector for embeddings, Edge Functions for the LLM and embedding calls, and Realtime to stream answers to the client. Everything — application data, vectors, and access control — lives in one database, so you get semantic search, joins, row-level security, and backups without operating a separate vector store. It is the leanest way to ship a RAG-style app that still scales to millions of vectors.

How it works

You enable Supabase Auth and run create extension vector, then create a table with a vector column sized to your embedding model, plus the source content and a user_id. An Edge Function handles writes: it reads your provider key from function secrets, embeds the text server-side, and inserts the row — the key never touches the browser. For reads, you add an HNSW (or IVFFlat) index on the vector column and an RPC that returns the nearest rows by cosine distance; row-level security policies keyed on user_id ensure a search only ever surfaces the caller’s own data. Those nearest rows become context for an LLM call, and the streamed answer is pushed to the client over Supabase Realtime so it appears incrementally.

Tips and the planner below

Enable RLS before you store anything real — without it, a similarity query can leak another tenant’s documents. Match the index distance operator to how your embedding model was trained, and build the index after loading representative data so it reflects real distribution. Keep provider keys in Edge Function secrets, never in client code. Watch memory: HNSW indexes are fast but memory-hungry, and that often sizes your plan more than raw storage does. The planner below estimates total vector storage and approximate index memory from your row count, embedding dimension, and index choice, so you can pick a Supabase tier before you hit a limit.

Ad placeholder (rectangle)