Skip to main content

Overview

PGVectorStore provides vector storage and similarity search using PostgreSQL with the pgvector extension. It supports multiple PostgreSQL client libraries and advanced filtering.

Installation

Basic Usage

Constructor Options

Using Client Configuration

pg.ClientConfig
PostgreSQL client configuration object. See node-postgres documentation

Using Existing Client

pg.Client | pg.PoolClient | Sql | VercelPool
Existing PostgreSQL client instance (pg, postgres, or @vercel/postgres)
boolean
Whether to connect the client. Set to false if already connected

Common Options

string
default:"public"
PostgreSQL schema name
string
default:"llamaindex_embedding"
Table name for storing vectors
number
default:1536
Vector dimensions (must match embedding model)
boolean
default:true
Automatically create schema, table, and indexes

Supported PostgreSQL Clients

node-postgres (pg)

postgres

@vercel/postgres

Database Setup

Install pgvector Extension

The vector store will automatically:
  • Create the schema if it doesn’t exist
  • Create the table with appropriate columns
  • Create indexes on external_id and collection

Manual Table Creation

Collections

Organize vectors using collections:

Querying

Basic Query

Metadata Filtering

Supported Filter Operators

PGVectorStore supports extensive filtering:
  • EQ (=) - Equal
  • NE (!=) - Not equal
  • GT (>) - Greater than
  • GTE (>=) - Greater than or equal
  • LT (<) - Less than
  • LTE (<=) - Less than or equal
  • IN (= ANY) - Value in array
  • NIN (!= ANY) - Value not in array
  • CONTAINS (@>) - JSONB contains
  • ANY (?|) - Any of the array elements exist
  • ALL (?&) - All of the array elements exist
  • IS_EMPTY - Field is null or empty
  • TEXT_MATCH - Text pattern matching (LIKE)

Advanced Filtering Examples

Managing Data

Add Documents

Delete by Document ID

Access Database Client

Complete Example

Distance Metrics

PGVectorStore uses cosine distance (<=>) by default. PostgreSQL with pgvector supports:
  • <=> - Cosine distance
  • <-> - L2 distance (Euclidean)
  • <#> - Inner product

Performance Optimization

Indexing

For better performance on large datasets, consider adding vector indexes:

Connection Pooling

Best Practices

  1. Use connection pooling: Reuse database connections
  2. Match dimensions: Ensure vector dimensions match embedding model
  3. Create indexes: Add HNSW or IVFFlat indexes for large datasets
  4. Use collections: Organize data by collection for easy management
  5. Monitor performance: Track query performance and optimize
  6. Regular maintenance: Run VACUUM and ANALYZE on tables

Troubleshooting

pgvector Extension Not Found

Dimension Mismatch

Ensure embedding dimensions match:

See Also