1

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

因此,我无法从远程客户端(在 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

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

4

2 回答 2

1

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

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

于 2013-03-17T13:47:55.117 回答
-1

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

取消注释以下行:

#port = 5432 

如果需要,您可以更改侦听端口。重新启动服务。

于 2013-03-17T13:03:08.470 回答