A LangGraph thread lasts only as long as the checkpointer behind it, and the in-memory saver loses every thread when the Python process stops. A PostgreSQL checkpointer keeps each thread's state in database tables, so a new process can resume the same thread from its last saved state.
PostgresSaver comes from the separate langgraph-checkpoint-postgres package and stores its data in four tables that its setup() method creates. Nodes and state schemas stay as they are, because the saver reaches the graph through the checkpointer argument of compile(), and thread_id values keep identifying each conversation or workflow run.
The database here comes from the Ubuntu 26.04 packages and runs on the same machine as the graph, which needs a Python environment with LangGraph installed. PostgresSaver serves synchronous invoke() calls, while code that awaits ainvoke() needs AsyncPostgresSaver instead.
Steps to configure a PostgreSQL checkpointer in LangGraph:
- Refresh the Ubuntu package index.
$ sudo apt update
- Install PostgreSQL.
$ sudo apt install postgresql
The postgresql package also installs the psql client. If you already run a server, create the role and database there, use its host name in the connection string, and install postgresql-client for psql.
- Create a login role named langgraph_app with a password prompt.
$ sudo -u postgres createuser --pwprompt langgraph_app Enter password for new role: Enter it again:
The packaged postgres administrator has no password and logs in through peer authentication, which is why this runs through sudo. The new role has no superuser rights.
Related: How to create roles in PostgreSQL - Create a database named langgraph owned by langgraph_app.
$ sudo -u postgres createdb --owner=langgraph_app langgraph
Ownership lets langgraph_app create the checkpoint tables. On PostgreSQL 18, setup() stops with permission denied for schema public in a database the role does not own.
- Install the checkpointer and its PostgreSQL driver in the project's activated Python environment.
langgraph-checkpoint-postgres 3.2.0 needs LangGraph 1.0.6 or newer, because earlier releases reject the langgraph-checkpoint version it installs. On an older environment, run the upgrade in the LangGraph install guide first.
$ python -m pip install langgraph-checkpoint-postgres \ "psycopg[binary]"
The binary extra bundles the libpq client library, which the driver cannot run without. Tested with langgraph-checkpoint-postgres 3.2.0, LangGraph 1.2.20, and Python 3.14.
- Store the langgraph_app password in a shell variable by typing it at the silent prompt and pressing Enter.
$ read -rs PGPASSWORD
The shell shows nothing while you type. The password stays out of the connection string, the source file, and the shell history.
- Export PGPASSWORD for Python and psql in the current shell.
$ export PGPASSWORD
Both psycopg and psql read this variable when a connection string has no password.
- Limit checkpoint loading to known-safe types in the current shell.
$ export LANGGRAPH_STRICT_MSGPACK=true
Without this setting, anyone who can write to the checkpoint tables can run Python code when the graph loads a checkpoint. Types declared in the state schema still load, while an undeclared dataclass comes back as a plain dictionary. A deployed application needs PGPASSWORD and this variable in its own environment.
- Create a file named checkpoint_app.py in the project directory with the imports, state schema, and node using your text editor.
- checkpoint_app.py
import sys from operator import add from typing import Annotated from langgraph.checkpoint.postgres import PostgresSaver from langgraph.graph import START, StateGraph from typing_extensions import NotRequired, TypedDict class ThreadState(TypedDict): events: Annotated[list[str], add] event_count: NotRequired[int] def count_events(state: ThreadState) -> dict[str, int]: return {"event_count": len(state["events"])}
The add reducer appends each new event to the list saved for the thread, so a restored thread keeps its earlier events.
- Add the graph builder below count_events().
builder = StateGraph(ThreadState) builder.add_node("count_events", count_events) builder.add_edge(START, "count_events")
The builder stays uncompiled here because compile() needs the open checkpointer. An existing graph keeps its own builder.
- Add the connection string and thread configuration at the end of the file.
DB_URI = "postgresql://langgraph_app@localhost/langgraph" config = {"configurable": {"thread_id": "customer-42"}}
The role and database names come from the earlier steps, and localhost is the server installed above. A server elsewhere needs its own role, host, and database. Every run that reuses customer-42 continues that thread.
- Add the checkpointer block at the end of the file.
with PostgresSaver.from_conn_string(DB_URI) as checkpointer: checkpointer.setup() graph = builder.compile(checkpointer=checkpointer) state = graph.invoke({"events": [sys.argv[1]]}, config) print(state["event_count"], state["events"])
setup() creates the tables on the first run and is safe to keep, since later runs skip migrations already applied. The connection closes when the with block ends, so call the graph inside it.
In an existing graph, replace InMemorySaver() with the checkpointer from this block. A service that handles concurrent requests can pass PostgresSaver a ConnectionPool from psycopg_pool, opened with autocommit=True and row_factory=dict_row.
- Save checkpoint_app.py.
- Run checkpoint_app.py with a first event.
$ python checkpoint_app.py "cart created" 1 ['cart created']
- Verify the saved thread in the checkpoints table.
$ psql postgresql://langgraph_app@localhost/langgraph \ -c 'SELECT thread_id, count(*) FROM checkpoints GROUP BY 1;' thread_id | count -------------+------- customer-42 | 3 (1 row)
One invoke() saves three checkpoints, for the input, the state before the node, and the state after it.
- Verify that a new process resumes the thread by running checkpoint_app.py with a second event.
$ python checkpoint_app.py "paid" 2 ['cart created', 'paid']
The new process prints a count of 2 and both events because it loaded customer-42 from PostgreSQL. With InMemorySaver, the same run prints only paid.