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

# Memory with PostgreSQL

> Store user memories in PostgreSQL by passing PostgresDb as the agent's database.

Store and recall user memories with an agent backed by PostgreSQL.

## Code

```python mem-postgres-memory.py theme={null}
from agno.agent import Agent
from agno.db.postgres import PostgresDb

# Setup Postgres
db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
db = PostgresDb(db_url=db_url)

agent = Agent(
    db=db,
    update_memory_on_run=True,
)

agent.print_response("My name is John Doe and I like to play basketball on the weekends.")
agent.print_response("What do I do on weekends?")
```

## Usage

<Snippet file="create-venv-step.mdx" />

**1. Set environment variables**

```bash macOS / Linux theme={null}
export OPENAI_API_KEY=xxx
```

```powershell Windows theme={null}
$Env:OPENAI_API_KEY="xxx"
```

**2. Install dependencies**

```bash theme={null}
uv pip install -U agno openai sqlalchemy 'psycopg[binary]'
```

<Snippet file="run-pgvector-step.mdx" />

**3. Run Example**

```bash Mac/Linux theme={null}
python mem-postgres-memory.py
```

```bash Windows theme={null}
python mem-postgres-memory.py
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.