The short answer
#If you already have Postgres, use pgvector. It adds a vector column type, distance operators and an HNSW index, and it handles millions of rows comfortably once indexed. Your embeddings live next to the rows they describe, so you filter and join with ordinary SQL and your existing permissions apply. Reach for a dedicated vector database when you have tens of millions of vectors or very high query rates, need features it has and Postgres lacks, or have no Postgres and no wish to run one.
_10CREATE EXTENSION IF NOT EXISTS vector;_10_10CREATE TABLE chunks (_10 id bigserial PRIMARY KEY,_10 document_id bigint REFERENCES documents(id),_10 content text NOT NULL,_10 embedding vector(1024)_10);
What it means
#A vector database answers one question: given a query vector, which stored vectors are nearest to it. With a few thousand rows you can compare the query to every row and sort. Beyond that you need an approximate nearest neighbour (ANN) index such as HNSW, which gives up a little recall for a large speed-up. That is the whole product. Dedicated vector databases, such as Pinecone, Qdrant or Weaviate, package it as a service with their own API, hosting and bill.
pgvector is the same idea as a Postgres extension. The trade is simple. A dedicated service may be faster at extreme scale and ships retrieval-specific features. pgvector keeps your data in one place, under one set of backups, transactions and access rules, and lets you write JOIN and WHERE like you already do. For a web application with a corpus in the thousands to low millions of chunks, that trade favours Postgres almost every time.
How to do it
#Insert and query
pgvector accepts a vector as a bracketed list, so JSON.stringify() on a number[] is exactly the right format. The <=> operator is cosine distance: 0 for identical direction, up to 2 for opposite. Subtract from 1 to get the similarity most people expect.
_15import { Pool } from 'pg';_15const pool = new Pool();_15_15await pool.query(_15 'INSERT INTO chunks (document_id, content, embedding) VALUES ($1, $2, $3::vector)',_15 [documentId, content, JSON.stringify(embedding)],_15);_15_15const { rows } = await pool.query(_15 `SELECT id, content, 1 - (embedding <=> $1::vector) AS similarity_15 FROM chunks_15 ORDER BY embedding <=> $1::vector_15 LIMIT 5`,_15 [JSON.stringify(queryEmbedding)],_15);
Always embed the query with the same model you used for the rows.
Add an HNSW index
Without an index Postgres compares every row, which is fine for tens of thousands and slow beyond that. The operator class must match the operator you query with: vector_cosine_ops for <=>.
_10CREATE INDEX ON chunks USING hnsw (embedding vector_cosine_ops);
HNSW indexes take time and memory to build but query fast. Build after a bulk load rather than before. At query time, SET hnsw.ef_search = 100; raises recall at some cost in speed; the default is 40.
Join and filter with your own tables
This is where pgvector stops being "good enough" and becomes the better choice. Per-tenant data, published status, date ranges and ownership are all just predicates.
_10SELECT c.id, c.content, d.title_10FROM chunks c_10JOIN documents d ON d.id = c.document_id_10WHERE d.workspace_id = $2_10 AND d.published_at IS NOT NULL_10ORDER BY c.embedding <=> $1::vector_10LIMIT 5;
If you use row-level security, the policies apply to this query as they do to any other, so a user can only ever retrieve chunks from documents they are allowed to see. In a dedicated vector database you would re-implement that filtering in metadata and keep it in sync by hand.
One thing to know: a very selective filter combined with an HNSW index can return fewer than LIMIT rows, because the index finds its nearest candidates first and the filter then removes most of them. pgvector 0.8 added iterative index scans to address this; if filtered queries come back short, check the pgvector README for hnsw.iterative_scan, or add a partial index per tenant.
Run it where you already are
Most managed Postgres providers ship pgvector; Supabase and Neon both do, and enabling it is CREATE EXTENSION vector;. Locally, the official pgvector/pgvector Docker image is Postgres with the extension installed. There is nothing new to deploy, monitor or pay for.
When a dedicated vector database makes sense
- Scale. Tens or hundreds of millions of vectors, or thousands of queries per second, where the HNSW index would not fit in Postgres memory and you would be tuning the database around it.
- Specialised features. Built-in hybrid search with rerankers, multi-vector documents, or sharding strategies tuned for vector workloads.
- No Postgres. If your stack has no relational database, a managed vector service may be less to operate than adding one. Though a managed Postgres is also an option.
- You already run one. If a team operates a vector database for other work, use it. The point is not to add infrastructure you do not need.
Common pitfalls
#- Forgetting the index. Queries that were instant at 10,000 rows crawl at a million. Add the HNSW index, and check
EXPLAINto see it being used. - Mismatched operator class. An index built with
vector_l2_opsdoes nothing for a<=>query. Match the class to the operator. - Dimension mismatch.
vector(1024)rejects a 1,536-dimension insert. Pick the column size for the embedding model you will actually use, and record that model with the data. - Storing vectors as JSON. A
jsonbarray of floats cannot be indexed for similarity and forces you to compare in application code. Use thevectortype. - Filtered queries coming back short. See above: HNSW plus a selective
WHEREcan under-fill. Use iterative scans or partial indexes. - Building the index before the bulk load. Inserting millions of rows into an existing HNSW index is far slower than loading first and indexing after.
Related reading
#quick question
Was this resource helpful?
by Danny