I want to make a project with codeigniter 4 and postgresql I have already seen several examples, I even read the documentation but it still shows an error when connecting to the database.
My file .env is like this:
# database.tests.hostname = localhost
# database.tests.database = postgres_test
# database.tests.username = test
# database.tests.password = test123
# database.tests.DBDriver = postgre
And my file config\App.php
public $tests = [
'DSN' => 'pgsql:host=localhost;port=5432;dbname=database_name',
'hostname' => 'localhost',
'username' => 'test',
'password' => 'test123',
'database' => 'postgres_test',
'DBDriver' => 'postgre',
'DBPrefix' => 'db_',
'pConnect' => false,
'DBDebug' => (ENVIRONMENT !== 'production'),
'cacheOn' => false,
'cacheDir' => '',
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port' => 5433,
];
I don't know what I'm doing wrong, I understand that the .env file overwrites the config\App.php configuration, and gives me the error CodeIgniter\Database\Exceptions\DatabaseException #8 and show throw new DatabaseException('Unable to connect to the database.');
Someone who knows more about the subject who can help me would greatly appreciate it.
Your .env file will overwrite the config/App.php settings. However you are setting the values on commented items.
All the values that start with # are commented. You need to remove it. However like you see there are two database groups. One for running unit test the other to run your app. The default one runs your app. That's the one you need to uncomment. Like so:
Note that I wrote the Postgre in uppercase as is that is how is referenced in the docs.
For more information on this subject you can find more here:
https://codeigniter.com/user_guide/database/configuration.html?highlight=env#configuring-with-env-file