how to connect to PostgreSQL server to query the database names list
Ok. I have figured it out my self. I can use this string to connect to the server with jdbc driver.
jdbc:postgresql://localhost:5432/?
and can use this code snippet to get the database list
private void listDownAllDatabases() {
try {
PreparedStatement ps = connection
.prepareStatement("SELECT datname FROM pg_database WHERE datistemplate = false;");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
System.out.println(rs.getString(1));
}
rs.close();
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
References: I used this dba stackexchange answer to get all the database list