# langchain-postgres
[](https://github.com/langchain-ai/langchain-postgres/releases)
[](https://github.com/langchain-ai/langchain-postgres/actions/workflows/ci.yml)
[](https://opensource.org/licenses/MIT)
[](https://twitter.com/langchainai)
[](https://discord.gg/6adMQxSpJS)
[](https://github.com/langchain-ai/langchain-postgres/issues)
The `langchain-postgres` package implementations of core LangChain abstractions using `Postgres`.
The package is released under the MIT license.
Feel free to use the abstraction as provided or else modify them / extend them as appropriate for your own application.
## Requirements
The package currently only supports the [psycogp3](https://www.psycopg.org/psycopg3/) driver.
## Installation
```bash
pip install -U langchain-postgres
```
## Change Log
0.0.6:
- Remove langgraph as a dependency as it was causing dependency conflicts.
- Base interface for checkpointer changed in langgraph, so existing implementation would've broken regardless.
## Usage
### ChatMessageHistory
The chat message history abstraction helps to persist chat message history
in a postgres table.
PostgresChatMessageHistory is parameterized using a `table_name` and a `session_id`.
The `table_name` is the name of the table in the database where
the chat messages will be stored.
The `session_id` is a unique identifier for the chat session. It can be assigned
by the caller using `uuid.uuid4()`.
```python
import uuid
from langchain_core.messages import SystemMessage, AIMessage, HumanMessage
from langchain_postgres import PostgresChatMessageHistory
import psycopg
# Establish a synchronous connection to the database
# (or use psycopg.AsyncConnection for async)
conn_info = ... # Fill in with your connection info
sync_connection = psycopg.connect(conn_info)
# Create the table schema (only needs to be done once)
table_name = "chat_history"
PostgresChatMessageHistory.create_tables(sync_connection, table_name)
session_id = str(uuid.uuid4())
# Initialize the chat history manager
chat_history = PostgresChatMessageHistory(
table_name,
session_id,
sync_connection=sync_connection
)
# Add messages to the chat history
chat_history.add_messages([
SystemMessage(content="Meow"),
AIMessage(content="woof"),
HumanMessage(content="bark"),
])
print(chat_history.messages)
```
### Vectorstore
See example for the [PGVector vectorstore here](https://github.com/langchain-ai/langchain-postgres/blob/main/examples/vectorstore.ipynb)
Raw data
{
"_id": null,
"home_page": "https://github.com/langchain-ai/langchain-postgres",
"name": "langchain-postgres",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.9",
"maintainer_email": null,
"keywords": null,
"author": null,
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/4e/20/dc09b62dfe97822841e9c3310e2faa265150ee3783902c251b0d083f8e8c/langchain_postgres-0.0.13.tar.gz",
"platform": null,
"description": "# langchain-postgres\n\n[](https://github.com/langchain-ai/langchain-postgres/releases)\n[](https://github.com/langchain-ai/langchain-postgres/actions/workflows/ci.yml)\n[](https://opensource.org/licenses/MIT)\n[](https://twitter.com/langchainai)\n[](https://discord.gg/6adMQxSpJS)\n[](https://github.com/langchain-ai/langchain-postgres/issues)\n\nThe `langchain-postgres` package implementations of core LangChain abstractions using `Postgres`.\n\nThe package is released under the MIT license. \n\nFeel free to use the abstraction as provided or else modify them / extend them as appropriate for your own application.\n\n## Requirements\n\nThe package currently only supports the [psycogp3](https://www.psycopg.org/psycopg3/) driver.\n\n## Installation\n\n```bash\npip install -U langchain-postgres\n```\n\n## Change Log\n\n0.0.6: \n- Remove langgraph as a dependency as it was causing dependency conflicts.\n- Base interface for checkpointer changed in langgraph, so existing implementation would've broken regardless.\n\n## Usage\n\n### ChatMessageHistory\n\nThe chat message history abstraction helps to persist chat message history \nin a postgres table.\n\nPostgresChatMessageHistory is parameterized using a `table_name` and a `session_id`.\n\nThe `table_name` is the name of the table in the database where \nthe chat messages will be stored.\n\nThe `session_id` is a unique identifier for the chat session. It can be assigned\nby the caller using `uuid.uuid4()`.\n\n```python\nimport uuid\n\nfrom langchain_core.messages import SystemMessage, AIMessage, HumanMessage\nfrom langchain_postgres import PostgresChatMessageHistory\nimport psycopg\n\n# Establish a synchronous connection to the database\n# (or use psycopg.AsyncConnection for async)\nconn_info = ... # Fill in with your connection info\nsync_connection = psycopg.connect(conn_info)\n\n# Create the table schema (only needs to be done once)\ntable_name = \"chat_history\"\nPostgresChatMessageHistory.create_tables(sync_connection, table_name)\n\nsession_id = str(uuid.uuid4())\n\n# Initialize the chat history manager\nchat_history = PostgresChatMessageHistory(\n table_name,\n session_id,\n sync_connection=sync_connection\n)\n\n# Add messages to the chat history\nchat_history.add_messages([\n SystemMessage(content=\"Meow\"),\n AIMessage(content=\"woof\"),\n HumanMessage(content=\"bark\"),\n])\n\nprint(chat_history.messages)\n```\n\n\n### Vectorstore\n\nSee example for the [PGVector vectorstore here](https://github.com/langchain-ai/langchain-postgres/blob/main/examples/vectorstore.ipynb)\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "An integration package connecting Postgres and LangChain",
"version": "0.0.13",
"project_urls": {
"Homepage": "https://github.com/langchain-ai/langchain-postgres",
"Repository": "https://github.com/langchain-ai/langchain-postgres",
"Source Code": "https://github.com/langchain-ai/langchain-postgres/tree/master/langchain_postgres"
},
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "55ef68293f413e2bf289ac17aaab84691ebaccb077709e23cfeaf9f3ee9d05e8",
"md5": "6c9a975f68aa19e08f61ded5f7816b70",
"sha256": "91cb4e62862b1a1f36cdf8462e34990bc112d5824dfb738cab9ca6577cb27cee"
},
"downloads": -1,
"filename": "langchain_postgres-0.0.13-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6c9a975f68aa19e08f61ded5f7816b70",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.9",
"size": 21901,
"upload_time": "2025-02-05T03:20:57",
"upload_time_iso_8601": "2025-02-05T03:20:57.706161Z",
"url": "https://files.pythonhosted.org/packages/55/ef/68293f413e2bf289ac17aaab84691ebaccb077709e23cfeaf9f3ee9d05e8/langchain_postgres-0.0.13-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "4e20dc09b62dfe97822841e9c3310e2faa265150ee3783902c251b0d083f8e8c",
"md5": "95e2bfd40c492b4f35e9f56295da1317",
"sha256": "3a23f95aaeca9bf03af63cf6b9ef1381b6d2a83605179d307a6606b05e335ab1"
},
"downloads": -1,
"filename": "langchain_postgres-0.0.13.tar.gz",
"has_sig": false,
"md5_digest": "95e2bfd40c492b4f35e9f56295da1317",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.9",
"size": 21455,
"upload_time": "2025-02-05T03:20:59",
"upload_time_iso_8601": "2025-02-05T03:20:59.448963Z",
"url": "https://files.pythonhosted.org/packages/4e/20/dc09b62dfe97822841e9c3310e2faa265150ee3783902c251b0d083f8e8c/langchain_postgres-0.0.13.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-05 03:20:59",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "langchain-ai",
"github_project": "langchain-postgres",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "langchain-postgres"
}