Getting usergroup in PostgreSQL
I'm building with a classmate a console application (written in Java) which connects to a PostgreSQL database.
Eventually we wanted to know in which user group was the user trying to connect to the database. Looking for some information about this, we found there's a table called pg_group where for each group we have a field with users' id (grolist). Then the problem was to know where a concrete id was present. We thought grolist was a plain string, so we had to code a StringTokenizer to get the id's, and then test if it was the one wanted. That resulted in an ugly, unefficient code which I wanted to rewrite.
Fortunately, we misunderstood the type of data stored in the field grolist. It's an array of id's instead a string. So, and after visiting this page :P, I finally could solve our problem like follows:
private static void setGroup() throws SQLException {ResultSet rs2;
}
String myGroup,query;
query = "SELECT groname FROM pg_group WHERE "+idUser+" = ANY(grolist);";
rs2 = DBConnection.execQuery(query);
rs2.next();
myGroup = rs2.getString(1);
...