Repolode
pgvector

pgvector/pgvector-python

Python library that adds pgvector vector-type and similarity search support to Django, SQLAlchemy, Psycopg, asyncpg, Peewee and other PostgreSQL drivers.

Stars
1,542
Forks
104
Open issues
9
Last commit
2 days ago

What is it?

pgvector-python is a Python client library that lets applications store and query vectors in PostgreSQL through the pgvector extension. It provides vector, half-precision, bit and sparse vector types, along with distance operators for L2, inner product, cosine, L1, Hamming and Jaccard comparisons.

The package integrates with a range of Python database stacks, including Django, SQLAlchemy, SQLModel, Psycopg 2 and 3, asyncpg, pg8000 and Peewee. Each integration covers the common steps: enabling the extension, adding a vector column, inserting embeddings, running nearest-neighbor queries, and creating HNSW or IVFFlat indexes.

The repository also includes example scripts for retrieval-augmented generation with Ollama, embeddings with OpenAI and Cohere, hybrid search, image search, recommendations and bulk loading with COPY.

Key features

  • 01Supports Django, SQLAlchemy, SQLModel, Psycopg 2/3, asyncpg, pg8000 and Peewee.
  • 02Provides vector, half-precision, bit and sparse vector field types.
  • 03Offers distance operators including L2, inner product, cosine, L1, Hamming and Jaccard.
  • 04Creates HNSW and IVFFlat approximate indexes through each library's API.
  • 05Converts between Python lists, NumPy arrays and SciPy sparse arrays.

Use cases

  • Adding semantic search or RAG retrieval to a Django or SQLAlchemy application backed by PostgreSQL.
  • Storing text or image embeddings from OpenAI, Cohere or SentenceTransformers in Postgres.
  • Building recommendation features from user-item vectors inside an existing Postgres database.

Quick start

  1. 1

    Install the package from PyPI

    pip install pgvector
  2. 2

    Enable the pgvector extension in your PostgreSQL database (Psycopg 3 example)

    conn.execute('CREATE EXTENSION IF NOT EXISTS vector')
  3. 3

    Register the vector type with your connection, then insert and query vectors following the README for your library

    register_vector(conn)

Things to know

  • Requires the pgvector extension installed on your PostgreSQL server.
  • Each database library needs its own setup steps and registration code.

In collections

Topics

FAQ

What is pgvector-python?
Python library that adds pgvector vector-type and similarity search support to Django, SQLAlchemy, Psycopg, asyncpg, Peewee and other PostgreSQL drivers. pgvector-python is a Python client library that lets applications store and query vectors in PostgreSQL through the pgvector extension. It provides vector, half-precision, bit and sparse vector types, along with distance operators for L2, inner product, cosine, L1, Hamming and Jaccard comparisons.
Is pgvector-python free to use? What license does it have?
pgvector-python is open source under the MIT license. Check the license terms before using it in commercial products.
Is pgvector-python still maintained?
Actively maintained. The last commit to pgvector-python was on Nov 5, 2026.
What can I use pgvector-python for?
Adding semantic search or RAG retrieval to a Django or SQLAlchemy application backed by PostgreSQL. Storing text or image embeddings from OpenAI, Cohere or SentenceTransformers in Postgres. Building recommendation features from user-item vectors inside an existing Postgres database.
How do I install pgvector-python?
Install the package from PyPI: pip install pgvector Enable the pgvector extension in your PostgreSQL database (Psycopg 3 example): conn.execute('CREATE EXTENSION IF NOT EXISTS vector') Register the vector type with your connection, then insert and query vectors following the README for your library: register_vector(conn)
Where can I get pgvector-python?
The source code and installation instructions are on GitHub: https://github.com/pgvector/pgvector-python

Summary generated by AI from the project's README. GitHub stats refresh daily.

Related projects