> ## 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

**PostgresTools** 使 Agent 能够与 PostgreSQL 数据库进行交互。

## 先决条件

以下示例需要 `psycopg2` 库。

```shell theme={null}
pip install -U psycopg2
```

您还需要一个数据库。以下示例使用运行在 Docker 容器中的 Postgres 数据库。

```shell 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
```

## 示例

以下 Agent 将列出数据库中的所有表。

```python cookbook/tools/postgres.py theme={null}
from agno.agent import Agent
from agno.tools.postgres import PostgresTools

# 使用连接详细信息初始化 PostgresTools
postgres_tools = PostgresTools(
    host="localhost",
    port=5532,
    db_name="ai",
    user="ai",
    password="ai"
)

# 使用 PostgresTools 创建一个 Agent
agent = Agent(tools=[postgres_tools])

# 示例：要求 Agent 运行 SQL 查询
agent.print_response("""
请运行一个 SQL 查询，获取 users 表中过去 30 天注册的所有用户
""")
```

## Toolkit 参数

| 名称                 | 类型                               | 默认值     | 描述               |
| ------------------ | -------------------------------- | ------- | ---------------- |
| `connection`       | `psycopg2.extensions.connection` | `None`  | 可选的数据库连接对象。      |
| `db_name`          | `str`                            | `None`  | 可选的要连接的数据库名称。    |
| `user`             | `str`                            | `None`  | 可选的数据库身份验证用户名。   |
| `password`         | `str`                            | `None`  | 可选的数据库身份验证密码。    |
| `host`             | `str`                            | `None`  | 可选的数据库连接主机。      |
| `port`             | `int`                            | `None`  | 可选的数据库连接端口。      |
| `run_queries`      | `bool`                           | `True`  | 启用运行 SQL 查询。     |
| `inspect_queries`  | `bool`                           | `False` | 在执行前启用检查 SQL 查询。 |
| `summarize_tables` | `bool`                           | `True`  | 启用表结构摘要。         |
| `export_tables`    | `bool`                           | `False` | 启用从数据库导出表。       |

## Toolkit 函数

| 函数                     | 描述                                                                                                       |
| ---------------------- | -------------------------------------------------------------------------------------------------------- |
| `show_tables`          | 获取并显示数据库中的表列表。返回表列表。                                                                                     |
| `describe_table`       | 通过返回列、数据类型和最大字符长度来描述指定表的结构。参数包括指定表名的 'table'。返回表描述。                                                      |
| `summarize_table`      | 通过计算数值列的最小值、最大值、平均值、标准差和非空计数等聚合函数来汇总表。参数包括指定表名的 'table'，以及可选的指定模式的 'table\_schema'（默认为 "public"）。返回表的摘要。 |
| `inspect_query`        | 通过返回查询计划来检查 SQL 查询。参数包括指定 SQL 查询的 'query'。返回查询计划。                                                        |
| `export_table_to_path` | 将指定表以 CSV 格式导出到给定路径。参数包括指定表名的 'table'，以及可选的指定保存文件位置的 'path'（默认为当前目录）。返回导出操作的结果。                          |
| `run_query`            | 执行 SQL 查询并返回结果。参数包括指定 SQL 查询的 'query'。返回查询执行结果。                                                          |

## 开发者资源

* 查看 [Tools](https://github.com/agno-agi/agno/blob/main/libs/agno/agno/tools/postgres.py)
* 查看 [Cookbook](https://github.com/agno-agi/agno/blob/main/cookbook/tools/postgres_tools.py)
