php Laravel:错误 [PDOException]:无法在 PostgreSQL 中找到驱动程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25329302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:47:35  来源:igfitidea点击:

Laravel: Error [PDOException]: Could not Find Driver in PostgreSQL

phpdatabasepostgresqllaravelsyntax-error

提问by Hyperion

I'm trying to connect with PostgreSQL database through Laravel in order to do a php artisan migrate but doesn't seem to be directed since it's reading the database name of MySQL.

我正在尝试通过 Laravel 连接 PostgreSQL 数据库以进行 php artisan 迁移,但似乎没有被引导,因为它正在读取 MySQL 的数据库名称。

Here are the commands from database.php:

以下是来自 database.php 的命令:

'connections' => array(

    'sqlite' => array(
        'driver'   => 'sqlite',
        'database' => __DIR__.'/../database/production.sqlite',
        'prefix'   => '',
    ),

    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'database',
        'username'  => 'root',
        'password'  => '',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

    'pgsql' => array(
        'driver'   => 'pgsql',
        'host'     => 'localhost',
        'database' => 'postgres',
        'username' => 'postgres',
        'password' => 'root',
        'charset'  => 'utf8',
        'prefix'   => '',
        'schema'   => 'public',
    ),

    'sqlsrv' => array(
        'driver'   => 'sqlsrv',
        'host'     => 'localhost',
        'database' => 'database',
        'username' => 'root',
        'password' => '',
        'prefix'   => '',
    ),

),

If I remove the MySQL paths I'll get:

如果我删除 MySQL 路径,我会得到:

[InvalidArgumentException]
Database [mysql] not configured.



EDIT:编辑:尝试执行 php artisan migrate 时,我收到“PDOException:找不到驱动程序”。我正在使用 WAMP 并且我在 Win8.1 中。使用 PostgreSQL 作为数据库。



EDIT:编辑:已经尝试了一系列替代解决方案,但我仍然应该解决这个问题。该php.iniphp.ini文件已在 Apache、WAMP(来自 php 文件夹)和 PostgreSQL 中检查。这extension_dirextension_dir是正确的 - >extension_dir = "c:/wamp/bin/php/php5.5.12/ext/"extension_dir = "c:/wamp/bin/php/php5.5.12/ext/"

The extension=pdo_pgsql.dlland extension=pgsql.dllare uncommented.

extension=pdo_pgsql.dllextension=pgsql.dll是注释。

Done the PATHtrick in the 'System Variables' and rebooted. No chance.

完成PATH“系统变量”中的技巧并重新启动。没有机会。

Thanks for the help so far.

感谢你目前的帮助。

These are my drivers php_pdo_driver.h& php_pdo.hfrom C:\Program Files (x86)\PostgreSQL\EnterpriseDB-ApachePHP\php\SDK\include\ext\pdo

这些是我的司机php_pdo_driver.h&php_pdo.h来自C:\Program Files (x86)\PostgreSQL\EnterpriseDB-ApachePHP\php\SDK\include\ext\pdo

Information from phpinfo:

来自phpinfo的信息:

PHP Version 5.5.12

Compiler MSVC11 (Visual C++ 2012) Configure Command cscript /nologo configure.js "--enable-snapshot-build" "--disable-isapi" "--enable-debug-pack" "--without-mssql" "--without-pdo-mssql" "--without-pi3web" "--with-pdo-oci=C:\php-sdk\oracle\x64\instantclient10\sdk,shared" "--with-oci8=C:\php-sdk\oracle\x64\instantclient10\sdk,shared" "--with-oci8-11g=C:\php-sdk\oracle\x64\instantclient11\sdk,shared" "--enable-object-out-dir=../obj/" "--enable-com-dotnet=shared" "--with-mcrypt=static" "--disable-static-analyze" "--with-pgo"

PHP 版本 5.5.12

编译器 MSVC11 (Visual C++ 2012) 配置命令 cscript /nologo configure.js "--enable-snapshot-build" "--disable-isapi" "--enable-debug-pack" "--without-mssql" "-- without-pdo-mssql" "--without-pi3web" "--with-pdo-oci=C:\php-sdk\oracle\x64\instantclient10\sdk,shared" "--with-oci8=C:\php -sdk\oracle\x64\instantclient10\sdk,shared" "--with-oci8-11g=C:\php-sdk\oracle\x64\instantclient11\sdk,shared" "--enable-object-out-dir= ../obj/" "--enable-com-dotnet=shared" "--with-mcrypt=static" "--disable-static-analyze" "--with-pgo"

