Test database connection failed with Postgres


I’ve followed all the instructions in chapter 4 to create a test file for the database connection.

Here’s the settings in ‘main.php’ :


	'connectionString' => 'pgsql:host=localhost;port=5432;dbname=trackstar',

	'username' => 'myusername',

	'password' => 'mypassword'


The error is :

CDbException: CDbConnection failed to open the DB connection.

The strange thing is, i can connect to the database in the application. Inside ‘protected/views/index.php’ , i have :

try {

        echo 'Connecting...<br/>';

	$connection = Yii::app()->db;  // (*)

	echo ($connection ? 'Successful' : 'Failed');


catch(Exception $ex) {

	echo $ex->getMessage();


It actually returns ‘Successful’. Or i can replace (*) in the code above with

$connection = new CDbConnection('pgsql:host=localhost;port=5432;dbname=trackstar','myusername','mypassword');


and it’s still ok.

Please help. Thanks.

What exact action are you taking that is generating the Exception? Is this the result of running a unit test?

Yes i run the unit test in Chapter 4 and it failed.

Here’s the test :


class DbTest extends CTestCase


	public function testConnection()


		$this->assertNotEquals(NULL, Yii::app()->db);




Well, the unit test will be loading the test.php config file. Do you happen to have a different ‘db’ component specified in this file (/protected/config/test.php)?

One thing to try is to load the interactive shell using the test.php config and then attempt to see what Yii::app()->db is:

From within your application’s /protected directory issue:

$ ./yiic shell config/test.php

Which should run the shell:

Yii Interactive Tool v1.1 (based on Yii v1.1.2)

Please type 'help' for help. Type 'exit' to quit.


Then try:

>> echo isset(Yii::app()->db);

and see if you get back a 1 (which means everything is good) or if this also generates the DBException.

Actually i only have the db component in the ‘main.php’, but i dont think it’s the problem, because when i replace Postgres with MySQL, the test passes.

I’ve put the db component in ‘test.php’, but the test still failed.

When i run

>> echo isset(Yii::app()->db);

it throws Exception :

exception 'CDbException' with message 'CDbConnection failed to open the DB connection: could not find driver' in /Applications/MAMP/Library/yii/framework/db/CDbConnection.php:272