gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 23:45:33 25 4
gpt4 key购买 nike

我很难用 pgsql 设置 laravel 5.3(在 wamp/windows 上运行)。当我尝试使用“php artisan migrate”迁移表时出现错误“PDO Exception - Could not find driver”

  1. 我安装了 postgres 9.6。
  2. 我为 postgresql 启用了 php 扩展:

enter image description here3. 我将 postgres 库添加到环境路径变量

enter image description here

  1. 我用 php 测试了 pdo postgres 连接,我正在连接:


$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';
}

So I am stuck, cant get the migration to work in laravel. My .env:

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

Here is the laravel error stack:

    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}

此外,phpinfo() 中的 PostgreSQL (libpq) 版本是 9.4.4,尽管我安装了 PostgreSQL 9.6 不确定是否可以..

最佳答案

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

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

关于php - 通过 laravel 连接 Postgresql 数据库的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40449805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com