langchain-ai/langchain-postgres
PostgreSQL-backed vector retrieval and chat history for LangChain
Automated SPDX check for reference only — not legal advice. Disclaimer
GitHub Star History
Full-history star trajectory from GitHub official history. Switch ranges or hover any point for exact counts.
Evidence-Backed Technical Brief
AI Deep ReviewArchitecture & Core Mechanics
langchain-postgres is an in-process Python SDK, not a standalone retrieval service. The vector path separates table setup (PGEngine), storage and retrieval (PGVectorStore), and embedding generation (an application-supplied embedding service). The application initializes a named table with a fixed vector dimension, then uses the store to add LangChain Document objects and run similarity queries. Sync APIs have async counterparts.
The manifest requires Python 3.11+ and LangChain Core >=1.2.11,<2.0. Dependencies include SQLAlchemy 2+ with asyncio support, asyncpg, psycopg 3 with binary support, psycopg-pool, the pgvector Python package, and NumPy. Chat persistence is a separate path: PostgresChatMessageHistory takes a psycopg connection, a table name, and an application-assigned session UUID. Both paths can use the same database without coupling their APIs or table lifecycles.
Key Capabilities & Differentiators
PGVectorStore provides document insertion and vector similarity search. HybridSearchConfig adds a text retrieval signal to vector search, with reciprocal rank fusion available as the fusion function. The example's 768-dimensional DeterministicFakeEmbedding is a test fixture, not a required dimension or a production model. The table must match the actual embedding output; changes to models or dimensions should be planned around re-embedding and table compatibility.
PostgresChatMessageHistory persists LangChain message types such as system, human, and AI messages in a PostgreSQL table scoped by session UUID. For cloud-specific connectivity, the separate AlloyDB and Cloud SQL integration packages offer IAM and instance-based connections. Both support metadata filtering, async access, and schema flexibility, but neither is listed as supporting hybrid search.
Trade-offs & Limitations
This is a good fit when PostgreSQL is already an operational dependency: retrieval and chat history can use existing database infrastructure, while LangChain gets a reusable adapter. Compared with writing pgvector SQL directly, the SDK reduces integration code but gives up some direct control over query shape, ranking, and database tuning. Hybrid search also needs relevance testing on representative queries; rank fusion is not a guarantee of better results.
Compared with a dedicated vector database, colocating these workloads avoids running another data service, but retrieval and ingestion may compete with transactional work for CPU, memory, I/O, and connections. Test under mixed load before sharing a production instance, especially if retrieval must scale independently. The package is MIT-licensed. For existing PGVector users, account for migration and cutover work: the class has been deprecated since 0.0.14, and a migration notebook targets PGVectorStore.
Hardware & Deployment Guide
Install langchain-postgres into Python 3.11+ and point the application at PostgreSQL with pgvector enabled. The included Compose file starts only the database: pgvector/pgvector:pg16, port 5432:5432, and a named data volume. It creates langchain_test, but the health check connects to langchain; the separate Python example also targets langchain, on port 6024. Align the health check and application DSN with the database name and host port you actually provision. Replace the fixture credentials and review log_statement=all before ingesting real data.
For production, manage schema changes explicitly and size PostgreSQL for vector dimensions, row count, indexes, and query concurrency. Self-hosting the database does not make the pipeline offline if embeddings still use an external service. The 0.0.19 release removes the vulnerable embed_query_inline fallback as a breaking change; smoke-test embedding and retrieval when upgrading.
Python LangChain teams that already operate PostgreSQL and want vector or hybrid retrieval plus session history without adding a separate data service.
You need a turnkey retrieval server, must scale retrieval independently of PostgreSQL, or cannot enable pgvector on your database.
Quick Start & Installation
pip install -U langchain-postgres
Comparison vs. Top RAG, Data & Memory Alternatives
Full comparison →| Repository | Stars | 7d Trend | Stack | License | Core Positioning |
|---|---|---|---|---|---|
| langchain-postgres ★ | 282 | — | Python | MIT | PostgreSQL-backed vector retrieval and chat history for LangChain |
| auto-ontology | 19.1k | +3.7k | Python | Apache-2.0 | An ontology framework that grounds agent-generated SQL in reviewed business defi |
| llama_index | 53.1k | +104 | Python | MIT | A modular Python SDK for document ingestion, indexing, and RAG |
| langextract | 39.5k | +85 | Python | Apache-2.0 | Python SDK for extracting structured data with traceable source spans |
AI Context Briefing (for Claude / Cursor / Windsurf)
Standardized architecture summary & quickstart commands ready to paste into coding agents.
curl -sL https://repoforai.com/repo/langchain-ai/langchain-postgres.md
Active README embeds unlock Hot-Tier 6h Telemetry Refresh automatically (zero pay-to-rank).
Frequently Asked Questions about langchain-postgres
How do vector retrieval and chat persistence fit into an existing LangChain application?
PGEngine.init_vectorstore_table, matching vector_size to your embedding implementation. Construct PGVectorStore with that engine, table, and embedding service, then add and search documents through the store. PostgresChatMessageHistory is separate: it uses a psycopg connection, a history table created once, and a caller-assigned session UUID.Do I need a GPU or a dedicated database server?
DeterministicFakeEmbedding is suitable for a wiring test, not a retrieval-quality test. If you use a local embedding model, size CPU, RAM, and any VRAM for that model and its concurrency. Size PostgreSQL separately for document count, vector dimensions, index footprint, and concurrent queries.Can I self-host this commercially, and what compliance work remains?
When should I choose PGVectorStore over legacy PGVector, direct SQL, or a cloud-specific integration?
PGVectorStore; PGVector has been deprecated since 0.0.14, and existing deployments should test the migration rather than assume a drop-in replacement. Direct pgvector SQL is a better fit when you need full control over queries and ranking and are willing to maintain the LangChain adapter. The separate AlloyDB and Cloud SQL integrations are worth considering when IAM and instance-based connections matter more than hybrid search through this package.Upstream README & Documentation
This profile focuses on independent engineering analysis and live telemetry. Read the full upstream README, changelog, and examples directly on GitHub.
Top 10 Breakout Open-Source AI Repos, Every Thursday
Signal-only digest of fast-growing AI repos, SPDX license alerts, and architecture picks. Zero spam.
Zero spam · 1-click unsubscribe · Privacy Policy