通过laravel连接Postgresql数据库的问题

时间:2016-11-06 13:21:23

标签: php postgresql laravel pdo wamp

我很难用pgsql设置laravel 5.3(在wamp / windows上运行)。当我尝试使用“php artisan migrate”迁移表时,我收到错误“PDO Exception - 找不到驱动程序”

  1. 我安装了postgres 9.6。
  2. 我启用了postgresql的php扩展:
  3. enter image description here 3.我将postgres库添加到环境路径变量

    enter image description here

    1. 我用php测试了pdo postgres连接,我正在连接:
    2. 
      
          $dbh = new PDO("pgsql:dbname=rotocms;host=127.0.0.1", "postgres", "mypassword");
      
          if($dbh){
           echo "connected";
          }else{
           echo 'there has been an error connecting';
          }
      
      
      

      所以我被困住了,无法让迁移到laravel工作。我的.env:

      
          DB_CONNECTION=pgsql
          DB_HOST=127.0.0.1
          DB_PORT=5432
          DB_DATABASE=mydatabasename
          DB_USERNAME=postgres
          DB_PASSWORD=password
      
      

      这是laravel错误堆栈:

          local.ERROR: PDOException: could not find driver in C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:119
      Stack trace:
      #0 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php(119): PDO->__construct('pgsql:host=127....', 'postgres', 'password', Array)
      #1 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php(56): Illuminate\Database\Connectors\Connector->createPdoConnection('pgsql:host=127....', 'postgres', 'password', Array)
      #2 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Connectors\PostgresConnector.php(36): Illuminate\Database\Connectors\Connector->createConnection('pgsql:host=127....', Array, Array)
      #3 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Connectors\ConnectionFactory.php(100): Illuminate\Database\Connectors\PostgresConnector->connect(Array)
      #4 [internal function]: Illuminate\Database\Connectors\ConnectionFactory->Illuminate\Database\Connectors\{closure}()
      #5 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Connection.php(964): call_user_func(Object(Closure))
      #6 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Connection.php(832): Illuminate\Database\Connection->getPdo()
      #7 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Connection.php(717): Illuminate\Database\Connection->reconnectIfMissingConnection()
      #8 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Connection.php(350): Illuminate\Database\Connection->run('select * from i...', Array, Object(Closure))
      #9 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Schema\PostgresBuilder.php(25): Illuminate\Database\Connection->select('select * from i...', Array)
      #10 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Migrations\DatabaseMigrationRepository.php(156): Illuminate\Database\Schema\PostgresBuilder->hasTable('migrations')
      #11 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php(515): Illuminate\Database\Migrations\DatabaseMigrationRepository->repositoryExists()
      #12 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Console\Migrations\MigrateCommand.php(92): Illuminate\Database\Migrations\Migrator->repositoryExists()
      #13 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Console\Migrations\MigrateCommand.php(58): Illuminate\Database\Console\Migrations\MigrateCommand->prepareDatabase()
      #14 [internal function]: Illuminate\Database\Console\Migrations\MigrateCommand->fire()
      #15 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Container\Container.php(508): call_user_func_array(Array, Array)
      #16 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Console\Command.php(169): Illuminate\Container\Container->call(Array)
      #17 C:\wamp64\www\myproject\vendor\symfony\console\Command\Command.php(256): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #18 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Console\Command.php(155): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #19 C:\wamp64\www\myproject\vendor\symfony\console\Application.php(820): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #20 C:\wamp64\www\myproject\vendor\symfony\console\Application.php(187): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Database\Console\Migrations\MigrateCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #21 C:\wamp64\www\myproject\vendor\symfony\console\Application.php(118): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #22 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php(121): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #23 C:\wamp64\www\myproject\artisan(35): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #24 {main}  
      

      同样PostgreSQL(libpq)phpinfo()中的版本是9.4.4,即使我安装了PostgreSQL 9.6也不确定是否可以。

3 个答案:

答案 0 :(得分:0)

你必须要找几件事,

确保配置'默认'键入app/config/database.php

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

检查您是否安装了正确的PHP扩展。您需要安装并启用pdo_pgsql.sopgsql.so。有关如何执行此操作的说明因操作系统而异。

取消注释extension=pdo_pgsql.so

中的行extension=pgsql.sophp.ini

现在将libpq.dllC:\wamp\bin\php\php5.*\复制到C:\wamp\bin\apache*\bin并重启所有服务。

如果问题仍然存在,请查看Environment Variables

让我知道结果。

答案 1 :(得分:0)

config\database.php下,文件名为database.php

此处选择默认为pgsql,如此

'default' => env('DB_CONNECTION', 'pgsql')

此处修改此内容包含您的所有详细信息

'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
            'sslmode' => 'prefer',
        ]

重启您的服务器,希望它连接

答案 2 :(得分:0)

您还必须在本地安装postgres驱动程序。在macOS上,您可以使用brew。

根据您的PHP版本(在终端中尝试php -v),使用以下命令:

brew install php71-pdo-pgsql
brew install php70-pdo-pgsql
brew install php55-pdo-pgsql

此外,如果您正在使用代客,请务必事后valet restart