Yavuz
Yavuz

Reputation: 1393

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:

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.

Upvotes: 6

Views: 20090

Answers (4)

Muhammad Afzaal
Muhammad Afzaal

Reputation: 31

There are different php version installed on your WAMP SERVER. when you click on WAMP icon it will show that pgsql and pdo_pgsql are active but for different php version. You need to check your php version using php -v command and then go to the WAMP folder and find that php version and edit php.ini file and enable pgsql extensions there.

Upvotes: 0

Yavuz
Yavuz

Reputation: 1393

I solved the issue. It is a bug at WAMP I think. When I edit php.ini from WAMP's menu it opens a different php.ini than active PHP version's. So I opened php.ini from file system and edited it from there.

Upvotes: 2

albus_severus
albus_severus

Reputation: 3712

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

Upvotes: 11

Bram Verstraten
Bram Verstraten

Reputation: 1557

Verify your PHP version with php -v

Install php7.2-pgsql when needed.

Upvotes: 2

Related Questions