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

# 外部データベース

# 3.9.0 の設定

## Helm Chart の値を表示

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

## 外部データベースに接続

PostgreSQL、MySQL、または TiDB を外部データベースとして接続できます。

以下は、Helm chart の values における外部データベース設定の一般的な例です。

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

# 3.9.1 の設定

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

  ###################################
  # データベースごとの認証情報（任意）
  ###################################
  # 各データベースごとにユーザー名とパスワードを個別に設定するために使用します
  # グローバルな 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 認証情報の動作

優先順位：

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