6

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.

2
  • Do you also define this driver in your models? Show us the code that triggers the error please? Commented Sep 20, 2019 at 11:08
  • I added how I get the error to second paragraph of the question. I have a model named Product also. Commented Sep 20, 2019 at 11:36

4 Answers 4

11

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

Sign up to request clarification or add additional context in comments.

1 Comment

I have extension=pdo_pgsql and extension=pgsql in my php.ini file. They are already uncommented.
2

Verify your PHP version with php -v

Install php7.2-pgsql when needed.

2 Comments

My PHP version is 7.2.10. I am using WAMP and activated pdo_pgsql and pgsql extensions.
In that case you should follow this guide on how to install php pgsql on WAMP carnaghan.com/knowledge-base/…
2

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.