# pgvector

> Open-source vector similarity search for Postgres

- Category: Retrieval & Knowledge (RAG) / Vector Databases
- Language: C
- Repository: https://github.com/pgvector/pgvector
- Canonical page: https://ai-tldr.dev/tools/pgvector/

## In short

pgvector is Open-source vector similarity search for Postgres. It sits in the Vector Databases category of the Retrieval & Knowledge (RAG) tools and is open-source, written in C. To get started: compile and install (Linux/Mac).

## Overview

pgvector is an open-source extension that adds a vector data type and similarity search to PostgreSQL. It lets you store embeddings in the same database as the rest of your application data and query them with regular SQL, so you don't need to run a separate vector store alongside Postgres.

It is aimed at developers who already use Postgres and want to add semantic search, recommendations, or retrieval for AI applications. Because vectors live in normal tables, you keep JOINs, transactions, ACID guarantees, point-in-time recovery, and the rest of Postgres on top of your similarity search.

As a vector database option, pgvector supports both exact and approximate nearest neighbor search, single-precision, half-precision, binary, and sparse vectors, and several distance functions including L2, inner product, cosine, L1, Hamming, and Jaccard. It works from any language that has a Postgres client.

## What it does

- Adds a native vector column type to Postgres tables, alongside halfvec, bit (binary), and sparsevec types
- Exact nearest neighbor search for perfect recall, plus approximate search via HNSW and IVFFlat indexes
- Distance operators for L2 (<->), inner product (<#>), cosine (<=>), L1 (<+>), Hamming (<~>), and Jaccard (<%>)
- Full Postgres feature set: ACID compliance, JOINs, point-in-time recovery, and standard SQL queries
- Works with any language that has a Postgres client; supports Postgres 13 and later
- Available via Docker, Homebrew, APT, Yum, conda-forge, and many hosted Postgres providers

## Getting started

Install the extension into a Postgres 13+ instance, enable it once per database, then create a vector column and run a nearest-neighbor query.

1. Compile and install (Linux/Mac)

   Clone the extension and build it with make. You may need sudo for the install step. Docker, Homebrew, APT, Yum, and conda-forge packages are also available.

```bash
cd /tmp
git clone --branch v0.8.2 https://github.com/pgvector/pgvector.git
cd pgvector
make
make install # may need sudo
```

2. Enable the extension

   Run this once in each database where you want to use vectors.

```sql
CREATE EXTENSION vector;
```

3. Create a table and insert vectors

   Add a vector column with a fixed number of dimensions, then insert your embeddings.

```sql
CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));
INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');
```

4. Query the nearest neighbors

   Order by a distance operator and limit the results. This example uses L2 distance; <#> (inner product), <=> (cosine), and <+> (L1) are also supported.

```sql
SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;
```

## When to use it

- Add semantic search or retrieval (RAG) to an app that already runs on Postgres, without standing up a separate vector database
- Store text or image embeddings next to their source rows so you can filter, JOIN, and rank in a single SQL query
- Build recommendation or similar-item features by finding the nearest vectors to a given row
- Scale to large vector sets using HNSW or IVFFlat indexes for fast approximate nearest neighbor search
