php 带有 Postgres SQL 的 Laravel 5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30480385/
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 21:48:46  来源:igfitidea点击:

Laravel 5 with Postgres SQL

phppostgresqllaravelpdolaravel-5

提问by Moyed Ansari

I'm working on Laravel 5 with postgres as database. I've configured postgres 9.4 and pgAdmin III and these are working normally. When I try to run migrate it is giving me error:

我正在使用 postgres 作为数据库开发 Laravel 5。我已经配置了 postgres 9.4 和 pgAdmin III,这些都正常工作。当我尝试运行 migrate 时,它​​给了我错误:

[PDOException]
could not find driver

[PDOException]
找不到驱动程序

This is my database.php

这是我的database.php

'default' => 'pgsql',

'pgsql' => [            'driver'   => 'pgsql',          
                        'host'     => '127.0.0.1',          
                        'database' => 'fms',            
                        'username' => 'postgres',           
                        'password' => 'root',           
                        'charset'  => 'utf8',           
                        'prefix'   => '',           
                        'schema'   => 'public',         ],

Initially I though, it was due to configuration of postgres on windows 7 but I tried with plain php it works perfect

最初我虽然,这是由于在 Windows 7 上配置了 postgres,但我尝试使用普通的 php 它工作得很好

<?php
   $host        = "host=127.0.0.1";
   $port        = "port=5432";
   $dbname      = "dbname=fms";

   $db = pg_connect( "$host $port $dbname user=postgres password=root"  );
   if(!$db){
      echo "Error : Unable to open database\n";
   } else {
      echo "Opened database successfully\n";
   }
?>

I've enabled php_pgsqland php_pdo_sqlin wamp as well. I'm not sure how to fix this on laravel 5.

我也在 wamp 中启用了php_pgsqlphp_pdo_sql。我不知道如何在 laravel 5 上解决这个问题。

回答by Sulthan Allaudeen

As you said you already choosed Default Database as Postgres SQL

正如您所说,您已经选择了默认数据库作为 Postgres SQL

'default' => 'pgsql',

It is a must that you need to uncomment the pdo and postgres shared object in your php configuration settings (php.ini)

您必须在 php 配置设置中取消对 pdo 和 postgres 共享对象的注释 (php.ini)

i.e., You need to uncomment the following lines in your php.ini

即,您需要在 php.ini 中取消注释以下行

extension=pdo_pgsql.so
extension=pgsql.so

Note :

笔记 :

Don't forget to stop and start your apacheafter doing this changes (or php-fpmif using that instead).

不要忘记apache在执行此更改后停止并开始您的更改(或者php-fpm如果使用它)。

回答by anchor

I had the same problem with Laravel-WAMP-PostgreSql driver not found exception. I even successfully established direct connection to Postgre as you have, but with no luck with the "php artisan migrate" command.

我在 Laravel-WAMP-PostgreSql 驱动程序未找到异常时遇到了同样的问题。我什至像你一样成功地建立了与 Postgre 的直接连接,但没有使用“php artisan migrate”命令。

After long research I found out that there are multiple php.ini files in which "extension=php_pdo_pgsql.dll" and "extension=php_pgsql.dll" are comented out.

经过长时间的研究,我发现有多个 php.ini 文件,其中“extension=php_pdo_pgsql.dll”和“extension=php_pgsql.dll”被注释掉了。

The solution is (of course) to uncoment the extensions in the following files:

解决方案是(当然)取消注释以下文件中的扩展名:

  • ~/wamp/bin/php/php5.5.*/php.ini
  • ~/wamp/bin/php/php5.5.*/phpForApache
  • ~/wamp/bin/php/php5.5.*/php.ini.install
  • ~/wamp/bin/php/php5.5.*/php.ini-development
  • ~/wamp/bin/php/php5.5.*/php.ini-production and
  • ~/wamp/bin/apache/apache2.4.9/php.ini
  • ~/wamp/bin/php/php5.5.*/php.ini
  • ~/wamp/bin/php/php5.5.*/phpForApache
  • ~/wamp/bin/php/php5.5.*/php.ini.install
  • ~/wamp/bin/php/php5.5.*/php.ini-development
  • ~/wamp/bin/php/php5.5.*/php.ini-production 和
  • ~/wamp/bin/apache/apache2.4.9/php.ini

** You can leave off the "php.ini-development" and "php.ini-production" (don't need to uncoment these files).

** 您可以省略“php.ini-development”和“php.ini-production”(不需要取消注释这些文件)。

回答by rinderwahn

Like @jeff said, this is probably caused by not setting DB_CONNECTION=pgsqlin the .env-file. The .env-file has MySQL preconfigured, so you mustedit this file.

就像@jeff 说的,这可能是由于未DB_CONNECTION=pgsql.env-file 中设置造成的。该.env-file已预先配置的MySQL,因此你必须编辑这个文件。

回答by Ravistm

You have to make DB related changes in

您必须在

  1. config/database.php
  2. .env file
  1. 配置/数据库.php
  2. .env 文件

and other settings in

和其他设置

  • php.ini settings
  • php.ini 设置

If you are still getting error, then clear cache and config

如果仍然出现错误,则清除缓存和配置

php artisan cache:clear
php artisan config:clear

It should work now!

它现在应该可以工作了!

回答by Ash

Run this command to easily 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

If you do not have the drivers already installed, or it gives error like unable to load dynamic library PGSQL run this:

如果您尚未安装驱动程序,或者出现无法加载动态库 PGSQL 之类的错误,请运行以下命令:

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 驱动程序。

回答by Ronald Garcia Magcalas

I'm using PHP7.3 and this worked for me, I forgot this:

我正在使用 PHP7.3,这对我有用,我忘记了:

apt-get install php7.3-pgsql

Everything runs smoothly afterwards. Just use whatever version of PHP you are using.

之后一切顺利。只需使用您正在使用的任何版本的 PHP。

回答by Jeff

you gotta modify .env file , then go to config>database.php and change the default to pgsql, and also in all your models file you need to change $protected $connection= 'pgsql'.

您必须修改 .env 文件,然后转到 config>database.php 并将默认值更改为 pgsql,并且您还需要在所有模型文件中更改 $protected $connection='pgsql'。