我尝试使用以下代码连接到我的数据库:
Db::$dbh = new PDO("pgsql:dbname=$db;host=$host;port=$port", $user, $pass, $opt);
,它失败并显示:
致命:用户“ postgres”的身份验证失败
当我在命令行上使用相同的凭据时,它可以正常工作:
psql -h127.0.0.1 -Upostgres --password -ddatabase1 -p5432
Password for user postgres:
psql (10.5)
Type "help" for help.
database1=#
即使我明确使用用户名和密码,为什么PDO也使用身份验证?
更新,我发现当使用localhost
作为主机名时,命令行也会失败。显然127.0.0.1
和localhost
不可互换。
答案 0 :(得分:1)
使用localhost
作为主机时,PDO(或Postgres?)可能使用IPv6进行连接。那行在我的pg_hba.conf
中是这样的:
host all all ::1/128 ident
因此,这就是使用ident
的原因。