pgvector
pgvector is an open-source extension for PostgreSQL that adds vector similarity search capabilities. It allows you to store vectors alongside your relational data and perform exact or approximate nearest neighbor searches, supporting various vector types and distance metrics. It is designed for developers who want to integrate vector search into their Postgres database without introducing a separate vector database.
✨ Key features
- Exact and approximate nearest neighbor search
- Supports single-precision, half-precision, binary, and sparse vectors
- Multiple distance functions: L2, inner product, cosine, L1, Hamming, Jaccard
- ACID compliance, point-in-time recovery, JOINs, and other Postgres features
- Indexing with HNSW and IVFFlat for approximate search
- Filtering and multitenancy support with partitioning
🎯 Use cases
- Recommendation systems: find similar items based on vector embeddings
- Semantic search: retrieve documents or images by similarity
- Anomaly detection: identify outliers by distance from a cluster
- Multitenant applications: isolate vector data per tenant using partitioning
📦 Installation
🧰 Requirements: Requires PostgreSQL 13 or later. No external services or API keys needed.
cd /tmp
git clone --branch v0.8.6 https://github.com/pgvector/pgvector.git
cd pgvector
make
make install # may need sudo
For Windows, use nmake in a Visual Studio command prompt:
set "PGROOT=C:\Program Files\PostgreSQL\18"
cd %TEMP%
git clone --branch v0.8.6 https://github.com/pgvector/pgvector.git
cd pgvector
nmake /F Makefile.win
nmake /F Makefile.win install
🚀 Usage
CREATE EXTENSION vector;
CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));
INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');
SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;
⚠️ Good to know
Approximate indexes (HNSW, IVFFlat) trade recall for speed and may return different results than exact search. Also, the README notes that for inner product, <#> returns negative inner product, and for cosine similarity, you need to compute 1 - cosine distance.
❓ FAQ
What PostgreSQL versions are supported?
pgvector supports PostgreSQL 13 and later.
How do I install pgvector?
You can compile from source using make on Linux/Mac or nmake on Windows, or use package managers like Docker, Homebrew, PGXN, APT, Yum, pkg, APK, or conda-forge.
What distance functions are available?
L2 distance (<->), inner product (<#>), cosine distance (<=>), L1 distance (<+>), Hamming distance (<~>), and Jaccard distance (<%>).
How do I create an index for approximate search?
Use CREATE INDEX ... USING hnsw (embedding vector_l2_ops) for HNSW or CREATE INDEX ... USING ivfflat (embedding vector_l2_ops) WITH (lists = 100) for IVFFlat, adjusting the operator class for your distance function.
📊 Repository
🤖 Overview, features, install steps and FAQ were generated from the project's README on Sep 4, 2026. Always check the original source before running commands.