没有路由到主机异常 - 试图远程连接到Postgresql数据库
我目前正试图通过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;
}
}
任何帮助,非常感谢!
就我个人而言,我觉得有什么地方出了问题我安装的Postgres的。尽管pg_hba.conf
正在更新postgres使用端口5432,它实际上并没有使用此端口和端口5433不允许我访问任何东西。
我通过这个过程去除我的Postgres:
yum remove postgres\* mv /var/lib/pgsql /var/lib/old.pgsql
然后我用这些instructions重新安装Postgres的,除了我用下面的yum仓库安装9.5.3,而不是9.4。
http://yum.postgresql.org/9.5/redhat/rhel-7-x86_64/pgdg-centos95-9.5-3.noarch.rpm
请注意,我用的Centos 7 64位系统。
我所做的更改,以pg_hba.conf
和postgresql.conf
像我以前那样,确保我把#
出从postgresql.conf
现在一切运作良好!
引起我注意的是你对postgresql.conf的改变。如果您确实将线路更改为#listen_addresses = '*'
,则可能是问题所在。该行起始处的#
将该行标记为注释,因此它不起作用,并且监听地址默认为localhost
。
所以尽量在listen_address
前postgresql.conf中取出#
:
listen_addresses = '*'
我删除了'#'并重新启动了系统,但它仍然不会远程连接。 – ebbBliss
我不认为端口是问题,但您可以随时使用'port = 5432'在postgresql.conf中更改端口。该条目通常位于“listen_address”条目的正下方。 – r0the
'java.net.NoRouteToHostException'暗示服务器机器实际上无法从客户端计算机访问。如果你的客户端可以访问服务器,但PostgreSQL没有监听,你会得到一个'java.net.ConnectException'(Connection refused)异常。 – r0the
但我可以ping通ip地址,ping工作 – ebbBliss