How to build Laravel + PostgreSQL project?

229 views Asked by At

I want to build Laravel + PostgreSQL existing Project.

config.php

 'pgsql' => [
            'driver' => 'pgsql',
            'url' => env('DATABASE_URL', "pgsql://postgres:[email protected]:5432/crawler"),
            'host' => env('DB_POSTGRES_HOST', '127.0.0.1'),
            'port' => env('DB_POSTGRES_PORT', '5432'),
            'database' => env('DB_POSTGRES_DATABASE', 'crawler'),
            'username' => env('DB_POSTGRES_USERNAME', 'postgres'),
            'password' => env('DB_POSTGRES_PASSWORD', '123'),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
            'schema' => 'public',
            'sslmode' => 'prefer'
        ],

I tried to migrate the database.

But error occured.

   Illuminate\Database\QueryException  : could not find driver (SQL: select * from information_schema.tables where table_schema = public and table_name = migrations and table_type = 'BASE TABLE')

  at E:\Task\cookie.crawler.backend.api\vendor\laravel\framework\src\Illuminate\Database\Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error
    666|         // message to include the bindings with SQL, which will make this exception a
    667|         // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e
    671|             );
    672|         }
    673|

  Exception trace:

  1   Doctrine\DBAL\Driver\PDO\Exception::("could not find driver")
      E:\Task\cookie.crawler.backend.api\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDO\Exception.php:18

  2   Doctrine\DBAL\Driver\PDO\Exception::new(Object(PDOException))
      E:\Task\cookie.crawler.backend.api\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php:44

  Please use the argument -v to see more details.

What's the reason.

I've installed PostgreSQL recently and not familiar with this.

2

There are 2 answers

5
mohammad mousavi On

The error message you are receiving means that you do not have the PostgreSQL driver installed for PHP, and therefore Laravel is unable to connect to the database, resulting in an error.

Check if the PostgreSQL driver is installed :

php -m | grep pdo_pgsql

If not installed:

sudo apt-get install php-pgsql

After installing , restart your web server:

sudo service apache2 restart

If you are using Windows, Download the PostgreSQL driver and place the downloaded file in your PHP installation directory. For example, choose the path

C:\xampp\php\ext.

and

find the following line in the php.ini file: ;extension=php_pgsql.dll and uncomment it by removing the semicolon at the beginning of the line.

1
Special Man On

I just got an answer. Find php.ini in xampp/php folder. add 2 things. extension=pdo_pgsql extension=pgsql If exist, remove ; from the line. It will works well.