---
title: "Do I need a vector database, or will Postgres with pgvector do?"
description: "For most apps, pgvector in the Postgres you already run is enough, up to millions of rows. A dedicated vector database earns its place only at very large scale."
url: https://www.danny.engineering/answer/ai/do-i-need-a-vector-database-or-will-postgres-with-pgvector-do
author: Danny
published: 2026-12-03T00:00:00.000Z
updated: 2026-12-03T15:35:00.000Z
tags: ["AI"]
---

# Do I need a vector database, or will Postgres with pgvector do?

## 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.


```sql
CREATE EXTENSION IF NOT EXISTS vector;

CREATE TABLE chunks (
  id bigserial PRIMARY KEY,
  document_id bigint REFERENCES documents(id),
  content text NOT NULL,
  embedding vector(1024)
);
```


## 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.


```typescript
import { Pool } from 'pg';
const pool = new Pool();

await pool.query(
  'INSERT INTO chunks (document_id, content, embedding) VALUES ($1, $2, $3::vector)',
  [documentId, content, JSON.stringify(embedding)],
);

const { rows } = await pool.query(
  `SELECT id, content, 1 - (embedding <=> $1::vector) AS similarity
   FROM chunks
   ORDER BY embedding <=> $1::vector
   LIMIT 5`,
  [JSON.stringify(queryEmbedding)],
);
```


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 `<=>`.


```sql
CREATE 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.


```sql
SELECT c.id, c.content, d.title
FROM chunks c
JOIN documents d ON d.id = c.document_id
WHERE d.workspace_id = $2
  AND d.published_at IS NOT NULL
ORDER BY c.embedding <=> $1::vector
LIMIT 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 `EXPLAIN` to see it being used.
- **Mismatched operator class.** An index built with `vector_l2_ops` does 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 `jsonb` array of floats cannot be indexed for similarity and forces you to compare in application code. Use the `vector` type.
- **Filtered queries coming back short.** See above: HNSW plus a selective `WHERE` can 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

- [pgvector](https://github.com/pgvector/pgvector)
- [Supabase: vector columns](https://supabase.com/docs/guides/ai/vector-columns)
- [Postgres row security policies](https://www.postgresql.org/docs/current/ddl-rowsecurity.html)
- [Postgres indexes](https://www.postgresql.org/docs/current/indexes.html)
