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

  ###################################
  # Per-database credentials（可选）
  ###################################
  # 用于为每个数据库单独配置账号密码
  # 优先级高于全局 user/password
  # 如果为空，则回退使用全局账号
  databaseCredentials:
    dify:
      user: ""
      password: ""
    enterprise:
      user: ""
      password: ""
    audit:
      user: ""
      password: ""
    plugin_daemon:
      user: ""
      password: ""

  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               | 各组件使用的数据库名称映射                               |
| databaseCredentials     | （新增）每个数据库独立账号配置，优先级高于全局账号                   |
| dialectOptions.postgres | PostgreSQL 专用配置（sslMode、uriScheme、extras 等） |
| dialectOptions.mysql    | MySQL/TiDB 专用配置（params、tls 等）               |

***

## Per-DB Credentials 行为说明

优先级规则：

```
databaseCredentials[db] > global user/password
```

* 若某个数据库配置了独立账号 → 使用该账号
* 若为空 → 回退使用全局 `user/password`

***

## 默认端口

| 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"
    enterprise: "enterprise"
    audit: "audit"

  databaseCredentials:
    dify:
      user: ""
      password: ""
    enterprise:
      user: ""
      password: ""
    audit:
      user: ""
      password: ""
    plugin_daemon:
      user: ""
      password: ""

  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"
    enterprise: "enterprise"
    audit: "audit"

  databaseCredentials:
    dify:
      user: ""
      password: ""
    enterprise:
      user: ""
      password: ""
    audit:
      user: ""
      password: ""
    plugin_daemon:
      user: ""
      password: ""

  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"
    enterprise: "enterprise"
    audit: "audit"

  databaseCredentials:
    dify:
      user: ""
      password: ""
    enterprise:
      user: ""
      password: ""
    audit:
      user: ""
      password: ""
    plugin_daemon:
      user: ""
      password: ""

  dialectOptions:
    mysql:
      params: "charset=utf8mb4&parseTime=true&loc=UTC"
```
