I am trying to connect to PostgreSQL using Codeigniter framework. Now in my database.php
I have the following code :
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'postgres',
'password' => '',
'database' => 'fmsdb',
'dbdriver' => 'postgre',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
But When I run my site in localhost, I get following database error :
A PHP Error was encountered
Severity: Warning
Message: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Permission denied Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? could not connect to server: Permission denied Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?
Filename: postgre/postgre_driver.php
Line Number: 154
I tried putting this in my PostgreSQL.conf file :
listen_addresses = '*'
Where am I going wrong?
In CodeIgniter, go to application/config/databse. php for database configuration file. In database. php file, fill the entries to connect CodeIgniter folder to your database.
CodeIgniter has a config file that lets you store your database connection values (username, password, database name, etc.). The config file is located at application/config/database. php. You can also set database connection values for specific environments by placing database.
PHP provides many functions for working directly with PostgreSQL databases. To connect to PostgreSQL using native functions, follow these steps: Use the following PHP code to connect to PostgreSQL and select a database.
Using $this->load->database(); will throw a error page if the connection failed or for example the config file does not exists.
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 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',
);
Database-configuration in codeigniter.com
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With