Laravel:PHP Artisan Migrate 抛出 PDO 异常:找不到驱动程序 (Postgres)

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

Laravel: PHP Artisan Migrate throws PDO Exception: Could not find driver (Postgres)

phppostgresqllaravelnginxpdo

提问by user968270

OS:Ubuntu 16.10

操作系统:Ubuntu 16.10

Server:Nginx

服务器:Nginx

DB:PostgreSQL 9.5.4

数据库:PostgreSQL 9.5.4

Laravel 5.4

Laravel 5.4

Trying to perform my database migrations, but php artisan migratethrows the following error:

尝试执行我的数据库迁移,但php artisan migrate引发以下错误:

[Illuminate\Database\QueryException] could not find driver: (SQL select * from information_schema.tables where tables_schema = username and table_name = migrations)

[Illuminate\Database\QueryException] 找不到驱动程序:(SQL select * from information_schema.tables where tables_schema = username and table_name = migrations)

Followed by a second message stating

接着是第二条消息,说明

[PDOException]

could not find driver

[PDO 异常]

找不到驱动程序

I have done the following:

我做了以下工作:

  1. Verified that I have php-pgsql's latest version installed
  2. Uncommented the line pertaining to php_pdo_pgsql.dll in /etc/php/7.0/fpm/php.ini
  3. Restarted both posgtresql and nginx multiple times
  1. 验证我安装了 php-pgsql 的最新版本
  2. 取消注释 /etc/php/7.0/fpm/php.ini 中与 php_pdo_pgsql.dll 相关的行
  3. 多次重启 posgtresql 和 nginx

Currently, when I run php -i, the output I get for the PDO section is

目前,当我运行时php -i,PDO 部分的输出是

PDO support => enabled

PDO drivers => pgsql

PDO 支持 => 启用

PDO 驱动程序 => pgsql

The pdo_pgsql section yields

pdo_pgsql 部分产生

PostgreSQL(libpq) Version =>9.5.4

Module version =>7.0.15-0ubuntu0.16.10.4

PostgreSQL(libpq) 版本 =>9.5.4

模块版本 =>7.0.15-0ubuntu0.16.10.4

And the pgsql section returns

并且 pgsql 部分返回

PostgreSQL(libpq) Version = 9.5.4

PostgreSQL(libpq) => PostgreSQL 9.5.4 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 6.1.1-11ubuntu12) 6.1.1 20160805, 64-bit Multibyte character support => enabled

SSL support => enabled

Active Persistent Links => 0

Active Links => 0

PostgreSQL(libpq) 版本 = 9.5.4

PostgreSQL(libpq) => x86_64-pc-linux-gnu 上的 PostgreSQL 9.5.4,由 gcc (Ubuntu 6.1.1-11ubuntu12) 6.1.1 20160805 编译,64 位多字节字符支持 => 启用

SSL 支持 => 已启用

活动持久链接 => 0

活动链接 => 0

But in spite of this, still getting the same error. I thought there was some chance it might be a permissions problem, but running the command as either root or www-data has the same result.

但尽管如此,仍然得到同样的错误。我认为有可能是权限问题,但是以 root 或 www-data 运行命令的结果相同。

回答by Yaser Darzi

you should install PHP pdo

你应该安装 PHP pdo

sudo apt upgrade 
sudo apt install php-pgsql

回答by lewis4u

You need to define your .env file

你需要定义你的 .env 文件

in your.env file you need to specify which database are you using (MySQL, SQLite, Postgres) and specify your DB username and password

在 your.env 文件中,您需要指定您使用的是哪个数据库(MySQL、SQLite、Postgres)并指定您的数据库用户名和密码

APP_ENV=local
APP_KEY=base64:gqaCpjbEfukDr5As7EnrqRzi5PSho1rOS/q2H0E+HOs=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=YOUR_DB_NAME
DB_USERNAME=YOUR_USERNAME
DB_PASSWORD=YOUR_PASSWORD

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=

And it is always good to run these commands when working with migrations

在处理迁移时运行这些命令总是好的

composer dump-autoload
php artisan cache:clear
php artisan view:clear

回答by Ahmed Awad

i'v been through the same situation, try the following in order,

我遇到过同样的情况,请按顺序尝试以下操作,

1) install the postgre extensions for php, those are:
   php-(YOUR_VERSION)-pgsql, and php-pgsql.
2) enable pgsql extensions in your php.ini file, basically, 
   uncomment the the extensions. 
3) restart the apache server(or nginx).
4) check that extensions are enable using phpinfo() or any else tool.
5) set a password for (in instance) the postgres user in postgreSQL 
   server, this is a very important step.

login to the psql shell:

登录到 psql shell:

sudo su - postgres
\password postgres

enter a password and confirm it.

输入密码并确认。

now, set your .env file and try again.

现在,设置您的 .env 文件并重试。

回答by user968270

SOLVED:

解决了:

Make sure in this scenario that you have run

确保在这种情况下你已经运行

php artisan cache:clear php artisan config:cache

php artisan cache:clear php artisan config:cache

I thought I'd done this at every pivotal step, but running both of these commands again allowed me to conduct the migration and get the site running properly. Thank you to lewis4u for the assistance, however.

我以为我在每个关键步骤都这样做了,但是再次运行这两个命令使我能够进行迁移并使站点正常运行。不过,感谢 lewis4u 的帮助。