Skip to main content

pgVectorDB

pgVectorDB is a PostgreSQL-native vector search and RAG toolkit built on pgvector, SQLAlchemy async, and LangChain-compatible documents. It gives you a fluent query API, multimodal spaces, production indexing controls, SQL diagnostics, reranking, and built-in retrieval evaluation without leaving Postgres.

Current version: 0.0.6
Recommended API: db.query(...) for new application code
Status: Active beta. The fluent API, docs, examples, and notebooks are being prepared for the next OSS release.

New in 0.0.6

  • LanceDB-style fluent query builder: .semantic(), .keyword(), .hybrid(), .trigram(), .where(...), .limit(...), .select(...), and lazy execution with .to_list().
  • First-class BM25 and PostgreSQL full-text search controls through .keyword().bm25(), .keyword().fts(), and hybrid variants.
  • Query diagnostics through explain_plan(), analyze_plan(), and raw PostgreSQL explain_query().
  • Multimodal ranking with .across_spaces(...) for text, numeric, category, and recency signals.
  • Output helpers for Python lists, pandas DataFrames, and PyArrow tables.
  • Reranking support for callable scorers and bundled reranker objects.

Why pgVectorDB

Capability What it gives you
Fluent search API Compose semantic, keyword, hybrid, trigram, filters, tuning, reranking, and output formats with db.query(...).
PostgreSQL-native storage Keep vectors, content, metadata, scalar indexes, full-text search, and query plans in one database.
Recency and multimodal spaces Combine text with numeric, category, and timestamp signals such as freshness or popularity.
Built-in evaluators Measure precision, recall, MRR, NDCG, hit rate, and K-value tradeoffs from retrieved document IDs.
SQL analyzers Use explain_plan(), analyze_plan(), and explain_query() to inspect real PostgreSQL execution behavior.
Optimization tools Build HNSW, IVFFlat, DiskANN, scalar indexes, BM25 indexes, binary quantized indexes, and subvector indexes.
Reranking integrations Use CrossEncoder, HuggingFace, Cohere, or AWS Bedrock rerankers.
LangChain compatibility Insert LangChain Document objects and expose pgVectorDB as a LangChain retriever.

Install

pip install pgvectordb

# Local embeddings
pip install 'pgvectordb[huggingface]'

# AWS Bedrock embeddings
pip install 'pgvectordb[aws]'

# Reranking integrations
pip install 'pgvectordb[rerankers,cohere]'

# Pandas and PyArrow output helpers
pip install 'pgvectordb[dataframe]'

Start PostgreSQL

The repository includes a Docker setup with PostgreSQL 17, pgvector, vectorscale, and pg_textsearch.

cd docker
docker compose up -d

For a manual database, enable the core extensions you need:

CREATE EXTENSION IF NOT EXISTS vector;
CREATE EXTENSION IF NOT EXISTS pg_trgm;

-- Optional, for DiskANN and BM25 features:
CREATE EXTENSION IF NOT EXISTS vectorscale CASCADE;
CREATE EXTENSION IF NOT EXISTS pg_textsearch;

Quickstart

from langchain_core.documents import Document
from langchain_huggingface import HuggingFaceEmbeddings

from pgvectordb import DistanceMetric, IndexType, pgVectorDB


embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")

db = pgVectorDB(
    collection_name="docs",
    embedding_model=embeddings,
    connection_string="postgresql+asyncpg://user:root@localhost:9002/postgres",
    index_type=IndexType.HNSW,
)

await db.initialize()

await db.add_documents([
    Document(
        page_content="pgVectorDB supports fluent PostgreSQL vector search.",
        metadata={"topic": "search", "year": 2024, "tier": "guide"},
    ),
    Document(
        page_content="Built-in evaluators help compare retrieval quality at different K values.",
        metadata={"topic": "evaluation", "year": 2024, "tier": "guide"},
    ),
])

await db.build_index(metric=DistanceMetric.COSINE)

results = await (
    db.query("how do I evaluate vector search quality?")
    .hybrid()
    .where({"year": {"$gte": 2024}})
    .rrf(k=60)
    .ef(100)
    .limit(5)
    .to_list()
)

Fluent Query API

db.query(...) is the recommended entry point for new application code.

# Semantic search
rows = await db.query("retrieval augmented generation").semantic().limit(10).to_list()

# Keyword or BM25 search
rows = await db.query("SOC 2 retention policy").keyword().bm25().limit(10).to_list()

# Hybrid search with reciprocal rank fusion
rows = await db.query("database backup policy").hybrid().rrf(k=60).limit(10).to_list()

