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

# Postgres 存储

Agno 支持使用 PostgreSQL 作为 Agent 的存储后端，使用 `PostgresStorage` 类。

## 用法

### 运行 PgVector

安装 [docker desktop](https://docs.docker.com/desktop/install/mac-install/) 并使用以下命令在端口 **5532** 上运行 **PgVector**：

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

```python postgres_storage_for_agent.py theme={null}
from agno.storage.postgres import PostgresStorage

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

# 使用 Postgres 数据库创建一个存储后端
storage = PostgresStorage(
    # 在 ai.sessions 表中存储会话
    table_name="agent_sessions",
    # db_url: Postgres 数据库 URL
    db_url=db_url,
)

# 將存储添加到 Agent
agent = Agent(storage=storage)
```

## 参数

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

## 开发资源

* 查看 [Cookbook](https://github.com/agno-agi/agno/blob/main/cookbook/storage/postgres_storage/postgres_storage_for_agent.py)
