14

I'm building a project using postgresql and java (NetBeans).

When I try to execute a simple query (select * from customer) I get an error, telling me that "permission denied for relation customer".

When I searched for an answer, all I found was about granting user privileges, so I went into pgAdmin and wrote "grant all privileges on database dirty to henrik;" .

That didn't change anything - and that's why I'm making a new post..

The complete error message output of the program:

run:
1
2
3

Mar 23, 2014 7:47:18 PM sletdendog.SletDenDog main
SEVERE: ERROR: permission denied for relation customer
org.postgresql.util.PSQLException: ERROR: permission denied for relation customer
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2157)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1886)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:555)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:302)
at sletdendog.SletDenDog.main(SletDenDog.java:27)

BUILD SUCCESSFUL (total time: 0 seconds)

The java program is really simple - it looks like this:

package sletdendog;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SletDenDog {

private static Connection connection = null;
private static PreparedStatement statement = null;
private static ResultSet result = null;
private static ResultSetMetaData meta = null;

public static void main(String[] args) {
    
    try{
        System.out.println("1");
        connection = DriverManager.getConnection("jdbc:postgresql://localhost/dirty", "henrik", "hokus pokus");
        System.out.println("2");
        statement = connection.prepareStatement("select * from customer;");
        System.out.println("3");
        result = statement.executeQuery();
        System.out.println("4");
        meta = result.getMetaData();
        System.out.println("5");
        for (int i = 1; i <= meta.getColumnCount(); i++) {
            System.out.println(meta.getColumnName(i));
        }
                
    } catch (SQLException ex){
        Logger lgr = Logger.getLogger(SletDenDog.class.getName());
        lgr.log(Level.SEVERE, ex.getMessage(), ex);
    } finally {
        try{
            if(connection != null){
                connection.close();
            }
            if(statement != null){
                statement.close();
            }
            if(result != null){
                result.close();
            }
        } catch(SQLException ex){
     
            System.out.println("Something wasn't closed...");
        
        }
    }   
}   
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Henry
  • 333
  • 1
  • 3
  • 10

1 Answers1

11

Privileges on the database are not enough.
You also need at least the USAGE privilege for relevant schema and the SELECT privilege for the table.

More details in this closely related answer:
How to grant all privileges on views to arbitrary user

Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
  • That solved it. Kind of misleading that the "grant all privileges" command doesn't grant all privileges though :/ Guess I have to read a whole lot more about users and privileges when I'm done with this project. But thanks a bunch! – Henry Mar 23 '14 at 20:37
  • @Henry: Well, it *does* [grant all privileges for database access: (`CREATE`, `CONNECT`, `TEMP`)](http://www.postgresql.org/docs/current/interactive/sql-grant.html). – Erwin Brandstetter Mar 23 '14 at 22:16