{"id":15442337,"url":"https://github.com/langchain-ai/langchain-postgres","last_synced_at":"2025-05-15T01:08:40.527Z","repository":{"id":232202309,"uuid":"783743154","full_name":"langchain-ai/langchain-postgres","owner":"langchain-ai","description":"LangChain abstractions backed by Postgres Backend","archived":false,"fork":false,"pushed_at":"2025-05-08T21:49:23.000Z","size":938,"stargazers_count":181,"open_issues_count":67,"forks_count":77,"subscribers_count":10,"default_branch":"main","last_synced_at":"2025-05-08T22:19:50.316Z","etag":null,"topics":["langchain","langchain-python","postgres","postgresql"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/langchain-ai.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"security.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-04-08T13:38:40.000Z","updated_at":"2025-05-08T20:43:52.000Z","dependencies_parsed_at":"2024-05-14T05:22:25.446Z","dependency_job_id":"0c7b389c-3a2b-4ccb-ad09-34a231a4bebd","html_url":"https://github.com/langchain-ai/langchain-postgres","commit_stats":{"total_commits":66,"total_committers":9,"mean_commits":7.333333333333333,"dds":"0.25757575757575757","last_synced_commit":"d69cc6befb5b9c88382a75942f55d6001bb2219f"},"previous_names":["langchain-ai/langchain-postgres"],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/langchain-ai%2Flangchain-postgres","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/langchain-ai%2Flangchain-postgres/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/langchain-ai%2Flangchain-postgres/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/langchain-ai%2Flangchain-postgres/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/langchain-ai","download_url":"https://codeload.github.com/langchain-ai/langchain-postgres/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254254042,"owners_count":22039792,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["langchain","langchain-python","postgres","postgresql"],"created_at":"2024-10-01T19:26:42.910Z","updated_at":"2025-05-15T01:08:35.518Z","avatar_url":"https://github.com/langchain-ai.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# langchain-postgres\n\n[![Release Notes](https://img.shields.io/github/release/langchain-ai/langchain-postgres)](https://github.com/langchain-ai/langchain-postgres/releases)\n[![CI](https://github.com/langchain-ai/langchain-postgres/actions/workflows/ci.yml/badge.svg)](https://github.com/langchain-ai/langchain-postgres/actions/workflows/ci.yml)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![Twitter](https://img.shields.io/twitter/url/https/twitter.com/langchainai.svg?style=social\u0026label=Follow%20%40LangChainAI)](https://twitter.com/langchainai)\n[![](https://dcbadge.vercel.app/api/server/6adMQxSpJS?compact=true\u0026style=flat)](https://discord.gg/6adMQxSpJS)\n[![Open Issues](https://img.shields.io/github/issues-raw/langchain-ai/langchain-postgres)](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 supports the [asyncpg](https://github.com/MagicStack/asyncpg) and [psycogp3](https://www.psycopg.org/psycopg3/) drivers.\n\n## Installation\n\n```bash\npip install -U langchain-postgres\n```\n\n## Vectorstore\n\n\u003e [!WARNING]\n\u003e In v0.0.14+, `PGVector` is deprecated. Please migrate to `PGVectorStore`\n\u003e for improved performance and manageability.\n\u003e See the [migration guide](https://github.com/langchain-ai/langchain-postgres/blob/main/examples/migrate_pgvector_to_pgvectorstore.ipynb) for details on how to migrate from `PGVector` to `PGVectorStore`.\n\n### Documentation\n\n* [Quickstart](https://github.com/langchain-ai/langchain-postgres/blob/main/examples/pg_vectorstore.ipynb)\n* [How-to](https://github.com/langchain-ai/langchain-postgres/blob/main/examples/pg_vectorstore_how_to.ipynb)\n\n### Example\n\n```python\nfrom langchain_core.documents import Document\nfrom langchain_core.embeddings import DeterministicFakeEmbedding\nfrom langchain_postgres import PGEngine, PGVectorStore\n\n# Replace the connection string with your own Postgres connection string\nCONNECTION_STRING = \"postgresql+psycopg3://langchain:langchain@localhost:6024/langchain\"\nengine = PGEngine.from_connection_string(url=CONNECTION_STRING)\n\n# Replace the vector size with your own vector size\nVECTOR_SIZE = 768\nembedding = DeterministicFakeEmbedding(size=VECTOR_SIZE)\n\nTABLE_NAME = \"my_doc_collection\"\n\nengine.init_vectorstore_table(\n    table_name=TABLE_NAME,\n    vector_size=VECTOR_SIZE,\n)\n\nstore = PGVectorStore.create_sync(\n    engine=engine,\n    table_name=TABLE_NAME,\n    embedding_service=embedding,\n)\n\ndocs = [\n    Document(page_content=\"Apples and oranges\"),\n    Document(page_content=\"Cars and airplanes\"),\n    Document(page_content=\"Train\")\n]\n\nstore.add_documents(docs)\n\nquery = \"I'd like a fruit.\"\ndocs = store.similarity_search(query)\nprint(docs)\n```\n\n\u003e [!TIP]\n\u003e All synchronous functions have corresponding asynchronous functions\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## Google Cloud Integrations\n\n[Google Cloud](https://python.langchain.com/docs/integrations/providers/google/) provides Vector Store, Chat Message History, and Data Loader integrations for [AlloyDB](https://cloud.google.com/alloydb) and [Cloud SQL](https://cloud.google.com/sql) for PostgreSQL databases via the following PyPi packages:\n\n* [`langchain-google-alloydb-pg`](https://github.com/googleapis/langchain-google-alloydb-pg-python)\n\n* [`langchain-google-cloud-sql-pg`](https://github.com/googleapis/langchain-google-cloud-sql-pg-python)\n\nUsing the Google Cloud integrations provides the following benefits:\n\n- **Enhanced Security**: Securely connect to Google Cloud databases utilizing IAM for authorization and database authentication without needing to manage SSL certificates, configure firewall rules, or enable authorized networks.\n- **Simplified and Secure Connections:** Connect to Google Cloud databases effortlessly using the instance name instead of complex connection strings. The integrations creates a secure connection pool that can be easily shared across your application using the `engine` object.\n\n| Vector Store             | Metadata filtering | Async support  | Schema Flexibility | Improved metadata handling | Hybrid Search |\n|--------------------------|--------------------|----------------|--------------------|----------------------------|---------------|\n| Google AlloyDB           |          ✓         |        ✓       |         ✓          |             ✓              |       ✗       |\n| Google Cloud SQL Postgres|          ✓         |        ✓       |         ✓          |             ✓              |       ✗       |\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flangchain-ai%2Flangchain-postgres","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flangchain-ai%2Flangchain-postgres","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flangchain-ai%2Flangchain-postgres/lists"}