无法通过 php pdo 驱动程序连接到 postgreSQL 数据库

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

Can't connect to postgreSQL database through php pdo driver

phppostgresqlpdo

提问by Parris Varney

I've decided to switch from MySQL to PostgreSQL recently, mostly just to learn a new DB. It's been pretty painful, but I think I'm close.

我最近决定从 MySQL 切换到 PostgreSQL,主要是为了学习一个新的 DB。这很痛苦,但我想我已经接近了。

I'm using php and PDO, my PDO driver has been successfully install and configured.

我正在使用 php 和 PDO,我的 PDO 驱动程序已成功安装和配置。

Opening my site, I get the error:

打开我的网站,我收到错误:

Connection failed: SQLSTATE[08006] [7] FATAL: Ident authentication failed for user "postgres"

I'm using the following connection calls (I've tried a few variations of calling user/pw in the $dsn variable, and in separate $user/$pass variables, and including port=5432):

我正在使用以下连接调用(我尝试了在 $dsn 变量和单独的 $user/$pass 变量中调用 user/pw 的一些变体,包括 port=5432):

$dsn  = 'pgsql:dbname=db1;host=localhost;user=postgres;password=pass';
$db = new PDO($dsn);

Also, I'm able to log into my db from the command line:

另外,我可以从命令行登录到我的数据库:

$ su postgres

(pass)

(经过)

$ psql db1

output:

输出:

could not change directory to "/home/ec2-user" psql (8.4.9) Type "help" for help.

db1=#

无法将目录更改为“/home/ec2-user” psql (8.4.9) 输入“help”以获得帮助。

db1=#

Any ideas? I'd love to provide more info if needed.

有任何想法吗?如果需要,我很乐意提供更多信息。

回答by maraspin

Is your pg_hba.conf file all right? It's a known source of pain at the beginning w/ PostgreSQL. And, the situation you mention, is a good candidate for such kind of a problem. :-)

你的 pg_hba.conf 文件还好吗?在使用 PostgreSQL 时,这是一个众所周知的痛苦来源。而且,你提到的情况是此类问题的一个很好的候选者。:-)