PostgreSQL安裝后Navicat客戶端連接報錯:Could not connect to server:Connection refused(0x00002740/10061


PostgreSQL安裝后Navicat客戶端連接報錯:Could not connect to server:Connection refused(0x00002740/10061)

     經常使用PostgreSQL數據庫的用戶,在用Navicat客戶端連接服務器端時,報錯:

     Could not connect to serverConnection refused(0x00002740/10061) 

         Is the server running on host “xxx.xxx.xx.xx” and accepting 

         TCP/IP connetions on port 5432?

此問題產生的原因有:

1.hostxxx.xxx.xxx.xxx”不存在,此時客戶端pingIP應不是不通的

2.Hostxxx.xxx.xxx.xxx”存在,客戶端pingIp地址能ping通,但是此主機並未安裝PostgreSQL數據庫

以上兩個原因好理解,下面還有幾個原因,后台查看ps -ef|grep postmaster如下:

1
2
3
[root@CM-126 pgsql_9.2.2] # ps -ef|grep postmaster   
root       793 32595  0 14:30 pts /0     00:00:00  grep  postmaster   
postgres 32762     1  0 13:50 ?        00:00:00  /usr/local/pgsql/bin/postmaster  -D  /usr/local/pgsql/data

3.hostxxx.xxx.xxx.xxx”存在,客戶端pingIp地址能ping通,此主機上也安裝了PostgreSQL數據庫,監聽端口是5432,但客戶端填寫時而不是5432,如果5432也不正確,就確認下數據庫監聽的端口,后台用“netstat -anp|grep postmaster”命令查看下

1
2
3
4
5
[root@localhost pgsql]# netstat -anp|grep postmaster   
tcp         0       0  0.0 . 0.0 : 5432                 0.0 . 0.0 :*                   LISTEN       14430 /postmaster  
tcp         0       0  ::: 5432                      :::*                        LISTEN       14430 /postmaster  
udp         0       0  127.0 . 0.1 : 1316               127.0 . 0.1 : 1316               ESTABLISHED  14430 /postmaster  
unix   2       [ ACC ]     STREAM     LISTENING      14603833  14430 /postmaster    /tmp/.s.PGSQL. 5432

4.hostxxx.xxx.xxx.xxx”存在,能被客戶端ping通,PostgreSQL數據庫在主機上已安裝,但$PGDATA/data/目錄下的配置文件postgresql.conf中關於監聽地址配置為:

1
listen_addresses =  'localhost'

或者

1
#listen_addresses = '*'

如果是此種情況,有兩種方法可以解決此問題。

第一種:修改配置文件postgresql.conf

此時只需將監聽地址改為:

1
listen_addresses =  '*'

然后,重啟(/etc/rc.d/init.d/postgresql restart)PostgreSQL即可

第二種:配置文件postgresql.conf不做修改

保持postgresql.conf不變,轉而修改PostgreSQL的啟動腳本postgresql:

原來啟動時命令行為

1
su  - $PGUSER -c  "$DAEMON -D '$PGDATA' &"  >>$PGLOG 2>&1

現改為

1
su  - $PGUSER -c  "$DAEMON -i -D '$PGDATA' &"  >>$PGLOG 2>&1

啟動后一般為

1
/usr/local/pgsql/bin/postmaster  -i -D  /usr/local/pgsql/data

啟動時加上-i選項,意思就是允許TCP/IP連接

1
2
[root@localhost pgsql] # /usr/local/pgsql/bin/postmaster --help|grep TCP/IP 
     -i                  enable  TCP /IP  connections

5、防火牆開啟也會造成此種現象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@cloudera135 ~] # iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt  source                 destination        
ACCEPT     udp  --  anywhere             anywhere            udp dpt:domain
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:domain
ACCEPT     udp  --  anywhere             anywhere            udp dpt:bootps
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:bootps
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere           
ACCEPT     all  --  anywhere             anywhere           
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt: ssh
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited
 
Chain FORWARD (policy ACCEPT)
target     prot opt  source                 destination        
ACCEPT     all  --  anywhere             192.168.122.0 /24      state RELATED,ESTABLISHED
ACCEPT     all  --  192.168.122.0 /24       anywhere           
ACCEPT     all  --  anywhere             anywhere           
REJECT     all  --  anywhere             anywhere            reject-with icmp-port-unreachable
REJECT     all  --  anywhere             anywhere            reject-with icmp-port-unreachable
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited
 
Chain OUTPUT (policy ACCEPT)
target     prot opt  source                 destination        

此種情況若解決,只需關閉防火牆即可

1
2
3
4
[root@cloudera135 ~] # service iptables stop
iptables:清除防火牆規則:                                 [確定]
iptables:將鏈設置為政策 ACCEPT:nat mangle filter         [確定]
iptables:正在卸載模塊:                                   [確定]

若不想關閉防火牆,也可以改變防火牆規則以解決此問題。

注意:34兩種情況下,$PGDATA/pg_hba.conf文件中的配置為

1
2
# TYPE   DATABASE   USER       ADDRESS     METHOD    
  host      all     all       0.0.0.0 /0      trust


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM