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

# Database Setup

> Configure database backends for persistence

Configure different database backends for agent persistence.

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

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
graph LR
    subgraph "Session Persistence"
        U[📋 Session] --> A[💾 Database]
        A --> O[✅ Restored]
    end

    classDef input fill:#6366F1,stroke:#7C90A0,color:#fff
    classDef process fill:#189AB4,stroke:#7C90A0,color:#fff
    classDef output fill:#10B981,stroke:#7C90A0,color:#fff

    class U input
    class A process
    class O output
```

## Quick Start

<Steps>
  <Step title="Simple Usage">
    Store sessions in a local SQLite file — no setup required.

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

  <Step title="With Configuration">
    Point the same session at PostgreSQL for multi-instance deployments.

    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    import os
    from praisonaiagents import Session

    session = Session(
        session_id="my-session",
        persistence="postgresql",
        connection_string=os.environ["DATABASE_URL"],
    )
    ```
  </Step>
</Steps>

***

## How It Works

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
sequenceDiagram
    participant User
    participant Agent
    participant Session
    participant Database

    User->>Agent: Send a message
    Agent->>Session: Record turn
    Session->>Database: Persist state
    Database-->>Session: Confirmed
    Agent-->>User: Reply
```

***

## SQLite (Default)

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Session

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

## PostgreSQL

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
pip install psycopg2-binary
```

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
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 theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
export DATABASE_URL="postgresql://user:pass@localhost:5432/praisonai"
```

## Redis

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
pip install redis
```

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Session

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

## MongoDB

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
pip install pymongo
```

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Session

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

## Best Practices

<AccordionGroup>
  <Accordion title="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.
  </Accordion>

  <Accordion title="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.
  </Accordion>

  <Accordion title="Match the backend to the access pattern">
    Redis suits short-lived, high-throughput sessions; PostgreSQL suits durable, queryable history; MongoDB suits flexible document state.
  </Accordion>

  <Accordion title="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.
  </Accordion>
</AccordionGroup>

***

## Related

<CardGroup cols={2}>
  <Card title="Persistence Overview" icon="book" href="/docs/guides/persistence/overview">
    Persistence concepts
  </Card>

  <Card title="Session Resume" icon="rotate" href="/docs/guides/persistence/session-resume">
    Resume saved sessions
  </Card>
</CardGroup>
