code: protected void closeAllConnections(String databasename)
    {
      String sql
        = "SELECT pg_terminate_backend(pg_stat_activity.pid)\n"
        + "FROM pg_stat_activity\n"
        + "WHERE pg_stat_activity.datname = ?\n"
        + "  AND pid <> pg_backend_pid();";
      try(Connection conn = getDataSource().getConnection())
      {
        DatabaseMetaData meta = conn.getMetaData();
        
        if(meta.getDatabaseMajorVersion() == 9 
          && meta.getDatabaseMinorVersion() <= 1)
        {
          sql
            = "SELECT pg_terminate_backend(pg_stat_activity.procpid)\n"
            + "FROM pg_stat_activity\n"
            + "WHERE pg_stat_activity.datname = ?\n"
            + "  AND procpid <> pg_backend_pid();";
        }
      }
      catch(SQLException ex)
      {
        log.warn("Could not get the PostgreSQL version", ex);
      }
      
      getJdbcTemplate().queryForRowSet(sql, databasename);
  
    }
docs: Closes all open idle connections. The current data source
  must have superuser rights.
  
  This can be used if a another database action needs full access to a database,
  e.g. when deleting and then creating it
  @param databasename