简体   繁体   English

我无法使用 Laravel 连接到 Postgresql

[英]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:我在使用 Postgresql 12.3 和 Laravel 7.24.0 时遇到问题这是我收到的错误消息:

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):这是我的设置(我使用 DBeaver 和命令行测试了设置(主机、端口、用户名、密码和 db_name)):

.env: .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:数据库.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编辑 /etc/php/7.4/apache2/php.ini 和未注释extension=pdo_pgsqlextension=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).由于字段名称周围的双引号而不是值使我感到困惑,因此我执行了原始 SQL 查询以确保语法正确(我使用 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();我跑了php artisan tinker ,然后是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.我从 database.php 中删除了 DATABASE_URL,因为它没有被定义。 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...我不断收到相同的信息,经过几个小时的研究,我有点没有想法......

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:好吧,所以按照@nibnut 的建议,我去为 Apache 禁用了 php 7.2 并启用了 7.4 当我在它的时候,我检查了 CLI 的版本并收到了这条消息:

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它现在可以工作并且我的数据保存在数据库中,尽管奇怪的是我在运行php -v时仍然收到消息

Thanks for the help @nibnut !感谢@nibnut 的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM