在命令行上连接到postgres可以工作,但不能与PDO连接

时间:2017-11-17 14:06:55

标签: postgresql pdo

我在命令行上登录Postgres服务器:

psql -h127.0.0.1 -Upostgres --password -dDATABASE

但是,当我尝试通过PDO使用它时,它不起作用:

new PDO("pgsql:dbname=DATABASE;host=127.0.0.1;port=5432", "postgres", "PASSWORD");

我收到了这条消息:

  

SQLSTATE [08006] [7]无法连接到服务器:权限被拒绝\ n \ t服务器在主机“127.0.0.1”上运行并接受端口5432上的\ n \ tTCP / IP连接?

2 个答案:

答案 0 :(得分:1)

问题是SE Linux。您必须明确允许Apache访问数据库:

setsebool -P httpd_can_network_connect_db 1

另见https://serverfault.com/questions/240015/how-do-i-allow-mysql-connections-through-selinux

答案 1 :(得分:0)

PDO构造函数中的用户名和密码参数是一个稍微奇怪的设计,主要用于MySQL。对于Postgres和大多数其他驱动程序,您应该include those parameters in the DSN, as shown in the examples in the manual

new PDO("pgsql:dbname=DATABASE;host=127.0.0.1;port=5432;user=postgres;password=PASSWORD");
相关问题