2

I have tried to connect PostgreSQL with Yii but is is showing

CDbConnection failed to open the DB connection.

In log it is showing

error   exception.CDbException  

could not find driver

My code

'db'=>array(
    'tablePrefix'=>'',
    'connectionString' => 'pgsql:host=localhost;port=5432;dbname=postgres',
    'username'=>'postgres',
    'password'=>'postgres',
    'charset'=>'UTF8',
),
Cœur
  • 37,241
  • 25
  • 195
  • 267
rajmohan
  • 1,618
  • 1
  • 15
  • 36

5 Answers5

2

Yii Database component is build on top of PHP PDO. So Here are list of posibilities to check

Check that PostgreSQL and PostgreSQL's PDO Extension is installed and working. Also that port is right one. You can test simple PHP Script to test.

Below are links to something that might be similar

PostgreSql 'PDOException' with message 'could not find driver'

PDOException “could not find driver”

Install PDO on Linux (Ubuntu/Debian)

Install pdo for postgres Ubuntu

https://serverfault.com/questions/89970/how-to-install-postgresql-extension-for-pdo-in-linux

Windows

http://blog.lysender.com/2010/08/php-and-postgresql-on-windows/

Apache php 5.3 postgreSQL driver could not be loaded

Official PHP Information on Issue

http://php.net/manual/en/pdo.installation.php

http://www.php.net/manual/en/ref.pdo-pgsql.php

Community
  • 1
  • 1
Stefano Mtangoo
  • 6,017
  • 6
  • 47
  • 93
  • in my phpinfo()...the pdo support is not enabled for postgressql..it is there for mysql and sqlite. how can i fix this – rajmohan Oct 30 '12 at 12:05
  • thank you for all these links....tried everything but the issue was resolved by following step... – rajmohan Oct 30 '12 at 14:46
1

Resolved issue...by adding

LoadFile "C:/wamp/bin/php/php5.3.5/libpq.dll"

to httpd.conf file. Now phpinfo() is showing pgsql PDO.

thanks...

rajmohan
  • 1,618
  • 1
  • 15
  • 36
  • 1
    That is bad way actually. Any upgrade that will overwrite config will result in need for you to re-write config file. If you read my first link on windows there is better way there. You *should* have followed that and accepted my answer :) here I quote "The PHP extension will look for libpq.dll which is found of your PostgreSQL installation. The simple fix is to add the path than contains that file to your environment PATH. Here is the steps I’ve taken." – Stefano Mtangoo Oct 30 '12 at 20:26
  • 1
    so Just add C:/wamp/bin/php/php5.3.5/ to your system path variable restart your machine (if necessary) and you are done! – Stefano Mtangoo Oct 30 '12 at 20:27
1

if you work in ubuntu make sure that you have installed pgsql library

sudo apt-get install php-pgsql
sudo service apache2 restart

after make sure that in the console and main-local there are this array

'components' => [
    'log' => [
        'targets' => [
            [
                'class' => 'yii\log\FileTarget',
                'levels' => ['error', 'warning'],
            ],
        ],
    ],
    'db' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'pgsql:host=localhost;port=5432;dbname=yourdb',
        'username' => 'user',
        'password' => 'pw',
        'charset' => 'utf8',
    ],],
Andrea Perdicchia
  • 2,786
  • 1
  • 20
  • 19
0

In my case, it was a case of enabling the php_pdo_pgsql extension in WAMP

Igbanam
  • 5,904
  • 5
  • 44
  • 68
0

for those struggling with the same issue i did the work around. environment Yii2 server:LAMP stack 7.1 problem Driver not found i.e the "requirements.php" shows warning "PDO PostgreSQL extension Warning All DB-related classes Required for PostgreSQL database." i noted that pdo_pgsql.so is not packaged together with bitnami so i search for it online or try locate if installed on any php versions in /usr/lib/php

  1. copy that file to lampstack-7.1.10-1/php/lib/php/extensions.
  2. now go to lampstack php.ini "lampstack-7.1.10-1/php/etc/php.ini" now place this line "extension=pdo_pgsql.so" remove ; if commented out NB you can point to the absolute path "/usr/lib/php/pdo_pgsql.so" if it existed there
  3. restart Lamp apache server
  4. visit requirement.php and if you see ""PDO PostgreSQL extension Passed All DB-related classes Required for PostgreSQL database.""