views:

464

answers:

1

Edits/Additions at bottom...

Environment:

Mac OS X 10.6 Snow Leopard
PHP 5.3
Kohana 3.0.4

When I try to configure and use a connection to a postgresql database on localhost I get the following error:

ErrorException [ Warning ]: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock)

Here is the configuration of the database in /modules/database/config/database.php (note the third instance named 'pgsqltest')

return array
(
'default' => array
(
    'type'       => 'mysql',
    'connection' => array(
        /**
         * The following options are available for MySQL:
         *
         * string   hostname
         * string   username
         * string   password
         * boolean  persistent
         * string   database
         *
         * Ports and sockets may be appended to the hostname.
         */
        'hostname'   => 'localhost',
        'username'   => FALSE,
        'password'   => FALSE,
        'persistent' => FALSE,
        'database'   => 'kohana',
    ),
    'table_prefix' => '',
    'charset'      => 'utf8',
    'caching'      => FALSE,
    'profiling'    => TRUE,
),
'alternate' => array(
    'type'       => 'pdo',
    'connection' => array(
        /**
         * The following options are available for PDO:
         *
         * string   dsn
         * string   username
         * string   password
         * boolean  persistent
         * string   identifier
         */
        'dsn'        => 'mysql:host=localhost;dbname=kohana',
        'username'   => 'root',
        'password'   => 'r00tdb',
        'persistent' => FALSE,
    ),
    'table_prefix' => '',
    'charset'      => 'utf8',
    'caching'      => FALSE,
    'profiling'    => TRUE,
),
'pgsqltest' => array(
    'type'       => 'pdo',
    'connection' => array(
        /**
         * The following options are available for PDO:
         *
         * string   dsn
         * string   username
         * string   password
         * boolean  persistent
         * string   identifier
         */
        'dsn'        => 'mysql:host=localhost;dbname=pgsqltest',
        'username'   => 'postgres',
        'password'   => 'dev1234',
        'persistent' => FALSE,
    ),
    'table_prefix' => '',
    'charset'      => 'utf8',
    'caching'      => FALSE,
    'profiling'    => TRUE,
),
);

And here is the code to create the database instance, create a query and execute the query:

$pgsqltest_db  = Database::instance('pgsqltest');
$query = DB::query(Database::SELECT, 'SELECT * FROM test')->execute();

I'm continuing to research a solution for this error but thought I'd ask to see if someone else has already found a solution. Any ideas are welcome.

One other note is that I know my build of PHP can access this postgresql db since I'm able to manage the db using phpPgAdmin. But I have yet to determine what phpPgAdmin is doing differently to connect to the db than what Kohana 3 is attempting.

Bart

///////////// EDIT ONE /////////////

Based on Matt's comment I changed the following in the configuration of the 'pgsqltest' database instance.

from

'dsn'        => 'mysql:host=localhost;dbname=pbeeep',  

to

'dsn'        => 'pgsql:host=localhost;dbname=pbeeep',  

I also changed the execution of the query.

from

$query = DB::query(Database::SELECT, 'SELECT * FROM test')->execute();  

to

$query = DB::query(Database::SELECT, 'SELECT * FROM test')->execute($pgsqltest_db);  

Now I get the following error

PDOException [ 0 ]: could not find driver

I'm not sure if this is progress or not but it's more info to share.

A: 

My first comment is that you have Kohana configured to use mysql 'type' => 'mysql',. Try updating that for now and get back to us.

Matt S
Hi Matt, thanks for your quick response. Yes, the default database instance has a type of 'mysql'. However, I think I'm connecting with the third database instance defined in the above config called 'pgsqltest'. For this instance the type is defined as 'pdo'. I've tried changing the default configuration to be my pdo connection and that fails as well. I can post that setup if you like.
Bart Gottschalk
Huzah! One should scroll down before commenting. I apologize. Do you have the OSX PHP PGSQl drivers installed? I believe http://www.php.net/manual/en/ref.pdo-pgsql.php has some more info and `phpinfo()` should tell you.
Matt S
Matt, spot on. I'm currently tracking down why I don't have those drivers installed. I currently only have these drivers installed according to phpinfo() - mysql, sqlite, sqlite2. More to come...
Bart Gottschalk
I installed this extension extension=pdo_pgsql.so and added it to php.ini and all is good now. For anyone else who runs into this issue I followed the instructions to install this found in this thread: http://stackoverflow.com/questions/61747/installing-pdo-drivers-for-postgresql-on-mac-using-zend-for-eclipse/1286153#1286153
Bart Gottschalk