Skip to main content

Database Persistence

PraisonAI Agents supports automatic conversation persistence with multiple database backends.

Quick Start

from praisonaiagents import Agent

# Create agent with database persistence
agent = Agent(
    name="Assistant",
    instructions="You are a helpful assistant.",
    memory={
        "db": "postgresql://localhost/mydb",
        "session_id": "my-session"
    }
)

# All chats are automatically persisted
response = agent.start("Hello!")
print(response)

Installation

pip install "praisonaiagents[tools]"

Supported Backends

CategoryBackends
ConversationPostgreSQL, MySQL, SQLite, SingleStore, Supabase, SurrealDB
Knowledge/VectorQdrant, ChromaDB, Pinecone, Weaviate, LanceDB, Milvus, PGVector, Redis
StateRedis, MongoDB, DynamoDB, Firestore, Upstash, Memory

Docker Setup (Local Development)

# PostgreSQL
docker run -d --name praison-postgres -p 5432:5432 \
    -e POSTGRES_PASSWORD=praison123 \
    -e POSTGRES_DB=praisonai \
    postgres:16

# Qdrant (vector search)
docker run -d --name praison-qdrant -p 6333:6333 qdrant/qdrant

# Redis (state/cache)
docker run -d --name praison-redis -p 6379:6379 redis:7

Usage Examples

PostgreSQL

from praisonaiagents import Agent

agent = Agent(
    name="Assistant",
    memory={
        "db": "postgresql://postgres:praison123@localhost:5432/praisonai",
        "session_id": "user-123-session"
    }
)

response = agent.start("Remember my name is Alice")
# Later, with same session_id, agent remembers the conversation

SQLite (Zero Config)

from praisonaiagents import Agent

agent = Agent(
    name="Assistant",
    memory={
        "db": "conversations.db",
        "session_id": "local-session"
    }
)
response = agent.start("Hello!")

Session Resume

When you use the same session_id, the agent automatically loads previous conversation history:
from praisonaiagents import Agent

# First run
agent = Agent(
    name="Bot",
    memory={
        "db": "mydata.db",
        "session_id": "session-001"
    }
)
agent.start("My favorite color is blue")

# Later run (same session_id)
agent2 = Agent(
    name="Bot",
    memory={
        "db": "mydata.db",
        "session_id": "session-001"
    }
)
response = agent2.start("What's my favorite color?")
# Agent remembers: "Your favorite color is blue"

Best Practices

  1. Use consistent session_ids - Same session_id = same conversation thread
  2. Use environment variables - Don’t hardcode credentials
import os
from praisonaiagents import Agent

agent = Agent(
    name="Assistant",
    memory={"db": os.getenv("DATABASE_URL")}
)

Default Session ID

If you don’t provide a session_id, PraisonAI generates one automatically based on the current hour (UTC):
Format: YYYYMMDDHH-{agent_hash}
Example: 2025122414-37812c
from praisonaiagents import Agent

# Auto session_id (per-hour)
agent = Agent(name="Bot", memory={"db": "mydata.db"})

# Explicit session_id (recommended for continuity)
agent = Agent(
    name="Bot",
    memory={
        "db": "mydata.db",
        "session_id": "user-123-thread-1"
    }
)

CLI Commands

# Check database connectivity
praisonai persistence doctor

# Export session to file
praisonai persistence export --session-id my-session --output backup.jsonl

# Import session from file
praisonai persistence import --file backup.jsonl

# Check schema status
praisonai persistence status

# Apply migrations
praisonai persistence migrate

Environment Variables

export PRAISON_CONVERSATION_URL=postgresql://localhost:5432/praisonai
export PRAISON_STATE_URL=redis://localhost:6379
export PRAISON_KNOWLEDGE_URL=http://localhost:6333