Yii Framework Forum: Test database connection failed with Postgres - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Test database connection failed with Postgres

#1 User is offline   namanhams 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 08-November 10

Posted 08 November 2010 - 04:10 AM

Hi,

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' :

'db'=>array(
	'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');
$connection->setActive(true);


and it's still ok.


Please help. Thanks.
0

#2 User is offline   jefftulsa 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 168
  • Joined: 06-October 08
  • Location:Austin, TX

Posted 08 November 2010 - 06:24 PM

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

#3 User is offline   namanhams 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 08-November 10

Posted 08 November 2010 - 09:49 PM

View Postjefftulsa, on 08 November 2010 - 06:24 PM, said:

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 :

<?php

class DbTest extends CTestCase
{
	public function testConnection()
	{
		$this->assertNotEquals(NULL, Yii::app()->db);
	}
}

?>

0

#4 User is offline   jefftulsa 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 168
  • Joined: 06-October 08
  • Location:Austin, TX

Posted 08 November 2010 - 11:33 PM

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.
0

#5 User is offline   namanhams 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 08-November 10

Posted 09 November 2010 - 01:19 AM

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

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users