# Typo-tolerant trigram search
rows = await db.query("vectro databse").trigram().threshold(0.2).limit(10).to_list()

# Output formats
frame = await db.query("search quality").semantic().limit(20).to_pandas()
arrow_table = await db.query("search quality").semantic().limit(20).to_arrow()

Use vector spaces when ranking should combine more than text similarity.

from pgvectordb import CategorySpace, NumberMode, NumberSpace, RecencySpace, TextSpace, TimeUnit

spaces = [
    TextSpace(name="description", weight=0.65),
    NumberSpace(name="price", mode=NumberMode.SIMILAR, weight=0.15),
    CategorySpace(name="property_type", weight=0.10),
    RecencySpace(name="listed_at", decay_period=7, time_unit=TimeUnit.DAY, weight=0.10),
]

results = await (
    db.query("fresh waterfront home near transit")
    .across_spaces(spaces)
    .where({"city": "Seattle"})
    .limit(10)
    .to_list()
)

Evaluation

The evaluator works with retrieved IDs and ground-truth relevant IDs, so you can compare semantic, keyword, hybrid, reranked, or filtered retrieval strategies.

from pgvectordb import RAGEvaluator

evaluator = RAGEvaluator(k=10)

result = evaluator.evaluate_single_query(
    retrieved_ids=["doc-7", "doc-3", "doc-9", "doc-1"],
    relevant_ids=["doc-3", "doc-9"],
)

print(result.precision_at_k, result.recall_at_k, result.ndcg_at_k)

Diagnostics and Optimization

plan = db.query("filtered vector search").semantic().where({"topic": "postgres"}).explain_plan()

metrics = await (
    db.query("filtered vector search")
    .semantic()
    .where({"topic": "postgres"})
    .limit(10)
    .analyze_plan()
)

await db.create_scalar_index("topic", index_type="btree")
await db.build_bm25_index()
await db.build_index_concurrent(index_type=IndexType.HNSW)

Documentation

Examples

Extension Support

Extension Required Used for
pgvector Yes Vector storage and similarity search.
pg_trgm Yes Typo-tolerant trigram search.
vectorscale Required for DiskANN DiskANN indexes and label-aware vector search.
pg_textsearch Required for BM25 BM25 ranking. Use PostgreSQL full-text search when unavailable.

Development

uv sync --group dev --group docs
uv run pytest
uv run ruff check .
uv run ruff format --check .
uv run pyright
NO_MKDOCS_2_WARNING=true uv run mkdocs build --strict

Useful local checks while working on docs and examples:

# Execute a notebook in-place against the local Docker database
DB_HOST=localhost DB_PORT=9002 DB_USER=user DB_PASSWORD=root DB_NAME=postgres \
DB_CONNECTION_STRING=postgresql+asyncpg://user:root@localhost:9002/postgres \
uv run jupyter nbconvert --to notebook --execute --inplace examples/01_quickstart.ipynb

# Build the docs without writing into site/
NO_MKDOCS_2_WARNING=true uv run mkdocs build --strict --site-dir /tmp/pgvectordb-mkdocs-check

Contributing

Contributions are welcome. Open an issue or pull request with a focused change, a clear explanation, and tests or docs updates when behavior changes.

License

pgVectorDB is released under the MIT License. See LICENSE.

Metadata

Release files for pgvectordb 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pgvectordb 0.0.6
File Size Uploaded Provenance Actions
pgvectordb-0.0.6.tar.gz 631.5 kB Without provenance Details for pgvectordb-0.0.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pgvectordb 0.0.6
File Interpreter ABI Platform Actions
pgvectordb-0.0.6-py3-none-any.whl Python 3 none any Details for pgvectordb-0.0.6-py3-none-any.whl

Total release size: 749.9 kB

Release files / pgvectordb-0.0.6.tar.gz

Download URL pgvectordb-0.0.6.tar.gz
Size 631.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d5a564fdd92f512c0ee6459b1f87a3e566e41e8200fbac2c33cbf59ae536ac58
BLAKE2b-256 checksum
How to use checksums
9e2c5bc96466163a17d8c869e67d1c4ac4fc80cd84a049fb7ebc67321572e5b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release files / pgvectordb-0.0.6-py3-none-any.whl

Download URL pgvectordb-0.0.6-py3-none-any.whl
Size 118.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ffb2fc9e9b6a5c3137d7a5db8209d5dff6ad347ee55bbd3e0a3dc390945cb04e
BLAKE2b-256 checksum
How to use checksums
566137b0b4b0b9886efef123785871ca43f18da3029807e8760b5880da130905
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Security for pgvectordb 0.0.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page