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

# 测试数据库连通性

> 在保存凭据之前检查它们，并列出它们能读取的 schema

```
POST /openapi/memorylake/api/v3/db-connections/connectivity-test
```

检查尚未保存的数据库凭据，并返回这组凭据能读取的 schema。在[创建连接](https://docs.memorylake.cn/features/memorylake/api-reference/database/connections/create-database-connection.md)之前，可以用它校验表单。这个接口不保存任何内容。

连接创建之后，请改用[列出连接可读的 Schema](https://docs.memorylake.cn/features/memorylake/api-reference/database/connections/list-connection-schemas.md)，它不需要密码。

> [!NOTE]
>
> **所需权限：** `dbconn:add`

### 请求体

**host** `string` *required*

服务器主机名或地址

**port** `integer` (default: 5432)

服务器端口，取值 1 到 65535

**database** `string` *required*

要读取的数据库名

**username** `string` *required*

用于认证的用户名

**password** `string` *required*

用于认证的密码

```bash cURL theme={null}
curl -X POST 'https://app.memorylake.cn/openapi/memorylake/api/v3/db-connections/connectivity-test' \
  -H 'Authorization: Bearer sk_xxxxxx' \
  -H 'Content-Type: application/json' \
  -d '{
    "host": "pg.internal.example.com",
    "port": 5432,
    "database": "sales",
    "username": "analytics_ro",
    "password": "your-database-password"
  }'
```

### 响应

**data** `object`

**可读的 schema**

**schemas** `array`

这组凭据能读取的 schema。创建数据源时从中选择一个。

如果数据库连不上，或者拒绝了这组凭据，接口返回 `400`，`error_code` 为 `DB_CONNECTION_FAILED`。响应不会说明是哪一种情况：请检查主机、端口、数据库名、用户名和密码，并确认该数据库允许来自 MemoryLake 的连接。用同样的参数重试不会成功。

```json Success (200) theme={null}
{
  "success": true,
  "data": {
    "schemas": ["public", "sales"]
  }
}
```

```json Connection failed (400) theme={null}
{
  "success": false,
  "message": "[CONNECTION_FAILED] Database connection failed",
  "error_code": "DB_CONNECTION_FAILED"
}
```


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