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 connectedCommon 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
- 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(=) - EqualNE(!=) - Not equalGT(>) - Greater thanGTE(>=) - Greater than or equalLT(<) - Less thanLTE(<=) - Less than or equalIN(= ANY) - Value in arrayNIN(!= ANY) - Value not in arrayCONTAINS(@>) - JSONB containsANY(?|) - Any of the array elements existALL(?&) - All of the array elements existIS_EMPTY- Field is null or emptyTEXT_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
- Use connection pooling: Reuse database connections
- Match dimensions: Ensure vector dimensions match embedding model
- Create indexes: Add HNSW or IVFFlat indexes for large datasets
- Use collections: Organize data by collection for easy management
- Monitor performance: Track query performance and optimize
- Regular maintenance: Run VACUUM and ANALYZE on tables