Database Configuration

CodeIgniter has a config file that lets you store your databaseconnection values (username, password, database name, etc.). The configfile is located at app/Config/Database.php. You can also setdatabase connection values in the .env file. See below for more details.

The config settings are stored in a class property that is an array with thisprototype:

  1. public $default = [
  2. 'DSN' => '',
  3. 'hostname' => 'localhost',
  4. 'username' => 'root',
  5. 'password' => '',
  6. 'database' => 'database_name',
  7. 'DBDriver' => 'MySQLi',
  8. 'DBPrefix' => '',
  9. 'pConnect' => TRUE,
  10. 'DBDebug' => TRUE,
  11. 'cacheOn' => FALSE,
  12. 'cacheDir' => '',
  13. 'charset' => 'utf8',
  14. 'DBCollat' => 'utf8_general_ci',
  15. 'swapPre' => '',
  16. 'encrypt' => FALSE,
  17. 'compress' => FALSE,
  18. 'strictOn' => FALSE,
  19. 'failover' => [],
  20. ];

The name of the class property is the connection name, and can be usedwhile connecting to specify a group name.

Some database drivers (such as PDO, PostgreSQL, Oracle, ODBC) mightrequire a full DSN string to be provided. If that is the case, youshould use the ‘DSN’ configuration setting, as if you’re using thedriver’s underlying native PHP extension, like this:

  1. // PDO
  2. $default['DSN'] = 'pgsql:host=localhost;port=5432;dbname=database_name';
  3.  
  4. // Oracle
  5. $default['DSN'] = '//localhost/XE';

Note

If you do not specify a DSN string for a driver that requires it, CodeIgniterwill try to build it with the rest of the provided settings.

Note

If you provide a DSN string and it is missing some valid settings (e.g. thedatabase character set), which are present in the rest of the configurationfields, CodeIgniter will append them.

You can also specify failovers for the situation when the main connection cannot connect for some reason.These failovers can be specified by setting the failover for a connection like this:

  1. $default['failover'] = [
  2. [
  3. 'hostname' => 'localhost1',
  4. 'username' => '',
  5. 'password' => '',
  6. 'database' => '',
  7. 'DBDriver' => 'MySQLi',
  8. 'DBPrefix' => '',
  9. 'pConnect' => TRUE,
  10. 'DBDebug' => TRUE,
  11. 'cacheOn' => FALSE,
  12. 'cacheDir' => '',
  13. 'charset' => 'utf8',
  14. 'DBCollat' => 'utf8_general_ci',
  15. 'swapPre' => '',
  16. 'encrypt' => FALSE,
  17. 'compress' => FALSE,
  18. 'strictOn' => FALSE
  19. ],
  20. [
  21. 'hostname' => 'localhost2',
  22. 'username' => '',
  23. 'password' => '',
  24. 'database' => '',
  25. 'DBDriver' => 'MySQLi',
  26. 'DBPrefix' => '',
  27. 'pConnect' => TRUE,
  28. 'DBDebug' => TRUE,
  29. 'cacheOn' => FALSE,
  30. 'cacheDir' => '',
  31. 'charset' => 'utf8',
  32. 'DBCollat' => 'utf8_general_ci',
  33. 'swapPre' => '',
  34. 'encrypt' => FALSE,
  35. 'compress' => FALSE,
  36. 'strictOn' => FALSE
  37. ]
  38. ];

You can specify as many failovers as you like.

You may optionally store multiple sets of connectionvalues. If, for example, you run multiple environments (development,production, test, etc.) under a single installation, you can set up aconnection group for each, then switch between groups as needed. Forexample, to set up a “test” environment you would do this:

  1. public $test = [
  2. 'DSN' => '',
  3. 'hostname' => 'localhost',
  4. 'username' => 'root',
  5. 'password' => '',
  6. 'database' => 'database_name',
  7. 'DBDriver' => 'MySQLi',
  8. 'DBPrefix' => '',
  9. 'pConnect' => TRUE,
  10. 'DBDebug' => TRUE,
  11. 'cacheOn' => FALSE,
  12. 'cacheDir' => '',
  13. 'charset' => 'utf8',
  14. 'DBCollat' => 'utf8_general_ci',
  15. 'swapPre' => '',
  16. 'compress' => FALSE,
  17. 'encrypt' => FALSE,
  18. 'strictOn' => FALSE,
  19. 'failover' => []
  20. );

