PostgreSQL Persistence
Production SQL persistence with JSONB, connection pooling, and serverless support
PostgreSQL persists agent conversations with JSONB metadata, connection pooling, and automatic serverless retry.
from praisonaiagents import Agent, MemoryConfig
from praisonaiagents import db
agent = Agent(
name="ProductionBot",
instructions="You are a helpful assistant.",
memory=MemoryConfig(
db=db(database_url="postgresql://user:pass@localhost:5432/praisonai"),
session_id="prod-session",
))
agent.start("Hello — save this to PostgreSQL")The user talks to production agents; PostgreSQL stores conversations with JSONB metadata.
graph LR
Agent[🤖 Agent] --> Store[🗃️ Postgres Store]
Store --> Pool[🔗 Connection Pool]
Pool --> DB[(🐘 PostgreSQL)]
classDef agent fill:#8B0000,stroke:#7C90A0,color:#fff
classDef store fill:#189AB4,stroke:#7C90A0,color:#fff
classDef database fill:#10B981,stroke:#7C90A0,color:#fff
class Agent agent
class Store,Pool store
class DB databaseQuick Start
- Simple Usage
bash pip install psycopg2-binary praisonaipython from praisonaiagents import Agent, MemoryConfig from praisonaiagents import db agent = Agent( name="ProductionBot", memory=MemoryConfig( db=db(database_url="postgresql://user:pass@localhost:5432/praisonai"), session_id="session-1", )) agent.start("Hello!") - With Configuration
python from praisonai.persistence.conversation.postgres import PostgresConversationStore store = PostgresConversationStore( url="postgresql://user:pass@localhost:5432/praisonai", schema="public", table_prefix="praison_", auto_create_tables=True, pool_size=10, max_retries=3, retry_delay=0.5, )
How It Works
sequenceDiagram
participant Agent
participant Store as PostgresConversationStore
participant PostgreSQL
Agent->>Store: save message
Store->>PostgreSQL: CREATE TABLE IF NOT EXISTS
Store->>PostgreSQL: INSERT (JSONB metadata)
PostgreSQL-->>Agent: persisted| Feature | Description |
|---|---|
| JSONB columns | Rich metadata queries on session data |
| Serverless retry | Auto SSL and retry for Neon, CockroachDB, Xata |
| Schema support | Isolate tables with a PostgreSQL schema |
Init-Failure Handling
PostgreSQL initialization distinguishes transient outages from fatal misconfiguration so a typo never hides behind a cooldown window.
- Transient (memoized for the cooldown, retried after):
ConnectionError,TimeoutError,OSError, and DBAPI operational errors matched by class name across the MRO (for examplepsycopg2.OperationalError,psycopg.errors.OperationalError) — no optional-dependency import needed. - Fatal (raised immediately on every call):
PermissionError, credential/typo/misconfiguration errors,ValueError/TypeErrorfrom bad config. KeyboardInterrupt/SystemExit/ otherBaseExceptions bypass the handler entirely and never poison the memoized state.
graph LR
Init[⚙️ DB Init] --> Check{Error type?}
Check -->|Transient<br/>Connection/Timeout/OSError<br/>DBAPI OperationalError| Memo[💤 Memoize for cooldown]
Check -->|Fatal<br/>PermissionError / config| Raise[🚨 Raise now]
Check -->|Success| Ready[✅ Initialized]
classDef init fill:#6366F1,stroke:#7C90A0,color:#fff
classDef check fill:#F59E0B,stroke:#7C90A0,color:#fff
classDef memo fill:#189AB4,stroke:#7C90A0,color:#fff
classDef raise fill:#8B0000,stroke:#7C90A0,color:#fff
classDef ready fill:#10B981,stroke:#7C90A0,color:#fff
class Init init
class Check check
class Memo memo
class Raise raise
class Ready readyConfiguration Options
| Option | Type | Default | Description |
|---|---|---|---|
url | str | None | Full connection URL (overrides individual options) |
host | str | "localhost" | Database host |
port | int | 5432 | Database port |
database | str | "praisonai" | Database name |
user | str | "postgres" | Database user |
password | str | "" | Database password |
schema | str | "public" | PostgreSQL schema |
table_prefix | str | "praison_" | Prefix for table names |
auto_create_tables | bool | True | Create tables automatically |
pool_size | int | 5 | Connection pool size |
max_retries | int | 3 | Retries on connection errors (serverless cold-start) |
retry_delay | float | 0.5 | Base delay between retries in seconds |
URL formats
db(database_url="postgresql://user:pass@localhost:5432/praisonai")
db(database_url="postgresql://user:pass@host:5432/db?sslmode=require")Aliases: neon, cockroachdb, crdb, xata resolve to the postgres backend. For async, use create_conversation_store("async_postgres", ...).
Docker Setup
docker run -d --name praison-postgres -p 5432:5432 \
-e POSTGRES_PASSWORD=praison123 \
-e POSTGRES_DB=praisonai \
postgres:16Connection String Format
postgresql://username:password@host:port/databaseExample:
from praisonaiagents import Agent
agent = Agent(
name="Bot",
memory={"db": "postgresql://user:password@host:5432/mydb"}
)Credentials with special characters “just work” through the structured constructor:
from praisonaiagents import Agent, MemoryConfig
from praisonaiagents.db import PostgresDB
# Password contains '@' — the structured constructor handles the escaping.
agent = Agent(
name="Assistant",
memory=MemoryConfig(
session_id="my-session",
db=PostgresDB(
host="localhost",
port=5432,
database="praisonai",
user="postgres",
password="p@ss/w0rd!",
),
),
)Environment Variables
export PRAISON_CONVERSATION_URL="postgresql://postgres:praison123@localhost:5432/praisonai"import os
from praisonaiagents import Agent
agent = Agent(
name="Assistant",
memory={"db": os.getenv("PRAISON_CONVERSATION_URL")}
)CLI
# Validate connection
praisonai persistence doctor \
--conversation-url "postgresql://postgres:praison123@localhost/praisonai"
# Run with persistence
praisonai persistence run \
--conversation-url "postgresql://postgres:praison123@localhost/praisonai" \
--session-id "cli-session" \
"Hello!"Best Practices
Use db() for most agents
Agent( memory=MemoryConfig( db=db(database_url="postgresql://..."), )) is the simplest path — store wiring is automatic.
Enable SSL in production
Set sslmode=require in the URL for encrypted connections.
Use serverless aliases for Neon/Xata
Point at Neon or Xata URLs — SSL and retry are applied automatically.
Resume sessions after restart
Reuse the same session_id — conversation history loads on the next agent.start().
Cold-start bursts are safe with async_postgres
create_conversation_store("async_postgres", ...) guards pool creation with double-checked locking, so concurrent first-requests under FastAPI share one connection pool instead of building and leaking duplicates.