LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
  • LangGraph Checkpoint
    Checkpoint Postgres
    Store Postgres
    Checkpoint SQLite
    LangGraph Prebuilt
    LangGraph CLI
    LangGraph SDK
    LangGraph Supervisor
    LangGraph Swarm
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    LangGraph Checkpoint
    Checkpoint Postgres
    Store Postgres
    Checkpoint SQLite
    LangGraph Prebuilt
    LangGraph CLI
    LangGraph SDK
    LangGraph Supervisor
    LangGraph Swarm
    Language
    Theme
    Pythonlanggraph.store.postgresbasePostgresStore
    Class●Since v1.0

    PostgresStore

    Copy
    PostgresStore(
      self,
      conn: _pg_internal.
    Conn
    ,
    *
    ,
    pipe
    :
    Pipeline
    |
    None
    =
    None
    ,
    deserializer
    :
    Callable
    [
    [
    bytes
    |
    orjson
    .
    Fragment
    ]
    ,
    dict
    [
    str
    ,
    Any
    ]
    ]
    |
    None
    =
    None
    ,
    index
    :
    PostgresIndexConfig
    |
    None
    =
    None
    ,
    ttl
    :
    TTLConfig
    |
    None
    =
    None
    )

    Bases

    BaseStoreBasePostgresStore[_pg_internal.Conn]

    Used in Docs

    • Memory

    Constructors

    constructor
    __init__
    NameType
    conn_pg_internal.Conn
    pipePipeline | None
    deserializerCallable[[bytes | orjson.Fragment], dict[str, Any

    Attributes

    attribute
    supports_ttl: bool
    attribute
    conn: conn
    attribute
    pipe: pipe
    attribute
    supports_pipeline
    attribute
    lock
    attribute
    index_config: index
    attribute
    embeddings: None
    attribute
    ttl_config: ttl

    Methods

    method
    from_conn_string
    method
    sweep_ttl
    method
    start_ttl_sweeper
    method
    stop_ttl_sweeper
    method
    batch
    method
    abatch
    method
    setup

    Inherited fromBaseStore(langchain_core)

    Methods

    MmgetMamgetMmsetMamsetMmdeleteMamdeleteMyield_keysMayield_keys

    Inherited fromBasePostgresStore

    Attributes

    AMIGRATIONS: MIGRATIONSAVECTOR_MIGRATIONS: VECTOR_MIGRATIONS
    View source on GitHub

    Postgres-backed store with optional vector search using pgvector.

    Examples

    Basic setup and usage:

    from langgraph.store.postgres import PostgresStore
    from psycopg import Connection
    
    conn_string = "postgresql://user:pass@localhost:5432/dbname"
    
    # Using direct connection
    with Connection.connect(conn_string) as conn:
        store = PostgresStore(conn)
        store.setup() # Run migrations. Done once
    
        # Store and retrieve data
        store.put(("users", "123"), "prefs", {"theme": "dark"})
        item = store.get(("users", "123"), "prefs")

    Or using the convenient from_conn_string helper:

    from langgraph.store.postgres import PostgresStore
    
    conn_string = "postgresql://user:pass@localhost:5432/dbname"
    
    with PostgresStore.from_conn_string(conn_string) as store:
        store.setup()
    
        # Store and retrieve data
        store.put(("users", "123"), "prefs", {"theme": "dark"})
        item = store.get(("users", "123"), "prefs")

    Vector search using LangChain embeddings:

    from langchain.embeddings import init_embeddings
    from langgraph.store.postgres import PostgresStore
    
    conn_string = "postgresql://user:pass@localhost:5432/dbname"
    
    with PostgresStore.from_conn_string(
        conn_string,
        index={
            "dims": 1536,
            "embed": init_embeddings("openai:text-embedding-3-small"),
            "fields": ["text"]  # specify which fields to embed. Default is the whole serialized value
        }
    ) as store:
        store.setup() # Do this once to run migrations
    
        # Store documents
        store.put(("docs",), "doc1", {"text": "Python tutorial"})
        store.put(("docs",), "doc2", {"text": "TypeScript guide"})
        store.put(("docs",), "doc2", {"text": "Other guide"}, index=False) # don't index
    
        # Search by similarity
        results = store.search(("docs",), query="programming guides", limit=2)

    Note:

    Semantic search is disabled by default. You can enable it by providing an index configuration when creating the store. Without this configuration, all index arguments passed to put or aputwill have no effect.

    Warning:

    Make sure to call setup() before first use to create necessary tables and indexes. The pgvector extension must be available to use vector search.

    Note:

    If you provide a TTL configuration, you must explicitly call start_ttl_sweeper() to begin the background thread that removes expired items. Call stop_ttl_sweeper() to properly clean up resources when you're done with the store.

    ]] |
    None
    indexPostgresIndexConfig | None
    ttlTTLConfig | None

    Create a new PostgresStore instance from a connection string.

    Delete expired store items based on TTL.

    Periodically delete expired store items based on TTL.

    Stop the TTL sweeper thread if it's running.

    Set up the store database.

    This method creates the necessary tables in the Postgres database if they don't already exist and runs database migrations. It MUST be called directly by the user the first time the store is used.