【问题标题】:connection refused in centOS with postgresql在 centOS 中使用 postgresql 拒绝连接
【发布时间】:2013-03-17 12:56:21
【问题描述】:

我知道很多人问这个问题,但我尝试了很多路径解决方案,但任何一个都是正确的。

所以,我在从远程客户端(在 LAN 中)连接到我的 postgresql 服务器(在 CentOS 6.4 最小版本上 - 我只有 postgresql 服务器、ftp 和 ssh 服务)时遇到问题。我有 ssh 连接,所以我认为这个问题可以通过 tcp/ip 权限来解决。 Java 抛出异常:

    org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:207)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:136)
    at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
    at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21)
    at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:31)
    at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
    at org.postgresql.Driver.makeConnection(Driver.java:393)
    at org.postgresql.Driver.connect(Driver.java:267)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at Main.main(Main.java:19)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at org.postgresql.core.PGStream.<init>(PGStream.java:60)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:101)
    ... 11 more

如果配置:

eth0      Link encap:Ethernet  HWaddr 08:00:27:F0:3A:F4
      inet addr:192.168.1.18  Bcast:192.168.1.255  Mask:255.255.255.0
      inet6 addr: fe80::a00:27ff:fef0:3af4/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:2677 errors:0 dropped:0 overruns:0 frame:0
      TX packets:1437 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000
      RX bytes:251266 (245.3 KiB)  TX bytes:214910 (209.8 KiB)

/var/lib/pgsql/9.2/data/pg_hba.conf - 只是文件的主要部分

 # "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
host    all             all             192.168.1.18 255.255.255.0      password
#hostssl all            all             192.168.1.18 255.255.255.0      trust
#hostnossl      all     all             192.168.1.18 255.255.255.0      trust
# IPv6 local connections:
host    all             all             ::1/128                 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            ident
#host    replication     postgres        ::1/128                 ident

host all all 192.168.1.18 255.255.255.255 password
host all all ::1 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff password

var/lib/pgsql/9.2/data/poistgresql.conf - 仅主要部分

# - Connection Settings -

listen_addresses = '*'
#'localhost,192.168.1.18'
                                        # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart)
#port = 5432                            # (change requires restart)
max_connections = 100                   # (change requires restart)
# Note:  Increasing max_connections costs ~400 bytes of shared memory per
# connection slot, plus lock space (see max_locks_per_transaction).
#superuser_reserved_connections = 3     # (change requires restart)
#unix_socket_directory = ''             # (change requires restart)
#unix_socket_group = ''                 # (change requires restart)
#unix_socket_permissions = 0777         # begin with 0 to use octal notation
                                        # (change requires restart)
#bonjour = off                          # advertise server via Bonjour
                                        # (change requires restart)

最后的调试信息是注释 netstat -ta

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 *:ssh                       *:*                         LISTEN
tcp        0      0 *:postgres                  *:*                         LISTEN
tcp        0      0 localhost:smtp              *:*                         LISTEN
tcp        0     52 PC2.home:ssh                Michal-laptop.home:26146    ESTABLISHED
tcp        0      0 PC2.home:ssh                Michal-laptop.home:25917    ESTABLISHED
tcp        0      0 *:ssh                       *:*                         LISTEN
tcp        0      0 *:postgres                  *:*                         LISTEN
tcp        0      0 localhost:smtp              *:*                         LISTEN

你能帮帮我吗?我应该怎么做才能允许连接? 感谢您的宝贵时间。

【问题讨论】:

  • 从你的客户端机器上,你能做telnet 192.168.1.16 5432吗?如果它说连接失败或连接超时,很可能是您服务器上的防火墙不允许该端口。
  • 我试试,你是对的:telnet:连接到地址 192.168.1.18:连接超时

标签: java database jdbc centos postgresql-9.2


【解决方案1】:

由于您无法远程登录到服务器端口 5432,因此您必须在防火墙中打开端口 5432。

通过控制台认为它类似于System -&gt; Administration -&gt; Firewall/usr/bin/system-config-firewall

【讨论】:

  • 你是对的。我解决了命令 service iptables stop 的问题,但你知道我应该在不停止防火墙的 iptables 文件中添加什么吗?我尝试添加行 -A INPUT -p tcp --dport 5432 -j ACCEPT 但它没有帮助??
  • 我想这最好作为一个单独的问题,以免搅浑水。
【解决方案2】:

在 var/lib/pgsql/9.2/data/poistgresql.conf 中

取消注释以下行:

#port = 5432 

您可以根据需要更改侦听端口。重启服务。

【讨论】:

  • 但 5432 是默认值,所以我认为它没有取消注释,因为我没有更改它。 - 它没有帮助
  • 试一试,如果不起作用,您可以回退:)。据我说,它会工作;)
  • 是的,感谢您的关注,我尝试更改它,但没有帮助
  • 因为centOS是虚拟机所以是competited情况。
猜你喜欢
  • 2017-08-05
  • 2012-06-05
  • 2017-02-17
  • 1970-01-01
  • 2019-08-23
  • 1970-01-01
  • 1970-01-01
  • 2014-12-21
  • 2013-05-30
相关资源
最近更新 更多