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
posted @ 2014-02-20 11:44  此人极帅  阅读(15662)  评论(0编辑  收藏  举报