> For the complete documentation index, see [llms.txt](https://docs.n8n.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.n8n.io/deploy/host-n8n/configure-n8n/basic-configuration/use-environment-variables/database.md).

# Database

Set up and configure databases with environment variables for your self-hosted n8n instance.

{% hint style="info" %}
**File-based configuration**

You can add `_FILE` to individual variables to provide their configuration in a separate file. Refer to [Keeping sensitive data in separate files](/deploy/host-n8n/configure-n8n/basic-configuration.md#keeping-sensitive-data-in-separate-files) for more details.
{% endhint %}

By default, n8n uses SQLite. n8n also supports PostgreSQL. n8n [deprecated support for MySQL and MariaDB](/changelog/v10-migration-guide.md#mysql-and-mariadb) from n8n 1.0.

This page outlines environment variables to configure your chosen database for your self-hosted n8n instance.

| Variable                                             | Type                                                                | Default  | Description                                                                                                                                                                        |
| ---------------------------------------------------- | ------------------------------------------------------------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>DB\_TYPE</code><br>/<code>\_FILE</code></p> | <p>Enum string:<br><code>sqlite</code>, <code>postgresdb</code></p> | `sqlite` | The database to use.                                                                                                                                                               |
| `DB_TABLE_PREFIX`                                    | \*                                                                  | -        | Prefix to use for table names.                                                                                                                                                     |
| `DB_PING_INTERVAL_SECONDS`                           | Number                                                              | `2`      | How often, in seconds, n8n pings the database to check that the connection is still alive.                                                                                         |
| `DB_PING_TIMEOUT_MS`                                 | Number                                                              | `5000`   | How long, in milliseconds, n8n waits for a single ping to respond before counting it as a failure. Falls back to `N8N_DB_PING_TIMEOUT`, deprecated from n8n 2.35.0, if that's set. |
| `DB_PING_MAX_FAILURES_BEFORE_RECOVERY`               | Number                                                              | `3`      | How many pings in a row must fail before n8n treats the connection as lost and starts recovery.                                                                                    |
| `DB_RECOVERY_BACKOFF_MIN_MS`                         | Number                                                              | `1000`   | How long, in milliseconds, n8n waits before its first recovery attempt. Each retry waits longer (exponential backoff).                                                             |
| `DB_RECOVERY_BACKOFF_MAX_MS`                         | Number                                                              | `30000`  | The longest, in milliseconds, n8n waits between recovery attempts. This caps the backoff. Must be greater than or equal to `DB_RECOVERY_BACKOFF_MIN_MS`.                           |
| `DB_CONNECTION_ACQUISITION_TIMEOUT_MS`               | Number                                                              | `30000`  | How long, in milliseconds, a query waits while recovery is in progress before failing fast with an error. Set to `0` to wait indefinitely. Applies to PostgreSQL only.             |

## PostgreSQL <a href="#postgresql" id="postgresql"></a>

| Variable                                                                                    | Type    | Default     | Description                                                                                                                                                                                                                                                |
| ------------------------------------------------------------------------------------------- | ------- | ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>DB\_POSTGRESDB\_DATABASE</code><br>/<code>\_FILE</code></p>                        | String  | `n8n`       | The name of the PostgreSQL database.                                                                                                                                                                                                                       |
| <p><code>DB\_POSTGRESDB\_HOST</code><br>/<code>\_FILE</code></p>                            | String  | `localhost` | The PostgreSQL host.                                                                                                                                                                                                                                       |
| <p><code>DB\_POSTGRESDB\_PORT</code><br>/<code>\_FILE</code></p>                            | Number  | `5432`      | The PostgreSQL port.                                                                                                                                                                                                                                       |
| <p><code>DB\_POSTGRESDB\_USER</code><br>/<code>\_FILE</code></p>                            | String  | `postgres`  | The PostgreSQL user.                                                                                                                                                                                                                                       |
| <p><code>DB\_POSTGRESDB\_PASSWORD</code><br>/<code>\_FILE</code></p>                        | String  | -           | The PostgreSQL password.                                                                                                                                                                                                                                   |
| <p><code>DB\_POSTGRESDB\_POOL\_SIZE</code><br>/<code>\_FILE</code></p>                      | Number  | `2`         | Control how many parallel open Postgres connections n8n should have. Increasing it may help with resource utilization, but too many connections may degrade performance.                                                                                   |
| <p><code>DB\_POSTGRESDB\_CONNECTION\_TIMEOUT</code><br>/<code>\_FILE</code></p>             | Number  | `20000`     | Postgres connection timeout (ms).                                                                                                                                                                                                                          |
| <p><code>DB\_POSTGRESDB\_IDLE\_CONNECTION\_TIMEOUT</code><br>/<code>\_FILE</code></p>       | Number  | `30000`     | Amount of time before an idle connection is eligible for eviction for being idle.                                                                                                                                                                          |
| <p><code>DB\_POSTGRESDB\_MAX\_CONNECTION\_LIFETIME\_MS</code><br>/<code>\_FILE</code></p>   | Number  | `3600000`   | How long, in milliseconds, n8n keeps a pooled connection before recycling it. Recycling old connections helps avoid stale connections that the database or network dropped without n8n noticing. The default is one hour. Set to `0` to disable recycling. |
| <p><code>DB\_POSTGRESDB\_KEEP\_ALIVE</code><br>/<code>\_FILE</code></p>                     | Boolean | `true`      | Whether to enable TCP keep-alive on connections. Keep-alive lets n8n notice a dead connection without having to run a query first.                                                                                                                         |
| <p><code>DB\_POSTGRESDB\_KEEP\_ALIVE\_INITIAL\_DELAY\_MS</code><br>/<code>\_FILE</code></p> | Number  | `10000`     | How long, in milliseconds, n8n waits before sending the first TCP keep-alive probe on a connection.                                                                                                                                                        |
| <p><code>DB\_POSTGRESDB\_SCHEMA</code><br>/<code>\_FILE</code></p>                          | String  | `public`    | The PostgreSQL schema.                                                                                                                                                                                                                                     |
| <p><code>DB\_POSTGRESDB\_SSL\_ENABLED</code><br>/<code>\_FILE</code></p>                    | Boolean | `false`     | Whether to enable SSL. Automatically enabled if `DB_POSTGRESDB_SSL_CA`, `DB_POSTGRESDB_SSL_CERT` or `DB_POSTGRESDB_SSL_KEY` is set to a non-empty value, or if `DB_POSTGRESDB_SSL_REJECT_UNAUTHORIZED` is set to `false`.                                  |
| <p><code>DB\_POSTGRESDB\_SSL\_CA</code><br>/<code>\_FILE</code></p>                         | String  | -           | The PostgreSQL SSL certificate authority.                                                                                                                                                                                                                  |
| <p><code>DB\_POSTGRESDB\_SSL\_CERT</code><br>/<code>\_FILE</code></p>                       | String  | -           | The PostgreSQL SSL certificate.                                                                                                                                                                                                                            |
| <p><code>DB\_POSTGRESDB\_SSL\_KEY</code><br>/<code>\_FILE</code></p>                        | String  | -           | The PostgreSQL SSL key.                                                                                                                                                                                                                                    |
| <p><code>DB\_POSTGRESDB\_SSL\_REJECT\_UNAUTHORIZED</code><br>/<code>\_FILE</code></p>       | Boolean | `true`      | If n8n should reject unauthorized SSL connections (true) or not (false).                                                                                                                                                                                   |

## SQLite <a href="#sqlite" id="sqlite"></a>

| Variable                      | Type    | Default | Description                                                                                                                                                                                                                                |
| ----------------------------- | ------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `DB_SQLITE_POOL_SIZE`         | Number  | `3`     | The number of parallel SQL read connections in the SQLite connection pool. Must be at least `1`. Setting it to `0` logs a warning and falls back to the default. n8n opens the SQLite file in [WAL mode](https://www.sqlite.org/wal.html). |
| `DB_SQLITE_VACUUM_ON_STARTUP` | Boolean | `false` | Runs [VACUUM](https://www.sqlite.org/lang_vacuum.html) operation on startup to rebuild the database. Reduces file size and optimizes indexes. This is a long running blocking operation and increases start-up time.                       |

## How database connection recovery works <a href="#how-database-connection-recovery-works" id="how-database-connection-recovery-works"></a>

n8n keeps checking that it can still reach your database. If the connection drops, for example after a database restart or a network blip, n8n can repair it on its own instead of staying broken until you restart n8n.

Here's the cycle the variables on this page control:

1. Every `DB_PING_INTERVAL_SECONDS`, n8n sends a ping. Each ping has `DB_PING_TIMEOUT_MS` to respond.
2. Once `DB_PING_MAX_FAILURES_BEFORE_RECOVERY` pings fail in a row, n8n treats the connection as lost.
3. n8n then rebuilds the connection, retrying with a growing wait between attempts that starts at `DB_RECOVERY_BACKOFF_MIN_MS` and never exceeds `DB_RECOVERY_BACKOFF_MAX_MS`.
4. On PostgreSQL, while recovery is in progress, any new query waits up to `DB_CONNECTION_ACQUISITION_TIMEOUT_MS` for the connection to come back, then fails with an error rather than hanging.

{% hint style="info" %}
**Applies to all databases**

The health checks and the recovery cycle (the `DB_PING_*` and `DB_RECOVERY_BACKOFF_*` variables) apply to every database type. The remaining settings (`DB_CONNECTION_ACQUISITION_TIMEOUT_MS`, `DB_POSTGRESDB_MAX_CONNECTION_LIFETIME_MS`, `DB_POSTGRESDB_KEEP_ALIVE`, and `DB_POSTGRESDB_KEEP_ALIVE_INITIAL_DELAY_MS`) apply to PostgreSQL only. The default values suit most deployments, so you can leave them unchanged unless you run into connection problems.
{% endhint %}

## Related resources

* [Environment variables](/deploy/host-n8n/configure-n8n/basic-configuration/use-environment-variables.md)
