Database configuration
The ./config/database.js
file (or the ./config/database.ts
file for TypeScript) is used to define database connections that will be used to store the application content.
Strapi applications are not meant to be connected to a pre-existing database, not created by a Strapi application, nor connected to a Strapi v3 database. The Strapi team will not support such attempts. Attempting to connect to an unsupported database may, and most likely will, result in lost data.
The CLI installation guide details supported database and versions.
Configuration structure
The /config/database.js|ts
file accepts 2 main configuration objects:
connection
for database configuration options passed to Knex.jssettings
for Strapi-specific database settings
connection
configuration object
Parameter | Description | Type | Default |
---|---|---|---|
client | Database client to create the connection. Accepts the following values:
| String | - |
connection | Database connection information | Knex.Config object<br />Knex.Config Function (experimental) | - |
debug | Show database exchanges and errors. | Boolean | false |
useNullAsDefault Optional, only for SQLite | Use NULL as a default value | Boolean | true |
pool Optional | Database pooling options | Object | - |
acquireConnectionTimeout Optional | How long knex will wait before throwing a timeout error when acquiring a connection (in milliseconds) | Integer | 60000 |
Strapi only supports the following client values, and will automatically rewrite the client
value to the following options before passing the configuration to Knex:
client value | Actual package used |
---|---|
sqlite | better-sqlite3 |
mysql | mysql2 |
postgres | pg |
Connection parameters
The connection.connection
object found in ./config/database.js
(or ./config/database.ts
for TypeScript) is used to pass database connection information and accepts the following parameters:
Parameter | Description | Type |
---|---|---|
connectionString | Database connection string. When set, it overrides the other connection.connection properties. To disable use an empty string: '' . Available in v4.6.2 + | String |
host | Database host name. Default value: localhost . | String |
port | Database port | Integer |
database | Database name. | String |
user | Username used to establish the connection | String |
password | Password used to establish the connection | String |
timezone | Set the default behavior for local time. Default value: utc Timezone options | String |
schema | Set the default database schema. Used only for Postgres DB. | String |
ssl | For SSL database connection. Use an object to pass certificate files as strings. | Boolean or Object |
Depending on the database client used, more parameters can be set (e.g., charset
and collation
for mysql). Check the database client documentation to know what parameters are available, for instance the pg, mysql, and better-sqlite3 documentations.
Database pooling options
The connection.pool
object optionally found in ./config/database.js
(or ./config/database.ts
for TypeScript) is used to pass Tarn.js database pooling options and accepts the following parameters:
When using Docker, change the pool min
value to 0
as Docker will kill any idle connections, making it impossible to keep any open connections to the database (see the Tarn.js pool settings used by Knex.js for more information).
Parameter | Description | Type | Default |
---|---|---|---|
min | Minimum number of database connections to keepalive | Integer | 2 |
max | Maximum number of database connections to keepalive | Integer | 10 |
acquireTimeoutMillis | Time in milliseconds before timing out a database connection attempt | Integer | 60000 |
createTimeoutMillis | Time in milliseconds before timing out a create query attempt | Integer | 30000 |
destroyTimeoutMillis | Time in milliseconds before timing out a destroy query attempt | Integer | 5000 |
idleTimeoutMillis | Time in milliseconds before free database connections are destroyed | Integer | 30000 |
reapIntervalMillis | Time in milliseconds to check for idle database connections to destroy | Integer | 1000 |
createRetryIntervalMillis | Time in milliseconds to idle before retrying failed create actions | Integer | 200 |
afterCreate | Callback function to execute custom logic when the pool acquires a new connection. See the Knex.js documentation for more information | Function | - |
settings
configuration object
The settings
object found in ./config/database.js
(or ./config/database.ts
for TypeScript) is used to configure Strapi-specific database settings and accepts the following parameters:
Parameter | Description | Type | Default |
---|---|---|---|
forceMigration | Enable or disable the forced database migration. | Boolean | true |
runMigrations | Enable or disable database migrations from running on start up. | Boolean | true |
useTypescriptMigrations | Look for migrations in the build dir instead of the src dir | Boolean | false |
When using useTypescriptMigrations
you can continue to use existing JavaScript migrations by setting compilerOptions { allowJs: true }
in your tsconfig file.
Configuration examples
- PostgreSQL
- MySQL/MariaDB
- SQLite
module.exports = ({ env }) => ({
connection: {
client: 'postgres',
connection: {
host: env('DATABASE_HOST', '127.0.0.1'),
port: env.int('DATABASE_PORT', 5432),
database: env('DATABASE_NAME', 'strapi'),
user: env('DATABASE_USERNAME', 'strapi'),
password: env('DATABASE_PASSWORD', 'strapi'),
schema: env('DATABASE_SCHEMA', 'public'), // Not required
ssl: {
rejectUnauthorized: env.bool('DATABASE_SSL_SELF', false), // For self-signed certificates
},
},
debug: false,
},
});
Strapi is aware that there is an issue regarding SSL support for the server.
In order to fix it, you have to set the ssl:{}
object as a boolean in order to disable it. See below for example:
module.exports = ({ env }) => ({
connection: {
client: 'postgres',
connection: {
...
ssl: env('DATABASE_SSL', false)
},
},
});
Please note that if you need client side SSL CA verification you will need to use the ssl:{}
object with the fs module to convert your CA certificate to a string. You can see an example below:
const fs = require('fs');
module.exports = ({ env }) => ({
connection: {
client: 'postgres',
connection: {
...
ssl: {
ca: fs.readFileSync(`${__dirname}/path/to/your/ca-certificate.crt`).toString(),
},
},
},
});
module.exports = ({ env }) => ({
connection: {
client: 'mysql',
connection: {
host: env('DATABASE_HOST', '127.0.0.1'),
port: env.int('DATABASE_PORT', 3306),
database: env('DATABASE_NAME', 'strapi'),
user: env('DATABASE_USERNAME', 'strapi'),
password: env('DATABASE_PASSWORD', 'strapi'),
ssl: {
rejectUnauthorized: env.bool('DATABASE_SSL_SELF', false), // For self-signed certificates
},
},
debug: false,
},
});
- JavaScript
- TypeScript
module.exports = ({ env }) => ({
connection: {
client: 'sqlite',
connection: {
filename: env('DATABASE_FILENAME', '.tmp/data.db'),
},
useNullAsDefault: true,
debug: false,
},
});
import path from 'path';
export default ({ env }) => ({
connection: {
client: 'sqlite',
connection: {
filename: path.join(
__dirname,
'..',
'..',
env('DATABASE_FILENAME', path.join('.tmp', 'data.db'))
),
},
useNullAsDefault: true,
},
});
Configuration in database
Configuration files are not multi-server friendly. To update configurations in production you can use a data store to get and set settings.
Get settings
environment
(string): Sets the environment you want to store the data in. By default it's current environment (can be an empty string if your configuration is environment agnostic).type
(string): Sets if your configuration is for anapi
,plugin
orcore
. By default it'score
.name
(string): You have to set the plugin or api name iftype
isapi
orplugin
.key
(string, required): The name of the key you want to store.
// strapi.store(object).get(object);
// create reusable plugin store variable
const pluginStore = strapi.store({
environment: strapi.config.environment,
type: 'plugin',
name: 'users-permissions',
});
await pluginStore.get({ key: 'grant' });