Install & Compatibility
Where this runs
tested against v0.0.17 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.915 runs
installs and imports cleanly · install 0.0s · import 3.340s · 191.9MB
glibcpy 3.10–3.915 runs
installs and imports cleanly · install 12.0s · import 3.088s · 199MB
199MB installed
● package 199MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
PGEngine
✓ from langchain_postgres import PGEngine
PGVectorStore
✓ from langchain_postgres import PGVectorStore
PostgresChatMessageHistory
✓ from langchain_postgres.chat_message_histories import PostgresChatMessageHistory
PostgresSaver
✓ from langgraph.checkpoint.postgres import PostgresSaver
✗ from langchain_postgres import PostgresSaver
PostgresSaver for LangGraph checkpoints is located in `langgraph.checkpoint.postgres`, not directly in `langchain_postgres`.
PGVector
✓ from langchain_postgres.vectorstores import PGVector
✗ from langchain_postgres import PGVector
As of v0.0.14+, `PGVector` is deprecated. Migrate to `PGVectorStore` for improved performance and manageability.
This quickstart demonstrates how to set up `PGEngine` for connecting to PostgreSQL, initialize a `PGVectorStore` for document embedding and similarity search, and use `PostgresChatMessageHistory` for persisting chat messages. It uses `DeterministicFakeEmbedding` for demonstration purposes; in a real application, you would replace this with an actual embedding model. Remember to set your `POSTGRES_CONNECTION_STRING` environment variable.
import os
from langchain_core.documents import Document
from langchain_core.embeddings import DeterministicFakeEmbedding
from langchain_postgres import PGEngine, PGVectorStore
# Replace with your PostgreSQL connection string
CONNECTION_STRING = os.environ.get('POSTGRES_CONNECTION_STRING', 'postgresql+psycopg://langchain:langchain@localhost:6024/langchain')
# Initialize PGEngine
engine = PGEngine.from_connection_string(url=CONNECTION_STRING)
# Define vector size and embedding service
VECTOR_SIZE = 768 # Adjust based on your embedding model
embedding = DeterministicFakeEmbedding(size=VECTOR_SIZE)
TABLE_NAME = "my_doc_collection"
# Initialize the vector store table (if it doesn't exist)
engine.init_vectorstore_table(
table_name=TABLE_NAME,
vector_size=VECTOR_SIZE,
)
# Create a synchronous PGVectorStore instance
store = PGVectorStore.create_sync(
engine=engine,
table_name=TABLE_NAME,
embedding_service=embedding,
)
# Add documents
docs = [
Document(page_content="Apples and oranges"),
Document(page_content="Cars and airplanes"),
Document(page_content="Dogs and cats"),
]
store.add_documents(docs)
# Perform a similarity search
query = "fruits"
results = store.similarity_search(query, k=1)
print(f"Similarity search for '{query}': {results[0].page_content}")
# Example for Chat Message History
from langchain_postgres.chat_message_histories import PostgresChatMessageHistory
import uuid
session_id = str(uuid.uuid4())
chat_history = PostgresChatMessageHistory(session_id=session_id, table_name="chat_messages", connection=engine.get_connection())
chat_history.add_user_message("Hello LangChain Postgres!")
chat_history.add_ai_message("Hi there!")
print(f"Chat history for session {session_id}: {chat_history.messages}")
Upgrade
Version history
0.0.17latest on PyPI · released Feb 17, 2026
Audit
Dependencies
langchain-corerequiredCore LangChain abstractions are utilized by this integration package.
psycopgoptionalDefault PostgreSQL driver (Psycopg 3) for synchronous operations.
asyncpgoptionalAsynchronous PostgreSQL driver.
sqlalchemyrequiredUsed for database abstraction and connection management.
psycopg-pooloptionalConnection pooling for psycopg.
pgvectorrequiredPostgreSQL extension for vector similarity search, integral to PGVectorStore.
numpyrequiredNumerical operations, potentially for embedding handling.