2017-08-03 139 views
1

我目前正試圖通過Java程序遠程連接到Postgresql。我有JDBC驅動程序工作,但是當我嘗試連接到數據庫時,連接失敗,並且出現java.net.NoRouteToHostException: No route to host (Host unreachable)錯誤。沒有路由到主機異常 - 試圖遠程連接到Postgresql數據庫

到目前爲止,我已經做了以下嘗試解決的事情:

  1. 在包含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

  2. 我也更新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; 
} 

} 

任何幫助,非常感謝!

+0

我不認爲端口是問題,但您可以隨時使用'port = 5432'在postgresql.conf中更改端口。該條目通常位於「listen_address」條目的正下方。 – r0the

+0

'java.net.NoRouteToHostException'暗示服務器機器實際上無法從客戶端計算機訪問。如果你的客戶端可以訪問服務器,但PostgreSQL沒有監聽,你會得到一個'java.net.ConnectException'(Connection refused)異常。 – r0the

+0

但我可以ping通ip地址,ping工作 – ebbBliss

回答

0

就我個人而言,我覺得有什麼地方出了問題我安裝的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.confpostgresql.conf像我以前那樣,確保我把#出從postgresql.conf現在一切運作良好!

0

引起我注意的是你對postgresql.conf的改變。如果您確實將線路更改爲#listen_addresses = '*',則可能是問題所在。該行起始處的#將該行標記爲註釋,因此它不起作用,並且監聽地址默認爲localhost

所以儘量在listen_address前postgresql.conf中取出#

listen_addresses = '*' 
+0

我刪除了'#'並重新啓動了系統,但它仍然不會遠程連接。 – ebbBliss