连线被拒,请检查主机名称和埠号,并确定 postmaster 可以接受 TCP/IP 连线。

错误如下:

org.postgresql.util.PSQLException: 连线被拒,请检查主机名称和埠号,并确定 postmaster 可以接受 TCP/IP 连线。
	at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:136)
	at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
	at org.postgresql.jdbc2.AbstractJdbc2Connection.(AbstractJdbc2Connection.java:123)
	at org.postgresql.jdbc3.AbstractJdbc3Connection.(AbstractJdbc3Connection.java:28)
	at org.postgresql.jdbc3g.AbstractJdbc3gConnection.(AbstractJdbc3gConnection.java:20)
	at org.postgresql.jdbc3g.Jdbc3gConnection.(Jdbc3gConnection.java:22)
	at org.postgresql.Driver.makeConnection(Driver.java:391)
	at org.postgresql.Driver.connect(Driver.java:265)
	at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:68)
	at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:87)
	at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1.acquireResource(C3P0PooledConnectionPool.java:83)
	at com.mchange.v2.resourcepool.BasicResourcePool.assimilateResource(BasicResourcePool.java:884)
	at com.mchange.v2.resourcepool.BasicResourcePool.acquireUntil(BasicResourcePool.java:601)
	at com.mchange.v2.resourcepool.BasicResourcePool.access$400(BasicResourcePool.java:31)
	at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1079)
	at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:354)
Caused by: java.net.ConnectException: Connection timed out: connect
	at java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.net.PlainSocketImpl.doConnect(Unknown Source)
	at java.net.PlainSocketImpl.connectToAddress(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 java.net.Socket.(Unknown Source)
	at java.net.Socket.(Unknown Source)
	at org.postgresql.core.PGStream.(PGStream.java:60)
	at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:74)
	... 15 more


解决方法:

一、postgresql 连线被拒,请检查主机名称和埠号,并确定 postmaster 可以接受 TCP/IP 连线
用jdbc(或者eclipse自带的数据库连接工具)连接Postgresql数据库时经常出现这个错误,然而用pgAdmin III是可以正确连接的,表明用户名和密码都是正确的。
这主要是由于用户密码认证方式引起的,Postgresql数据库安装好后默认采用md5密码加密认证方式。

二、或则是连接其他机器的数据库,也会发生这个问题

解决方法:
!、打开Postgresql安装目录下的data文件夹,找到pg_hba.conf文件并打开
在# TYPE DATABASE    USER        CIDR-ADDRESS          METHOD的下面:
加上一句:
host all all    0.0.0.0/0    md5

2、更改postgresql.conf下
#listen_addresses = 'localhost' # what IP address(es) to listen on;

listen_addresses = '*' # what IP address(es) to listen on;
记得去掉listen_addresses前的#号

你可能感兴趣的:(JAVA,SQL)