2014-05-07 63 views
2

我有一個運行在VirtualBox上的CentOS 6.5虛擬機。 對此,我設置了Apache,PHP,PHP Postgres擴展(php-pgsql)和Postgres數據庫。 我修改pg_hba.conf文件以允許連接:CentOS 6.5上的PHP無法連接到Postgres DB

# "local" is for Unix domain socket connections only 
local all    all          peer 
# IPv4 local connections: 
host all    all    127.0.0.1/32   ident 
# IPv6 local connections: 
host all    all    ::1/128     ident 

host all all 192.168.0.0/24 trust 
host all all 0.0.0.0/0 md5 

這是我的iptables文件:

Chain INPUT (policy ACCEPT) 
target  prot opt source    destination   
ACCEPT  tcp -- anywhere    anywhere   state NEW tcp dpt:8090 
ACCEPT  tcp -- anywhere    anywhere   state NEW tcp dpt:postgres 

Chain FORWARD (policy ACCEPT) 
target  prot opt source    destination   

Chain OUTPUT (policy ACCEPT) 
target  prot opt source    destination 

現在,當我從我的主機連接(IP:192.168.0.117)到Postgres的與客戶端pgAdmin3(用戶:Postgres的,密碼:Postgres的),我能做到這一點,一切工作正常,但是PHP在我的CentOS的VM上運行無法連接到數據庫:

$connection_string = "host=localhost port=5432 dbname=rt_prezzario user=postgres password=postgres"; 
$resource = pg_connect($connection_string); 

這是當我運行這兩條代碼行時觸發的警告:

Warning: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Permission denied Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? 

任何人都有建議嗎?

+0

嘗試使用'127.0.0.1' IP地址而不是'localhost'。你的'/ etc/hosts'文件是什麼樣的?您可能需要刪除'localhost'條目的IPv6地址(IIRC我過去不得不爲Postfix做類似的事情) – Phil

回答

5

SELinux可能會阻止連接。當連接被阻止時,您應該能夠在/ var/log/messages中找到日誌條目。

嘗試SELinux中設置此標誌:

setsebool -P httpd_can_network_connect_db on 

紅帽有一些文檔,對這個標誌的SELinux:1