Dot All Lisbon – the official Craft CMS conference – is happening September 23 - 25.

Database Connection Settings

Craft can connect to MySQL and Postgres databases.

Database connection settings may be set from a config/db.php file, but because they’re often entirely environment-specific, Craft supports assigning directly from environment variables. In a new Craft 4 project, your .env file will need to define these options:

# Required variables:
CRAFT_APP_ID=
CRAFT_ENVIRONMENT=dev
CRAFT_SECURITY_KEY=

# Database-specific variables:
CRAFT_DB_DRIVER=mysql
CRAFT_DB_SERVER=127.0.0.1
CRAFT_DB_PORT=3306
CRAFT_DB_DATABASE=
CRAFT_DB_USER=root
CRAFT_DB_PASSWORD=
CRAFT_DB_SCHEMA=public
CRAFT_DB_TABLE_PREFIX=

We recommend this approach because it:

  1. Keeps sensitive information out of your project’s codebase (.env files should never be shared or committed to version control);
  2. Makes collaborating with other developers easier, as each developer can define their own settings from scratch, without overwriting someone else’s settings.

Environment overrides are covered in greater detail on the configuration overview page.

If you need to use your own environment variables in a config file (or connection details are provided via platform-specific keys), create config/db.php and return an explicit array of settings:

use craft\helpers\App;

return [
    'driver' => App::env('MY_DB_DRIVER'),
    'server' => App::env('MY_DB_SERVER'),
    'port' => App::env('MY_DB_PORT'),
    'database' => App::env('MY_DB_DATABASE'),
    'user' => App::env('MY_DB_USER'),
    'password' => App::env('MY_DB_PASSWORD'),
    'schema' => App::env('MY_DB_SCHEMA'),
    'tablePrefix' => App::env('MY_DB_TABLE_PREFIX'),
    'attributes' => [
        PDO::MYSQL_ATTR_SSL_CA => '/path/to/my.crt.pem'
        // ...
    ],
];

Finer-grain control of Craft’s database connection is possible by configuring the underlying db application component. This may be necessary if you have specific security requirements, or your app needs to connect to multiple databases.

Note that if you need to supply custom PDO attributes to your primary database connection, you should do so via the attributes key in your config/db.php file, not from config/app.php while overriding the db component.

Supported Settings

attributes

Allowed types
array
Default value
[]
Defined by
DbConfig::$attributes

An array of key-value pairs of PDO attributes to pass into the PDO constructor.

