Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDOException with message 'could not find driver' Error for Laravel and Postgresql

First of all, the problem is about Laravel not postgresql or PHP. I can connect postgresql with a simple PHP file. But Laravel can't do it somehow.

When I try to connect postgresql server in my computer with laravel I get "PDOException with message 'could not find driver'" error. I am getting this error when I run DB::connection()->getPdo(); command at artisan tinker.

If I run php artisan migrate command, the error is 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')

My configuration is below:

  • Windows 10
  • Wamp Server 3.1.4
  • Apache 2.4.35
  • PHP 7.2.10
  • Laravel Framework 6.0.3

Related lines of Laravel .env file:

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=laravel_dnm1
DB_USERNAME=postgres
DB_PASSWORD=mrd.BE.265

Related lines of Laravel database.php file:

'default' => env('DB_CONNECTION', 'pgsql'),

...

    'pgsql' => [
        'driver' => 'pgsql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '5432'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'prefix' => '',
        'prefix_indexes' => true,
        'schema' => 'public',
        'sslmode' => 'prefer',
    ],

When I run print_r(PDO::getAvailableDrivers()); at my server I get below output:

Array ( [0] => mysql [1] => pgsql [2] => sqlite )

Related lines of php info is below:

Screenshot of phpinfo

NOTE: There is no problem when I use mysql instead of postgresql.

NOTE2: I can connect the DB when I use regular PHP. Only Laravel gives this error.

like image 817
Yavuz Avatar asked Dec 23 '22 21:12

Yavuz


2 Answers

install postgresql

sudo apt-get install php-pgsql

then uncomment pgsql and pdo-pgsql extensions in etc/php/$PHP_VERSION/apache2/php.ini file

then restart apache2

like image 80
albus_severus Avatar answered Mar 19 '23 16:03

albus_severus


Verify your PHP version with php -v

Install php7.2-pgsql when needed.

like image 41
Bram Verstraten Avatar answered Mar 19 '23 16:03

Bram Verstraten