public static List < String > getDatabases ( String host , String port , String existingDb , String user , String pwd ) throws Exception { if ( existingDb == null ) { existingDb = "postgres" ; } String url = EDb . POSTGRES . getJdbcPrefix ( ) + host + ":" + port + "/" + existingDb ; try ( Connection connection = DriverManager . getConnection ( url , user , pwd ) ) { String sql = "SELECT datname FROM pg_database WHERE datistemplate = false;" ; List < String > dbs = new ArrayList <> ( ) ; try ( Statement stmt = connection . createStatement ( ) ; ResultSet rs = stmt . executeQuery ( sql ) ) { while ( rs . next ( ) ) { String dbName = rs . getString ( 1 ) ; dbs . add ( dbName ) ; } return dbs ; } } }
Get the list of databases .