How to connect to POSTGRESQL in Codeigniter 3?
First enable Postgresql
extension in php.ini
extension=php_pgsql.dll
You also can enable Postgresql
extension for PDO as well.
extension=php_pdo_pgsql.dll
$db['default'] = array( 'port' => 5432, # Add );
OR
$db['default'] = array( 'dsn' => 'pgsql:host=localhost;port=5432;dbname=database_name', 'dbdriver' => 'pdo',);
Tested with Codeigniter 4, PHP 7.3 and PostgreSQL 9.3.5:
1) Enable in your php.ini
extension=php_pgsql.dll
2) In app/Config/Database class override your $default property as follows:
/** * The default database connection. * * @var array */public $default = [ 'DSN' => '', 'hostname' => 'your_host', 'username' => 'your-user', 'password' => 'your-password', 'database' => 'your-database', 'DBDriver' => 'postgre', 'DBPrefix' => '', 'pConnect' => false, 'DBDebug' => (ENVIRONMENT !== 'production'), 'cacheOn' => false, 'cacheDir' => '', 'charset' => 'utf8', 'DBCollat' => 'utf8_general_ci', 'swapPre' => '', 'encrypt' => false, 'compress' => false, 'strictOn' => false, 'failover' => [], 'port' => 5432, //the default port ];
First enable these two extensions
extension=php_pgsql.dllextension=php_pdo_pgsql.dll
- Then restart apache
- Add $db['default']['port'] = 5432 in database.php file with all other codes.