Olivier
Olivier

Reputation: 11

I can't connect to Postgresql with Laravel

I'm having trouble using Postgresql 12.3 with Laravel 7.24.0 Here's the error message I get:

Illuminate\Database\QueryException
could not find driver (SQL: insert into "posts" ("title", "content", "updated_at", "created_at") values (aze, bcb, 2020-08-11 18:02:26, 2020-08-11 18:02:26) returning "id") 

Here's my setup (I tested the settings (host, port, username, password, and db_name) with DBeaver and with the command line):

.env :

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=<db_name>
DB_USERNAME=<user_name>
DB_PASSWORD=<password>

database.php:

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

And here's what I've tried:

  1. composer require doctrine/dbal
  2. Edit /etc/php/7.4/apache2/php.ini and uncommented extension=pdo_pgsql and extension=pgsql
  3. apt-get install php-pgsql && sudo systemctl restart apache2
  4. As the double quotes around the fields name and not the values confused me, I executed a raw SQL query to make sure the syntax was correct (I tested it with DBeaver).
$sqlQuery = "insert into posts 
             (title, post, updated_at, created_at) values
             ('aze', 'bcb', '2020-08-11 17:10:47', '2020-08-11 17:10:48') returning id";
$result = DB::select(DB::raw($sqlQuery));
  1. I ran php artisan tinker, then DB::connection()->getPdo(); on command line and got this result:
=> Doctrine\DBAL\Driver\PDOConnection {#3085
     inTransaction: false,
     attributes: {
       CASE: NATURAL,
       ERRMODE: EXCEPTION,
       PERSISTENT: false,
       DRIVER_NAME: "pgsql",
       SERVER_INFO: "PID: 27281; Client Encoding: UTF8; Is Superuser: off; Session Authorization: mespas; Date Style: ISO, DMY",
       ORACLE_NULLS: NATURAL,
       CLIENT_VERSION: "10.12 (Ubuntu 10.12-0ubuntu0.18.04.1)",
       SERVER_VERSION: "12.3 (Ubuntu 12.3-1.pgdg18.04+1)",
       STATEMENT_CLASS: [
         "Doctrine\DBAL\Driver\PDOStatement",
         [],
       ],
       EMULATE_PREPARES: false,
       CONNECTION_STATUS: "Connection OK; waiting to send.",
       DEFAULT_FETCH_MODE: BOTH,
     },
   }

Which is apparently what what I'm supposed to get

  1. I removed DATABASE_URL from database.php since it was not defined. I figure if all the other params are set, it is not necessary.

I constantly get the same message and after a few hours of research, I'm kind of out of ideas...

Upvotes: 0

Views: 3816

Answers (1)

Olivier
Olivier

Reputation: 11

All right, so following @nibnut's advice I went and disabled php 7.2 for Apache and enabled 7.4 While I was at it, I checked the version for CLI and got this message:

PHP Warning:  PHP Startup: Unable to load dynamic library 'pdo_pgsql' (tried: /usr/lib/php/20190902/pdo_pgsql (/usr/lib/php/20190902/pdo_pgsql: cannot open shared object file: No such file or directory), /usr/lib/php/20190902/pdo_pgsql.so (/usr/lib/php/20190902/pdo_pgsql.so: undefined symbol: pdo_parse_params)) in Unknown on line 0

For those interested, I found how to install it here.

It works now and my data are saved in the database, even though strangely enough I still get the message when running php -v

Thanks for the help @nibnut !

Upvotes: 1

Related Questions