我目前正试图通过Java程序远程连接到Postgresql。我有JDBC驱动程序工作,但是当我尝试连接到数据库时,连接失败,并且出现java.net.NoRouteToHostException: No route to host (Host unreachable)
错误。没有路由到主机异常 - 试图远程连接到Postgresql数据库
到目前为止,我已经做了以下尝试解决的事情:
在包含PostgreSQL数据库的服务器上,我做了改变的pg_hba.conf包括设备试图连接的IP地址:
# IPv4 local connections: host all all 127.0.0.1/32 trust host all all 192.168.25.170/32 trust
我也更新postgresql.conf中,这样
#listen_addresses = 'localhost'
改为#listen_addresses = '*'
埃德它改为listen_addresses = '*'
根据建议
我已经重新启动计算机多次,但仍然得到错误。有一两件事我注意到的是,当我执行sudo netstat -nlp | grep postgres
我得到:
`tcp 0 0 0.0.0.0:5433 0.0.0.0:* LISTEN 1309/postgres
tcp6 0 0 :::5433 :::* LISTEN 1309/postgres
unix 2 [ ACC ] STREAM LISTENING 20873 1309/postgres /tmp/.s.PGSQL.5433`
这混淆了我,因为我知道Postgres的默认端口应该是5432,我很好奇,如果因为这不是默认端口,我有问题?
编辑 请注意,我已经在本地运行代码以访问数据库,并在本地运行完美。当我编辑要进行远程访问的代码时,发生错误。这里是java代码:
public class HdfsTransferToDatabase {
public static void main(String[] args) throws SQLException {
//SQLData sql = new SQLData();
System.out.println("---------- PostgreSQL JDBC Connection Testing ----------");
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your PostgreSQL JDBC Driver? "
+ "Include it in your library path.");
e.printStackTrace();
return;
}
System.out.println("---------- PostgreSQL JDBC Driver Registered ----------");
Connection connection = connect();
if(connection != null) {
System.out.println("---------- Connection Successful for PostgreSQL ----------");
//sql.viewTable(connection);
} else {
System.out.println("---------- Connection Failed for PostgreSQL ----------");
}
}
private static Connection connect() {
//Connection string
String url = "jdbc:postgresql://192.168.25.179:5433/gisdb";
Connection conn = null;
try {
conn = DriverManager.getConnection(
url, "testuser", "testpassword");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console.");
e.printStackTrace();
}
return conn;
}
}
任何帮助,非常感谢!
我不认为端口是问题,但您可以随时使用'port = 5432'在postgresql.conf中更改端口。该条目通常位于“listen_address”条目的正下方。 – r0the
'java.net.NoRouteToHostException'暗示服务器机器实际上无法从客户端计算机访问。如果你的客户端可以访问服务器,但PostgreSQL没有监听,你会得到一个'java.net.ConnectException'(Connection refused)异常。 – r0the
但我可以ping通ip地址,ping工作 – ebbBliss