Strands PostgreSQL Session Manager
A production-ready session manager for Strands Agents that uses PostgreSQL for persistent storage. This enables agents to maintain conversation history and state with full ACID guarantees, even in distributed environments.
Tested with Strands Agents 1.55.1 and PostgreSQL 14+ (local, RDS, Cloud SQL, Azure Database, and Supabase).
Features
- Persistent Sessions: Store agent conversations and state in PostgreSQL with full ACID guarantees
- Distributed Ready: Share sessions across multiple application instances
- JSONB Storage: Native PostgreSQL JSONB support for complex data structures (state, conversation manager, messages)
- Multi-agent State: Persist Graph and Swarm execution state in PostgreSQL
- Referential Integrity: CASCADE deletes ensure data consistency
- Production Tested: Battle-tested with 18 unit tests + 4 integration tests (100% passing)
- Analytics Ready: SQL queries for conversation analysis and reporting
Installation
pip install strands-postgresql-session-manager
Quick Start
from strands import Agent
from strands_postgresql_session_manager import PostgresSessionManager
from sqlmodel import create_engine, SQLModel
# Create PostgreSQL engine
engine = create_engine("postgresql://user:password@localhost:5432/agents_db")
# Create tables for a new database
SQLModel.metadata.create_all(engine)
# Create session manager with unique session ID
session_manager = PostgresSessionManager(
session_id="user_123",
engine=engine
)
# Create agent with session manager
agent = Agent(session_manager=session_manager)
# Use agent - all messages are automatically persisted to PostgreSQL
agent("Hello! Tell me about PostgreSQL session storage.")
# The conversation is now stored in PostgreSQL and can be resumed later,
# using the same session_id
For an existing v0.1.0 database, install the migration extra and run the versioned migration before starting the application:
pip install 'strands-postgresql-session-manager[migrations]'
export DATABASE_URL="postgresql://user:password@localhost:5432/agents_db"
alembic -c alembic.ini upgrade head
The migration converts state columns to PostgreSQL JSONB, adds multi-agent state, and enforces message-to-agent cascade deletes.
Storage Structure
The PostgresSessionManager stores data using the following table structure:
sessions (session_id PK, session_type, created_at, updated_at)
└── agents (session_id FK, agent_id, state JSONB, conversation_manager_state JSONB, _internal_state JSONB)
├── messages (session_id FK, agent_id FK, message_id, message JSONB, redact_message JSONB)
└── multi_agents (session_id FK, multi_agent_id, state JSONB)
Foreign keys use ON DELETE CASCADE to ensure referential integrity.
This package uses Strands' repository-based session API. Strands now recommends SnapshotSessionManager with the unified Storage protocol for new single-agent applications; use this package when you need PostgreSQL-backed record-level persistence or Graph/Swarm sessions.
API Reference
PostgresSessionManager
PostgresSessionManager(
session_id: str,
engine: Engine
)
Parameters:
session_id: Unique identifier for the sessionengine: Configured SQLAlchemy/SQLModel engine instance
Methods (Note that these methods are used transparently by Strands):
create_session(session): Create a new sessionread_session(session_id): Retrieve session datadelete_session(session_id): Remove session and all associated data (CASCADE)create_agent(session_id, agent): Store agent in sessionread_agent(session_id, agent_id): Retrieve agent dataupdate_agent(session_id, agent): Update agent statecreate_message(session_id, agent_id, message): Store messageread_message(session_id, agent_id, message_id): Retrieve messageupdate_message(session_id, agent_id, message): Update messagelist_messages(session_id, agent_id, limit=None, offset=0): List all messagescreate_multi_agent(session_id, multi_agent): Store Graph or Swarm stateread_multi_agent(session_id, multi_agent_id): Retrieve Graph or Swarm stateupdate_multi_agent(session_id, multi_agent): Update Graph or Swarm state
Contributing
Setup
# Clone the repository
git clone https://github.com/macuartin/strands-postgresql-session-manager
cd strands-postgresql-session-manager
# Create virtual environment and install dependencies
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
pip install -e ".[dev]"
Running Tests
# Run unit tests only (default)
pytest tests/test_unit.py -v
# Run with coverage
pytest tests/test_unit.py --cov=src/strands_postgresql_session_manager --cov-report=html
Integration Tests
Integration tests require a running PostgreSQL instance:
# Start PostgreSQL (Docker)
docker run -d --name postgres-test \
-p 5432:5432 \
-e POSTGRES_PASSWORD=test \
-e POSTGRES_USER=test \
-e POSTGRES_DB=test \
postgres:14
# Set DATABASE_URL
export DATABASE_URL="postgresql://test:test@localhost:5432/test"
# Run integration tests
pytest tests/test_integration.py -v
# Cleanup
docker stop postgres-test && docker rm postgres-test
Requirements
- Python 3.10+
- PostgreSQL 14+
- strands-agents >= 1.0.0 (tested with 1.55.1)
- sqlmodel >= 0.0.14
- psycopg2-binary >= 2.9.0
License
MIT License - see LICENSE for details.
Metadata
Release files for strands-postgresql-session-manager 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| strands_postgresql_session_manager-0.2.0.tar.gz | 19.2 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| strands_postgresql_session_manager-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 30.8 kB
Release files / strands_postgresql_session_manager-0.2.0.tar.gz
| Download URL | strands_postgresql_session_manager-0.2.0.tar.gz |
|---|---|
| Size | 19.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
55e7aed9fc71a5bee0eade1daabb20fd578d3f98113fa8553bc6b5405d716c34
|
|
BLAKE2b-256 checksum How to use checksums |
e753d7e3b0f7d4c4c2d52932d55ccbe22fa1e10e6e077a9cab08aafc207006c5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 10, 2026.
Transparency logRelease files / strands_postgresql_session_manager-0.2.0-py3-none-any.whl
| Download URL | strands_postgresql_session_manager-0.2.0-py3-none-any.whl |
|---|---|
| Size | 11.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5f6cf83d595fc3bb1974b6a96d113efc311b4ae2883bebc57c5c49098f37f249
|
|
BLAKE2b-256 checksum How to use checksums |
ca1564bc924eaea128c107a972433745d02aebc1c8b84bb124adaf190c5041b9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 10, 2026.
Transparency logSecurity for strands-postgresql-session-manager 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for strands_postgresql_session_manager-0.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 10, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.