Then, to globally tell the system to use that group you would set thisvariable located in the config file:

  1. $defaultGroup = 'test';

Note

The name ‘test’ is arbitrary. It can be anything you want. Bydefault we’ve used the word “default” for the primary connection,but it too can be renamed to something more relevant to your project.

You could modify the config file to detect the environment and automaticallyupdate the defaultGroup value to the correct one by adding the required logicwithin the class’ constructor:

  1. class Database
  2. {
  3. public $development = [...];
  4. public $test = [...];
  5. public $production = [...];
  6.  
  7. public function __construct()
  8. {
  9. $this->defaultGroup = ENVIRONMENT;
  10. }
  11. }

Configuring With .env File

You can also save your configuration values within a .env file with the current server’sdatabase settings. You only need to enter the values that change from what is in thedefault group’s configuration settings. The values should be name following this format, wheredefault is the group name:

  1. database.default.username = 'root';
  2. database.default.password = '';
  3. database.default.database = 'ci4';

As with all other

Explanation of Values:

Name ConfigDescription
dsnThe DSN connect string (an all-in-one configuration sequence).
hostnameThe hostname of your database server. Often this is ‘localhost’.
usernameThe username used to connect to the database.
passwordThe password used to connect to the database.
databaseThe name of the database you want to connect to.
DBDriverThe database type. eg: MySQLi, Postgre, etc. The case must match the driver name
DBPrefixAn optional table prefix which will added to the table name when runningQuery Builder queries. This permits multiple CodeIgniterinstallations to share one database.
pConnectTRUE/FALSE (boolean) - Whether to use a persistent connection.
DBDebugTRUE/FALSE (boolean) - Whether database errors should be displayed.
cacheOnTRUE/FALSE (boolean) - Whether database query caching is enabled.
cacheDirThe absolute server path to your database query cache directory.
charsetThe character set used in communicating with the database.
DBCollatThe character collation used in communicating with the databaseNoteOnly used in the ‘MySQLi’ driver.
swapPreA default table prefix that should be swapped with dbprefix. This is useful for distributedapplications where you might run manually written queries, and need the prefix to still becustomizable by the end user.
schemaThe database schema, defaults to ‘public’. Used by PostgreSQL and ODBC drivers.
encryptWhether or not to use an encrypted connection.
  • ‘sqlsrv’ and ‘pdo/sqlsrv’ drivers accept TRUE/FALSE
  • ‘MySQLi’ and ‘pdo/mysql’ drivers accept an array with the following options:
    • ‘ssl_key’ - Path to the private key file
    • ‘ssl_cert’ - Path to the public key certificate file
    • ‘ssl_ca’ - Path to the certificate authority file
    • ‘ssl_capath’ - Path to a directory containing trusted CA certificates in PEM format
    • ‘ssl_cipher’ - List of allowed ciphers to be used for the encryption, separated by colons (‘:’)
    • ‘ssl_verify’ - TRUE/FALSE; Whether to verify the server certificate or not (‘MySQLi’ only)
compressWhether or not to use client compression (MySQL only).
strictOnTRUE/FALSE (boolean) - Whether to force “Strict Mode” connections, good for ensuring strict SQLwhile developing an application.
portThe database port number. To use this value you have to add a line to the database config array.
  1. $default['port'] = 5432;

Note

Depending on what database platform you are using (MySQL, PostgreSQL,etc.) not all values will be needed. For example, when using SQLite youwill not need to supply a username or password, and the database namewill be the path to your database file. The information above assumesyou are using MySQL.