HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

ALTER DEFAULT PRIVILEGES - permission denied

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
privilegespermissiondenieddefaultalter

Problem

I have a problem with ALTER DEFAULT PRIVILEGES. I grant the permission:

ALTER DEFAULT PRIVILEGES 
    FOR USER user_name
    IN SCHEMA schema_name
    GRANT SELECT ON TABLES TO user_name;


in the next step, I create a table:

CREATE TABLE schema_name.a (q int);


Now, I connect to the database using conn_user and try to read the data in the new table:

SELECT * FROM schema_name.a


and I receive the error:

SQL Error [42501]: ERROR: permission denied for relation a
  org.postgresql.util.PSQLException: ERROR: permission denied for relation a


Do you have any ideas why the error appears?

Solution

To grant default privileges, you actually need to grant rights to the user you are creating the table with.

e.g.: you're creating the tables as SA_user, but reading the tables as READ_user.
Your code needs to look like this:

ALTER DEFAULT PRIVILEGES 
FOR USER SA_user
IN SCHEMA schema_name
GRANT SELECT ON TABLES TO READ_user;


So whenever the SA_user creates a table, it will grant select rights for the READ_user.

Code Snippets

ALTER DEFAULT PRIVILEGES 
FOR USER SA_user
IN SCHEMA schema_name
GRANT SELECT ON TABLES TO READ_user;

Context

StackExchange Database Administrators Q#186797, answer score: 14

Revisions (0)

No revisions yet.