回答by lainceline

Be sure to configure the 'default'key in app/config/database.php

一定要配置好'default'keyapp/config/database.php

For postgres, this would be 'default' => 'postgres',

对于 postgres,这将是 'default' => 'postgres',

If you are receiving a [PDOException] could not find drivererror, check to see if you have the correct PHP extensions installed. You need pdo_pgsql.soand pgsql.soinstalled and enabled. Instructions on how to do this vary between operating systems.

如果您收到[PDOException] could not find driver错误消息,请检查您是否安装了正确的 PHP 扩展。你需要pdo_pgsql.sopgsql.so安装并启用。有关如何执行此操作的说明因操作系统而异。

For Windows, the pgsqlextensions should come pre-downloaded with the official PHP distribution. Just edit your php.iniand uncomment the lines extension=pdo_pgsql.soand extension=pgsql.so

对于 Windows,pgsql扩展应该与官方 PHP 发行版一起预先下载。只需编辑php.ini并取消注释行extension=pdo_pgsql.soextension=pgsql.so

Also, in php.ini, make sure extension_diris set to the proper directory. It should be a folder called extensionsor extor similar inside your PHP install directory.

此外,在 中php.ini,确保extension_dir设置为正确的目录。它应该是PHP 安装目录中名为extensionsext类似的文件夹。

Finally, copy libpq.dllfrom C:\wamp\bin\php\php5.*\into C:\wamp\bin\apache*\binand restart all services through the WampServer interface.

最后,通过 WampServer 接口libpq.dll从复制C:\wamp\bin\php\php5.*\C:\wamp\bin\apache*\bin并重新启动所有服务。

If you still get the exception, you may need to add the postgres \bindirectory to your PATH:

如果您仍然遇到异常,您可能需要将 postgres\bin目录添加到您的PATH

  1. System Properties -> Advanced tab -> Environment Variables
  2. In 'System variables' group on lower half of window, scroll through and find the PATHentry.
  3. Select it and click Edit
  4. At the end of the existing entry, put the full path to your postgres bin directory. The bin folder should be located in the root of your postgres installation directory.
  5. Restart any open command prompts, or to be certain, restart your computer.
  1. 系统属性 -> 高级选项卡 -> 环境变量
  2. 在窗口下半部分的“系统变量”组中,滚动并找到PATH条目。
  3. 选择它并单击编辑
  4. 在现有条目的末尾,输入 postgres bin 目录的完整路径。bin 文件夹应位于 postgres 安装目录的根目录中。
  5. 重新启动任何打开的命令提示符,或者确定重新启动您的计算机。

This should hopefully resolve any problems. For more information see:

这应该有望解决任何问题。有关更多信息,请参阅:

回答by Nasif Md. Tanjim

For PDOException: could not find driverfor MySQL, and if it is Debianbased OS,

对于PDOException: could not find driverfor MySQL,如果它是Debian基于操作系统的,

sudo apt-get -y install php5-mysql

回答by Святослав Павленко

For PHP 7 in Ubuntu you can also do:

对于 Ubuntu 中的 PHP 7,您还可以执行以下操作:

sudo apt-get install php7.0-pgsql

So, now you can do not uncomment lines in php.ini

所以,现在你不能取消注释行 php.ini

UPD:I have a same error, so problem was not in driver. I changed my database.ini, but every time I saw an error. And I change database config in .envand errors gone.

UPD:我有同样的错误,所以问题不在驱动程序中。我更改了我的database.ini,但每次看到错误时。我更改了数据库配置.env,错误消失了。

回答by Lucky

This worked for me:

这对我有用:

$ sudo apt-get install php-gd php-mysql

$ sudo apt-get install php-gd php-mysql

回答by William Brawner

I realize this is an old question but I found it in a Google search so I'm going to go ahead and answer just in case someone else runs across this. I'm on a Mac and had the same issue, but solved it by using HomeBrew. Once you've got it installed, you can just run this command:

我意识到这是一个老问题,但我在谷歌搜索中找到了它,所以我会继续回答,以防其他人遇到这个问题。我在 Mac 上遇到了同样的问题,但通过使用HomeBrew解决了它。安装完成后,您可以运行以下命令:

brew install php56-pdo-pgsql

