Schema-level access control helps you decide who can see or change groups of database objects. It keeps your data safe and organized.
Schema-level access control in PostgreSQL
GRANT { { USAGE | CREATE } [, ...] | ALL [ PRIVILEGES ] } ON SCHEMA schema_name TO role_name;
REVOKE { { USAGE | CREATE } [, ...] | ALL [ PRIVILEGES ] } ON SCHEMA schema_name FROM role_name;USAGE lets a user access objects in the schema (like tables).
CREATE lets a user add new objects (like tables) to the schema.
GRANT USAGE ON SCHEMA sales TO alice;GRANT CREATE ON SCHEMA marketing TO bob;
REVOKE USAGE ON SCHEMA sales FROM alice;
GRANT ALL PRIVILEGES ON SCHEMA hr TO hr_team;This example creates a schema named 'project' and a role named 'developer'. It then gives the developer role permission to use and create objects in the 'project' schema. Finally, it shows the granted privileges.
CREATE SCHEMA project; CREATE ROLE developer; GRANT USAGE ON SCHEMA project TO developer; GRANT CREATE ON SCHEMA project TO developer; -- Check privileges SELECT grantee, privilege_type FROM information_schema.schema_privileges WHERE schema_name = 'project' AND grantee = 'developer';
Granting USAGE alone does not allow creating or modifying objects, only accessing them.
CREATE privilege is needed to add new tables or other objects inside the schema.
Always check current privileges with information_schema.schema_privileges to confirm access rights.
Schema-level access control manages who can use or add objects in a schema.
Use GRANT and REVOKE with USAGE and CREATE privileges to control access.
Check privileges anytime to keep your database secure and organized.