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

# 外部数据库

## 显示 Helm Chart 值

```bash theme={null}
helm show values dify/dify
```

## 连接外部数据库

支持连接 PostgreSQL、MySQL 或 TiDB 作为外部数据库。

以下是 Helm chart 值中外部数据库配置的通用示例代码：

```yaml theme={null}
###################################
# External database (Postgres/MySQL/TiDB)
###################################
externalDatabase:
  enabled: false
  # postgres | mysql | tidb
  engine: "postgres"
  host: "localhost"
  # Default ports: postgres=5432, mysql=3306, tidb=4000
  # Note: when switching engine, remember to update port accordingly
  port: 5432
  user: "postgres"
  password: "#REPLACE_ME#"
  timezone: "UTC"
  databases:
    dify: "dify"
    plugin_daemon: "dify_plugin_daemon"
    enterprise: "enterprise"
    audit: "audit"
  dialectOptions:
    postgres:
      sslMode: "require"
      uriScheme: "postgresql"
      # extras: "options=-c search_path=your-schema -c your-other-option=xxx"
      extras: ""
    mysql:
      # MySQL/TiDB params, e.g. charset=utf8mb4&parseTime=true&loc=UTC
      params: "charset=utf8mb4&parseTime=true&loc=UTC"
      tls: false
```

> **注意**
>
> 在启动 Dify 之前，你需要手动创建所需的数据库。

### PostgreSQL

```sql theme={null}
CREATE DATABASE dify;
CREATE DATABASE enterprise;
CREATE DATABASE audit;
-- dify_plugin_daemon 会在插件守护进程首次启动时自动创建
```

### MySQL / TiDB

```sql theme={null}
CREATE DATABASE IF NOT EXISTS dify;
CREATE DATABASE IF NOT EXISTS enterprise;
CREATE DATABASE IF NOT EXISTS audit;
-- dify_plugin_daemon 会在插件守护进程首次启动时自动创建
```

## 配置说明

| 字段                        | 说明                                          |
| ------------------------- | ------------------------------------------- |
| `engine`                  | 数据库类型：`postgres`、`mysql`、`tidb`             |
| `databases`               | 各组件使用的数据库名称映射                               |
| `dialectOptions.postgres` | PostgreSQL 专用配置（sslMode、uriScheme、extras 等） |
| `dialectOptions.mysql`    | MySQL/TiDB 专用配置（params、tls 等）               |

### 默认端口

| Engine   | 默认端口 |
| -------- | ---- |
| postgres | 5432 |
| mysql    | 3306 |
| tidb     | 4000 |

## 配置示例

### PostgreSQL（外部）

```yaml theme={null}
externalDatabase:
  enabled: true
  engine: "postgres"
  host: "postgres.example.com"
  port: 5432
  user: "postgres"
  password: "******"
  databases:
    dify: "dify"
    plugin_daemon: "dify_plugin_daemon"
    extras: ""
  dialectOptions:
    postgres:
      sslMode: "require"
```

### MySQL（外部）

```yaml theme={null}
externalDatabase:
  enabled: true
  engine: "mysql"
  host: "mysql.example.com"
  port: 3306
  user: "root"
  password: "******"
  databases:
    dify: "dify"
    plugin_daemon: "dify_plugin_daemon"
  dialectOptions:
    mysql:
      params: "charset=utf8mb4&parseTime=true&loc=UTC"
```

### TiDB（外部）

```yaml theme={null}
externalDatabase:
  enabled: true
  engine: "tidb"
  host: "tidb.example.com"
  port: 4000
  user: "root"
  password: "******"
  databases:
    dify: "dify"
    plugin_daemon: "dify_plugin_daemon"
  dialectOptions:
    mysql:
      params: "charset=utf8mb4&parseTime=true&loc=UTC"
```
