Database Setup

Configure database backends for persistence

Configure different database backends for agent persistence.

python
from praisonaiagents import Agent, Session

session = Session(session_id="my-session", persistence="sqlite", db_path="./data/sessions.db")
agent = Agent(name="Assistant", session=session)

agent.start("Remember this preference for next time.")

The user saves sessions to a database so the same person can reconnect after a restart.

Diagram

Session Persistence

📋 Session

💾 Database

✅ Restored

Quick Start

  1. Simple Usage

    Store sessions in a local SQLite file — no setup required.

    python
    from praisonaiagents import Agent, Session
    
    session = Session(session_id="my-session", persistence="sqlite", db_path="./data/sessions.db")
    agent = Agent(name="Assistant", session=session)
    agent.start("Remember this preference for next time.")
  2. With Configuration

    Point the same session at PostgreSQL for multi-instance deployments.

    python
    import os
    from praisonaiagents import Session
    
    session = Session(
        session_id="my-session",
        persistence="postgresql",
        connection_string=os.environ["DATABASE_URL"],
    )

How It Works

Diagram
DatabaseSessionAgentUserDatabaseSessionAgentUserSend a messageRecord turnPersist stateConfirmedReply

SQLite (Default)

python
from praisonaiagents import Session

session = Session(
    session_id="my-session",
    persistence="sqlite",
    db_path="./data/sessions.db"
)

PostgreSQL

bash
pip install psycopg2-binary
python
from praisonaiagents import Session

session = Session(
    session_id="my-session",
    persistence="postgresql",
    connection_string="postgresql://user:pass@localhost:5432/praisonai"
)

Or via environment variable:

bash
export DATABASE_URL="postgresql://user:pass@localhost:5432/praisonai"

Redis

bash
pip install redis
python
from praisonaiagents import Session

session = Session(
    session_id="my-session",
    persistence="redis",
    redis_url="redis://localhost:6379/0"
)

MongoDB

bash
pip install pymongo
python
from praisonaiagents import Session

session = Session(
    session_id="my-session",
    persistence="mongodb",
    mongodb_url="mongodb://localhost:27017/praisonai"
)

Best Practices

Start with SQLite in development

persistence="sqlite" needs no server and stores everything in a single file. Switch to a networked backend only when multiple instances share sessions.

Use a connection string for shared backends

PostgreSQL, Redis, and MongoDB accept a URL. Keep credentials in DATABASE_URL rather than hard-coding them in the Session call.

Match the backend to the access pattern

Redis suits short-lived, high-throughput sessions; PostgreSQL suits durable, queryable history; MongoDB suits flexible document state.

Reuse one session_id to resume

The session_id is the key that ties writes together. Pass the same ID on reconnect to continue the earlier conversation.


Related