Environment
- YugabyteDB Anywhere - All Versions
Issue
User execute ALTER DEFAULT PRIVILEGES IN SCHEMA and grant select ON ALL TABLES IN SCHEMA with super admin user, but still newly create tables in the same SCHEMA doesn't have those permission GRANT-ed automatically.
Resolution
Overview
This is one of the most common points of confusion in PostgreSQL administration.
ALTER DEFAULT PRIVILEGES does exactly what its name implies: it defines a set of permissions that are automatically applied to objects created in the future. However, it is not a blanket setting for the entire database.
The Three Rules of Default Privileges
To make sure this actually works for your use case, keep these three rules in mind:
It does not affect existing objects: This command only applies to tables, sequences, or functions created after the command is executed. If you have existing tables, you must grant permissions for those manually using
GRANT.It is tied to the user who ran it: This is the most common
gotcha. TheALTER DEFAULT PRIVILEGEScommand applies to objects created by the user who executed the command. If User A runs the command, but User B creates the table, User B's tables will not receive those default privileges.Pro-tip: If you want these privileges to apply regardless of who creates the object, you often need to execute the command as the owner of the schema or use a specific "admin" role that creates the objects.
It is schema-specific: As the syntax suggests, it only applies to objects created within the schema(s) you specify.
How to use it
If you want to ensure that any new tables created in the public schema are readable by a role named readonly_user, you would run:
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT ON TABLES TO readonly_user;Another possible workaround is to use the FOR clause as the superadmin if they want u2 to have access on objects created by u1.
ALTER DEFAULT PRIVILEGES FOR USER u1 IN SCHEMA public GRANT SELECT ON TABLES TO u2;
Why it feels counter-intuitive
Many users expect that running this command as a superuser would apply to everyone, but PostgreSQL design intentionally separates who defines the privilege from who creates the object. It’s a security feature to ensure that one user cannot arbitrarily hijack the security configuration of tables created by another user.
Comments
0 comments
Please sign in to leave a comment.