For example, when using the MySQL PDO driver, if you wanted to enable a SSL database connection (assuming SSL is enabled in MySQL and 'user' can connect via SSL, you’d set these:

->attributes([
    PDO::MYSQL_ATTR_SSL_KEY => '/path/to/my/client-key.pem',
    PDO::MYSQL_ATTR_SSL_CERT => '/path/to/my/client-cert.pem',
    PDO::MYSQL_ATTR_SSL_CA => '/path/to/my/ca-cert.pem',
])

charset

Allowed types
string
Default value
'utf8'
Defined by
DbConfig::$charset

The character set to use when creating tables.

::: tip You can change the character set and collation across all existing database tables using this terminal command:

php craft db/convert-charset

:::

::: warning If you set this to something besides utf8 or utf8mb4 for MySQL, you must also set the collation() setting to a compatible collation name. :::

::: code

->charset('utf8mb3')
->collation('utf8mb3_general_ci')
CRAFT_DB_CHARSET=utf8mb3
CRAFT_DB_COLLATION=utf8mb3_general_ci

:::

collation

Allowed types
string, null
Default value
null
Defined by
DbConfig::$collation
Since
3.6.4

The collation to use when creating tables. (MySQL only.)

If null, the following collation will be used by default:

  • MySQL 8.0+: utf8mb4_0900_ai_ci
  • Older MySQL versions and MariaDB: utf8mb4_unicode_ci

::: tip You can change the character set and collation across all existing database tables using this terminal command:

php craft db/convert-charset

:::

::: code

->charset('utf8mb3')
->collation('utf8mb3_general_ci')
CRAFT_DB_CHARSET=utf8mb3
CRAFT_DB_COLLATION=utf8mb3_general_ci

:::

database

Allowed types
string, null
Default value
null
Defined by
DbConfig::$database

The name of the database to select.

::: code

->database('mydatabase')
CRAFT_DB_DATABASE=mydatabase

:::

driver

Allowed types
string, null
Default value
null
Defined by
DbConfig::$driver

The database driver to use. Either mysql for MySQL or pgsql for PostgreSQL.

::: code

->driver('mysql')
CRAFT_DB_DRIVER=mysql

:::

dsn

Allowed types
string, null
Default value
null
Defined by
DbConfig::$dsn

The Data Source Name (“DSN”) that tells Craft how to connect to the database.

DSNs should begin with a driver prefix (mysql: or pgsql:), followed by driver-specific parameters. For example, mysql:host=127.0.0.1;port=3306;dbname=acme_corp.

::: code

->dsn('mysql:host=127.0.0.1;port=3306;dbname=acme_corp')
CRAFT_DB_DSN=mysql:host=127.0.0.1;port=3306;dbname=acme_corp

:::

password

Allowed types
string
Default value
''
Defined by
DbConfig::$password

The database password to connect with.

::: code

->password('super-secret')
CRAFT_DB_PASSWORD=super-secret

:::

port

Allowed types
integer, null
Default value
null
Defined by
DbConfig::$port

The database server port. Defaults to 3306 for MySQL and 5432 for PostgreSQL.

::: code

->port(3306)
CRAFT_DB_PORT=3306

:::

schema

Allowed types
string, null
Default value
'public'
Defined by
DbConfig::$schema

The schema that Postgres is configured to use by default (PostgreSQL only).

::: tip To force Craft to use the specified schema regardless of PostgreSQL’s search_path setting, you must enable the setSchemaOnConnect() setting. :::

::: code

->schema('myschema,public')
CRAFT_DB_SCHEMA=myschema,public

:::

server

Allowed types
string, null
Default value
null
Defined by
DbConfig::$server

The database server name or IP address. Usually localhost or 127.0.0.1.

::: code

->server('localhost')
CRAFT_DB_SERVER=localhost

:::

setSchemaOnConnect

Allowed types
boolean
Default value
false
Defined by
DbConfig::$setSchemaOnConnect
Since
3.7.27

Whether the schema() should be explicitly used for database queries (PostgreSQL only).

::: warning This will cause an extra SET search_path SQL query to be executed per database connection. Ideally, PostgreSQL’s search_path setting should be configured to prioritize the desired schema. :::

::: code

->setSchemaOnConnect(true)
CRAFT_DB_SET_SCHEMA_ON_CONNECT=true

:::

tablePrefix

Allowed types
string, null
Default value
null
Defined by
DbConfig::$tablePrefix

If you’re sharing Craft installs in a single database (MySQL) or a single database and using a shared schema (PostgreSQL), you can set a table prefix here to avoid per-install table naming conflicts. This can be no more than 5 characters, and must be all lowercase.

::: code

->tablePrefix('craft_')
CRAFT_DB_TABLE_PREFIX=craft_

:::

unixSocket

Allowed types
string, null
Default value
null
Defined by
DbConfig::$unixSocket

MySQL only. If this is set, the CLI connection string (used for yiic) will connect to the Unix socket instead of the server and port. If this is specified, then server and port settings are ignored.

::: code

->unixSocket('/Applications/MAMP/tmp/mysql/mysql.sock')
CRAFT_DB_UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

:::

url

Allowed types
string, null
Default value
null
Defined by
DbConfig::$url

The database connection URL, if one was provided by your hosting environment.

If this is set, the values for driver(), user(), database(), server(), port(), and database() will be extracted from it.

::: code

->url('jdbc:mysql://database.foo:3306/mydb')
CRAFT_DB_URL=jdbc:mysql://database.foo:3306/mydb

:::

useUnbufferedConnections

Allowed types
boolean
Default value
false
Defined by
DbConfig::$useUnbufferedConnections
Since
3.7.0

Whether batched queries should be executed on a separate, unbuffered database connection.

This setting only applies to MySQL. It can be enabled when working with high volume content, to prevent PHP from running out of memory when querying too much data at once. (See https://www.yiiframework.com/doc/guide/2.0/en/db-query-builder#batch-query-mysql for an explanation of MySQL’s batch query limitations.)

For more on Craft batch queries, see https://craftcms.com/knowledge-base/query-batching-batch-each.

::: code

->useUnbufferedConnections(true)
CRAFT_DB_USE_UNBUFFERED_CONNECTIONS=true

:::

user

Allowed types
string
Default value
'root'
Defined by
DbConfig::$user

The database username to connect with.

::: code

->user('db')
CRAFT_DB_USER=db

:::