pgvector — Vector Similarity Search Inside PostgreSQL
A PostgreSQL extension that adds a native `vector` type, HNSW and IVFFlat indexes, and distance operators so semantic search, RAG and recommendation workloads can reuse the same database as the rest of the app.
What it is
pgvector is a PostgreSQL extension that adds a native vector data type, HNSW and IVFFlat indexes, and distance operators for similarity search. It lets you run semantic search, RAG retrieval, and recommendation workloads inside the same database as your application data.
The extension targets teams that already use PostgreSQL and want to add vector search without deploying a separate vector database. It keeps your operational complexity low by reusing your existing database infrastructure.
The project is actively maintained and suitable for both individual developers and teams looking to integrate it into their existing toolchain. Documentation and community support are available for onboarding.
How it saves time or tokens
pgvector eliminates the need to sync data between PostgreSQL and a dedicated vector store. Embeddings live in the same table as your application data, enabling SQL JOINs between vectors and structured data. Transactional consistency means your vector index stays in sync with your data automatically. One backup, one monitoring stack, one connection pool.
How to use
- Install the pgvector extension:
CREATE EXTENSION vector; - Add a vector column to your table:
ALTER TABLE items ADD COLUMN embedding vector(1536); - Insert embeddings generated by your embedding model (OpenAI, Cohere, local models).
- Create an index and query by similarity using distance operators.
Example
-- Enable the extension
CREATE EXTENSION vector;
-- Create a table with embeddings
CREATE TABLE documents (
id SERIAL PRIMARY KEY,
title TEXT,
content TEXT,
embedding vector(1536)
);
-- Create an HNSW index for fast approximate search
CREATE INDEX ON documents
USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64);
-- Find the 5 most similar documents
SELECT id, title, 1 - (embedding <=> $1::vector) AS similarity
FROM documents
ORDER BY embedding <=> $1::vector
LIMIT 5;
Related on TokRepo
- AI Tools for RAG — RAG frameworks and retrieval tools that work with pgvector.
- AI Tools for Database — Database tools and extensions for AI workloads.
Common pitfalls
- Not creating an index before querying. Without an HNSW or IVFFlat index, pgvector does exact (brute-force) search, which is slow for tables with more than 10,000 rows.
- Using the wrong distance operator.
<=>is cosine distance,<->is L2 distance,<#>is inner product. Match the operator to how your embeddings were trained. - Storing embeddings with the wrong dimension. The vector column dimension must match your embedding model's output dimension exactly. A mismatch causes insertion errors.
- Not reading the changelog before upgrading. Breaking changes between versions can cause unexpected failures in production. Pin your version and review release notes.
Frequently Asked Questions
pgvector runs inside PostgreSQL, so there is no additional infrastructure. Pinecone and Milvus are purpose-built vector databases with more advanced features (filtering, sharding, hybrid search) at very high scale. pgvector is sufficient for most applications with up to millions of vectors.
HNSW is recommended for most use cases. It offers better query performance with slightly higher memory usage. IVFFlat uses less memory but requires a separate training step and may have lower recall. HNSW is the default choice.
Yes. pgvector with HNSW indexes handles millions of vectors with sub-10ms query latency on typical hardware. For tens of millions of vectors, consider dedicated vector databases or sharding strategies.
Yes. You can combine vector similarity search with standard SQL WHERE clauses. PostgreSQL's query planner uses the HNSW index alongside standard B-tree indexes for filtered queries.
Use an embedding model (OpenAI text-embedding-3-small, Cohere embed, or open-source models via Hugging Face). Generate embeddings in your application code and insert them as vector values. pgvector stores and indexes them; it does not generate embeddings itself.
Citations (3)
- pgvector GitHub— Native vector type and similarity search for PostgreSQL
- pgvector README— HNSW and IVFFlat index support
- PostgreSQL Documentation— PostgreSQL extension ecosystem
Related on TokRepo
Discussion
Related Assets
Cython — Write C Extensions for Python Using Python-Like Syntax
Cython is an optimizing static compiler that translates Python-like code into C, producing extension modules that run at native speed. It is used to build high-performance libraries and to wrap existing C/C++ code for Python access.
Numba — JIT Compiler That Makes Python Code Run at C Speed
Numba is an open-source JIT compiler that translates Python and NumPy code into fast machine code using LLVM. It accelerates numerical functions by orders of magnitude with minimal code changes.
ImageMagick — Command-Line Image Processing for 200+ Formats
ImageMagick is a free, open-source software suite for creating, editing, compositing, and converting images. It supports over 200 image formats including PNG, JPEG, TIFF, WebP, SVG, and PDF.