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

# 创建数据库连接

> 保存访问数据库的方式及其凭据

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

保存访问数据库的方式和读取它所需的凭据。连接属于您的团队，可以为任意工作空间中的数据源提供服务。

密码只写不读：任何接口都不会返回它。如需先检查凭据，请调用[测试数据库连通性](https://docs.memorylake.cn/features/memorylake/api-reference/database/connections/test-database-connectivity.md)。

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

### 请求体

**name** `string` *required*

显示名称

**host** `string` *required*

服务器主机名或地址

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

服务器端口，取值 1 到 65535

**database** `string` *required*

要读取的数据库名

**username** `string` *required*

用于认证的用户名

**password** `string` *required*

用于认证的密码。任何接口都不会返回它。

**description** `string`

自由格式的描述

**custom\_id** `string`

这个连接在您自己系统里的标识符，最长 255 个字符。在您的租户内必须唯一，设置后不能修改，且不能以 `_sys_` 开头。调用[获取数据库连接](https://docs.memorylake.cn/features/memorylake/api-reference/database/connections/get-database-connection.md)时带上 `by_custom_id=true`，即可用它查找连接。参见[您自己的标识符](https://docs.memorylake.cn/features/memorylake/api-reference/overview.md#您自己的标识符)。

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

### 响应

**data** `object`

**连接对象**

**id** `string`

数据库连接标识符，请把它当作不透明字符串处理。

**name** `string`

显示名称

**description** `string`

自由格式的描述

**host** `string`

服务器主机名或地址

**port** `integer`

服务器端口

**username** `string`

连接用于认证的用户名

**database** `string`

要读取的数据库名

**custom\_id** `string`

您为这个连接设置的自定义标识符（如果设置过）

**datasource\_count** `integer`

当前通过这个连接读取数据的数据源数量

**created\_at** `string`

创建时间戳

**updated\_at** `string`

最后更新时间戳

```json Success (200) theme={null}
{
  "success": true,
  "data": {
    "id": "dbconn-7c1e9a4b2d6f4a8e9b3c5d7f1a2b4c6d",
    "name": "Sales warehouse",
    "description": "Read-only replica of the sales database",
    "host": "pg.internal.example.com",
    "port": 5432,
    "username": "analytics_ro",
    "database": "sales",
    "custom_id": "sales-warehouse",
    "datasource_count": 0,
    "created_at": "2026-09-18T07:42:17.139Z",
    "updated_at": "2026-09-18T07:42:17.139Z"
  }
}
```


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