我无法使用Laravel连接到Postgresql



在Laravel 7.24.0中使用Postgresql 12.3时遇到问题这是我收到的错误消息:

IlluminateDatabaseQueryException
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") 

以下是我的设置(我用DBeaver和命令行测试了设置(主机、端口、用户名、密码和数据库名称(:

.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',
],
],

以下是我尝试过的:

  1. composer require doctrine/dbal
  2. 编辑/etc/php/7.4/apache2/hp.ini并取消注释extension=pdo_pgsqlextension=pgsql
  3. apt-get install php-pgsql && sudo systemctl restart apache2
  4. 由于字段名称周围的双引号而不是值让我感到困惑,我执行了一个原始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. 我在命令行上运行php artisan tinker,然后运行DB::connection()->getPdo();,得到以下结果:
=> DoctrineDBALDriverPDOConnection {#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: [
"DoctrineDBALDriverPDOStatement",
[],
],
EMULATE_PREPARES: false,
CONNECTION_STATUS: "Connection OK; waiting to send.",
DEFAULT_FETCH_MODE: BOTH,
},
}

这显然是我应该得到的

  1. 我从DATABASE.php中删除了DATABASE_URL,因为它没有定义。我想如果所有其他参数都设置好了,就没有必要了

我经常收到同样的信息,经过几个小时的研究,我有点不知所措。。。

好吧,所以按照@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

对于那些感兴趣的人,我在这里找到了如何安装它。

它现在工作了,我的数据被保存在数据库中,尽管奇怪的是,我在运行php -v时仍然收到消息

感谢你的帮助@nibnut!

相关内容

  • 没有找到相关文章

最新更新