Search code examples
yii2yii2-basic-app

error while creating table using Yii2 migration


I'm pretty new to yii2, I'm using postgres, my db.php file is -

return [
       'class' => 'yii\db\Connection',
       'dsn' => 'pgsql:host=localhost;port=5432;dbname=xxxx',
       'username' => 'postgres',
       'password' => 'abc',
       'charset' => 'utf8',
];

I have create one table directly in postgres and fetched the data successfully using ActiveRecord. Then I went for creating a table using migration with

./yii migrate/create logins

which successfully created a file in migration folder, then i put following content in up method -

public function up()
{
    $this->createTable('logins', [
            'id' => Schema::TYPE_PK,
            'name' => Schema::TYPE_STRING . ' NOT NULL',
            'password' => Schema::TYPE_STRING . ' NOT NULL'
    ]);
}

and fired ./yii migrate to update a database, but I'm getting following error -

Yii Migration Tool (based on Yii v2.0.10)
Exception 'yii\db\Exception' with message 'could not find driver'
in /opt/lampp/htdocs/project/server/api/project/vendor/yiisoft/yii2/db/Connection.php:549

Is there any step I'm missing? or there is some problem in postgres connection?


Solution

  • I assume this is basic template.

    Make sure you have got db component configured.

    In your config/console.php file check if there is db key in components section like:

    // ...
    'components' => [
        // ...
        'db' => require(__DIR__ . '/db.php'),
        // ...
    ],
    

    If everything is ok with it but error is still present you need to check if you have got pgsql driver properly installed.

    See PDO Installation for details.