pgvector /pgvector

Open-source vector similarity search for Postgres

★ 23,850⑂ 1,415👁 139⑃ 202● 874▤ 0
CNOASSERTIONCreated 2021-04-20
+9
日增长
+90
周增长
23,850
stars
1,415
forks
139
watchers
202
prs
874
issues
0
releases

README

pgvector

Open-source vector similarity search for Postgres

Store your vectors with the rest of your data. Supports:

- exact and approximate nearest neighbor search
- single-precision, half-precision, binary, and sparse vectors
- L2 distance, inner product, cosine distance, L1 distance, Hamming distance, and Jaccard distance
- any language with a Postgres client

Plus ACID compliance, point-in-time recovery, JOINs, and all of the other great features of Postgres

Have a lot of vectors? Use quantization to scale



 Installation

 Linux and Mac

Compile and install the extension (supports Postgres 13+)

sh
cd /tmp
git clone --branch v0.8.7 https://github.com/pgvector/pgvector.git
cd pgvector
make
make install  may need sudo


See the installation notes if you run into issues

You can also install it with Docker, Homebrew, PGXN, APT, Yum, pkg, APK, or conda-forge, and it comes preinstalled with Postgres.app and many hosted providers. There are also instructions for GitHub Actions.

 Windows

Ensure C++ support in Visual Studio is installed and run x64 Native Tools Command Prompt for VS [version] as administrator. Then use nmake to build:

cmd
set "PGROOT=C:\Program Files\PostgreSQL\19"
cd %TEMP%
git clone --branch v0.8.7 https://github.com/pgvector/pgvector.git
cd pgvector
nmake /F Makefile.win
nmake /F Makefile.win install


See the installation notes if you run into issues

You can also install it with Docker or conda-forge.

 Getting Started

Enable the extension (do this once in each database where you want to use it)

tsql
CREATE EXTENSION vector;


Create a vector column with 3 dimensions

sql
CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));


Insert vectors

sql
INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');


Get the nearest neighbors by L2 distance

sql
SELECT  FROM items ORDER BY embedding  '[3,1,2]' LIMIT 5;


Also supports inner product (), cosine distance (), and L1 distance ()

Note:  returns the negative inner product since Postgres only supports ASC order index scans on operators

 Storing

Create a new table with a vector column

sql
CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));


Or add a vector column to an existing table

sql
ALTER TABLE items ADD COLUMN embedding vector(3);


Also supports half-precision, binary, and sparse vectors

Insert vectors

sql
INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');


Or load vectors in bulk using COPY (example)

sql
COPY items (embedding) FROM STDIN WITH (FORMAT BINARY);


Upsert vectors

sql
INSERT INTO items (id, embedding) VALUES (1, '[1,2,3]'), (2, '[4,5,6]')
    ON CONFLICT (id) DO UPDATE SET embedding = EXCLUDED.embedding;


Update vectors

sql
UPDATE items SET embedding = '[1,2,3]' WHERE id = 1;


Delete vectors

sql
DELETE FROM items WHERE id = 1;


 Querying

Get the nearest neighbors to a vector

sql
SELECT  FROM items ORDER BY embedding  '[3,1,2]' LIMIT 5;


Supported distance functions are:

-  - L2 distance

Top issues

Snapshot: 2026-11-28T10:06:57Z · Data license Apache-2.0 · JSON