> ## Documentation Index
> Fetch the complete documentation index at: https://docs.praison.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Database Persistence

> Enable automatic conversation persistence in 2 lines of code

# Database Persistence

PraisonAI Agents supports automatic conversation persistence with multiple database backends.

## Quick Start

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
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

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
pip install "praisonaiagents[tools]"
```

## Supported Backends

| Category             | Backends                                                               |
| -------------------- | ---------------------------------------------------------------------- |
| **Conversation**     | PostgreSQL, MySQL, SQLite, SingleStore, Supabase, SurrealDB            |
| **Knowledge/Vector** | Qdrant, ChromaDB, Pinecone, Weaviate, LanceDB, Milvus, PGVector, Redis |
| **State**            | Redis, MongoDB, DynamoDB, Firestore, Upstash, Memory                   |

## Docker Setup (Local Development)

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
# 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

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
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)

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
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:

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
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

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
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
```

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
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

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
# 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

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
export PRAISON_CONVERSATION_URL=postgresql://localhost:5432/praisonai
export PRAISON_STATE_URL=redis://localhost:6379
export PRAISON_KNOWLEDGE_URL=http://localhost:6333
```
