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

# Async Postgres for Agent

> Store agent sessions and run history asynchronously in PostgreSQL with AsyncPostgresDb.

`AsyncPostgresDb` stores an Agent's sessions and run history asynchronously in [PostgreSQL](https://www.postgresql.org/). Use async Agent methods such as `arun()` and `aprint_response()`.

## Usage

Install Agno and the Postgres, model, and tool dependencies:

```shell theme={null}
uv pip install -U agno sqlalchemy "psycopg[binary]" openai ddgs
```

```shell theme={null}
export OPENAI_API_KEY="your_openai_api_key_here"
```

### Run PostgreSQL

Install [Docker Desktop](https://docs.docker.com/get-started/get-docker/), then start PostgreSQL with pgvector on port `5532`:

```bash theme={null}
docker run -d \
  -e POSTGRES_DB=ai \
  -e POSTGRES_USER=ai \
  -e POSTGRES_PASSWORD=ai \
  -e PGDATA=/var/lib/postgresql \
  -v pgvolume:/var/lib/postgresql \
  -p 5532:5432 \
  --name pgvector \
  agnohq/pgvector:18
```

```python async_postgres_for_agent.py theme={null}
import asyncio

from agno.agent import Agent
from agno.db.postgres import AsyncPostgresDb
from agno.tools.websearch import WebSearchTools

db_url = "postgresql+psycopg_async://ai:ai@localhost:5532/ai"
db = AsyncPostgresDb(db_url=db_url)

agent = Agent(
    db=db,
    tools=[WebSearchTools()],
    add_history_to_context=True,
    add_datetime_to_context=True,
)


async def main():
    try:
        await agent.aprint_response("How many people live in Canada?")
        await agent.aprint_response("What is their national anthem called?")
    finally:
        await db.close()


if __name__ == "__main__":
    asyncio.run(main())
```

### Run the Example

Save the code above as `async_postgres_for_agent.py`, then run:

```bash theme={null}
python async_postgres_for_agent.py
```

## Parameters

<Snippet file="db-async-postgres-params.mdx" />
