10

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?

3
  • this will help: stackoverflow.com/questions/29630851/… Commented Sep 21, 2016 at 7:03
  • add postgreSQL port $db['default']['port'] = 5432; also enabled the ext from php ini. extension=php_pdo_pgsql.dll Commented Sep 21, 2016 at 7:06
  • how to enable this extension in centos7 Commented Sep 21, 2016 at 8:40

3 Answers 3

10

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

Sign up to request clarification or add additional context in comments.

2 Comments

i think my pgsql is already loaded as i have installed phpPGAdmin and its working fine
u can also suggest to restart APACHE, this will help to others. i think. :)
3

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 
];

Comments

1
  1. First enable these two extensions

    extension=php_pgsql.dll
    extension=php_pdo_pgsql.dll
    
  2. Then restart apache
  3. Add $db['default']['port'] = 5432 in database.php file with all other codes.

3 Comments

I am using centos 7 will these settings be same ?
i think my pgsql is already loaded as i have installed phpPGAdmin and its working fine
check your phpinfo. if pdo_pgsql & pgsql are available, then i think will be no error. if not run yum list "php*" for available php packages. here you found package like php5-pgsql/ php-pdo_pgsql/ php-pgsql. then run install the package yum install php-pgsql & yum install php-pdo_pgsql. then restart apache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.