And replace the 56 with whatever version of PHP you're using without the decimal point.

并将 56 替换为您使用的任何版本的 PHP,不带小数点。

回答by user3293860

I had the same issue. This is what worked for me.

我遇到过同样的问题。这对我有用。

There are 2 php.ini files:

有 2 个 php.ini 文件:

  • C:\wamp\bin\apache\apache2.4.9\bin
  • C:\wamp\bin\php\php5.5.12
  • C:\wamp\bin\apache\apache2.4.9\bin
  • C:\wamp\bin\php\php5.5.12

NOTE: This is using my version of PHP and Apache, change to what yours are.

注意:这是使用我的 PHP 和 Apache 版本,请更改为您的版本。

The php.ini file located in the both folders is what you need to update, the extentions:

位于这两个文件夹中的 php.ini 文件是您需要更新的文件,扩展名:

  • extension=php_pdo_pgsql.dll
  • extension=php_pgsql.dll
  • 扩展名=php_pdo_pgsql.dll
  • 扩展名=php_pgsql.dll

These are what you need to uncomment (remove the ; symbol).

这些是您需要取消注释的内容(删除 ; 符号)。

Restart both Wamp and the Command Prompt.

重新启动 Wamp 和命令提示符。

Hopefully it will work for you :).

希望它对你有用:)。

回答by Sujal Patel

sudo apt-get install php7.1-pgsql

回答by Ash

I had the same error on PHP 7.3.7 docker with laravel:

我在使用 laravel 的 PHP 7.3.7 docker 上遇到了同样的错误:

This works for me

这对我有用

apt-get update && apt-get install -y libpq-dev && docker-php-ext-install pdo pgsql pdo_pgsql

This will install the pgsql and pdo_pgsql drivers.

这将安装 pgsql 和 pdo_pgsql 驱动程序。

Now run this command to uncomment the lines extension=pdo_pgsql.so and extension=pgsql.so from php.ini

现在运行此命令以取消注释 php.ini 中的 extension=pdo_pgsql.so 和 extension=pgsql.so

sed -ri -e 's!;extension=pdo_pgsql!extension=pdo_pgsql!' $PHP_INI_DIR/php.ini

sed -ri -e 's!;extension=pgsql!extension=pgsql!' $PHP_INI_DIR/php.ini

回答by Netwons

my php version 7.2.

我的 php 版本 7.2。

my system is ubuntu.

我的系统是ubuntu。

Just edit your php.ini and uncomment the lines extension=pdo_pgsql.so and extension=pgsql.so.

只需编辑您的 php.ini 并取消注释 extension=pdo_pgsql.so 和 extension=pgsql.so 行。

but yet this error:

但是这个错误:

could not find driver (SQL: select * from information_schema.tables where table_schema = public and table_name = migrations and table_type = 'BASE TABLE')

回答by Jozsa-Csiki Lorant Csaba

Solved after 3 hours... I am using WAMP (PHP 7.2.4), PostgreSQL 10, Laravel 5.6.29. Loaded PHP extensions (pgsql, pdo_pgsql) from Wampserver 3.3, then I was able to connect to the PostgreSQL server with a simple php testcode from the www directory. But $ php artisan migratestill returned

3 小时后解决...我使用的是 WAMP(PHP 7.2.4)、PostgreSQL 10、Laravel 5.6.29。从 Wampserver 3.3 加载 PHP 扩展(pgsql、pdo_pgsql),然后我能够使用 www 目录中的简单 php 测试代码连接到 PostgreSQL 服务器。但$ php artisan migrate还是回来了

PDOException::("could not find driver")

PDOException::("找不到驱动程序")

I checked the shell (git bash for windows) with $ php --ini, that returned C:\wamp64\bin\php\php7.2.4\php.ini instead of c:\wamp64\bin\apache\apache2.4.33\bin\php.ini, loaded by WAMP So you have to uncomment pgsql and pdo_pgsql extensions also in C:\wamp64\bin\php\php7.2.4\php.ini, and then migrate will work ...

我检查了外壳(Windows 的 git bash)$ php --ini,返回 C:\wamp64\bin\php\php7.2.4\php.ini 而不是 c:\wamp64\bin\apache\apache2.4.33\bin\php.ini , 由 WAMP 加载所以你必须在 C:\wamp64\bin\php\php7.2.4\php.ini 中取消注释 pgsql 和 pdo_pgsql 扩展,然后迁移将工作......