We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

[Solved] Webtools not working with Postgres (app is working)

I have an app that is working with Postgres, but the developer tools webtools.php won't work (it's trying to connect to MySQL).

Here is my config.ini:

[database]
host     = localhost
username = postgres
password = 
name     = databasename
schema   = public

Here is my index.php

$di->set('db', function() use ($config) {
        return new \Phalcon\Db\Adapter\Pdo\Postgresql(array(
            "host" => $config->database->host,
            "username" => $config->database->username,
            "password" => $config->database->password,
            "dbname" => $config->database->name,
            "schema" => $config->database->schema
        ));
});

This is the output I get from the webtools (notice #0 is trying to connect to MySQL):

PDOException: could not find driver
  #0 [internal function]: PDO->__construct('mysql:host=loca...', 'postgres', '', Array)
  #1 [internal function]: Phalcon\Db\Adapter\Pdo->connect(Array)
  #2 /var/www/html/phalcon-devtools/scripts/Phalcon/Web/Tools.php(313): Phalcon\Db\Adapter\Pdo->__construct(Array)
  #3 [internal function]: Phalcon\Web\{closure}()
  #4 [internal function]: Phalcon\DI\Service->resolve(NULL, Object(Phalcon\DI\FactoryDefault))
  #5 [internal function]: Phalcon\DI->get('db', NULL)
  #6 /var/www/html/phalcon-devtools/scripts/Phalcon/Web/Tools.php(181): Phalcon\DI->getShared('db')
  #7 /var/www/html/phalcon-devtools/scripts/Phalcon/Web/Tools/controllers/ControllerBase.php(61): Phalcon\Web\Tools::getConnection()
  #8 /var/www/html/phalcon-devtools/scripts/Phalcon/Web/Tools/controllers/ModelsController.php(30): ControllerBase->listTables(true)
  #9 [internal function]: ModelsController->indexAction()
  #10 [internal function]: Phalcon\Dispatcher->dispatch()
  #11 /var/www/html/phalcon-devtools/scripts/Phalcon/Web/Tools.php(322): Phalcon\Mvc\Application->handle()
  #12 /var/www/html/project-dir/public/webtools.php(26): Phalcon\Web\Tools::main('/var/www/html/p...', '{my ip}')
  #13 {main}

Thanks



7.7k
Accepted
answer
edited Mar '14

I was able to get it working.

I didn't have "adapter" configured in the config.ini or index.php. I also had "name" in config.ini, when it should have been "dbname" (the app worked fine, but the dev tools caught that).

New config.ini:

[database]
adapter  = Postgresql
host     = localhost
username = postgres
password = 
dbname     = databasename
schema   = public

New index.php:

$di->set('db', function() use ($config) {
        return new \Phalcon\Db\Adapter\Pdo\Postgresql(array(
            'adapter' => $config->database->adapter,
            'host' => $config->database->host,
            'username' => $config->database->username,
            'password' => $config->database->password,
            'dbname' => $config->database->dbname,
            'schema' => $config->database->schema
        ));
    });


7.7k
edited Mar '14

Correction, the index.php shouldn't have the adapter declaration, but the config should:

$di->set('db', function() use ($config) {
return new \Phalcon\Db\Adapter\Pdo\Postgresql(array(
'host' => $config->database->host,
'username' => $config->database->username,
'password' => $config->database->password,
'dbname' => $config->database->dbname,
'schema' => $config->database->schema
));
});

Hello, he change is in services.php

From: use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;

To: use Phalcon\Db\Adapter\Pdo\Postgresql as DbAdapter;

and the app work fine.

Regards