ALTER_DEFAULT_PRIVILEGES(7pg13)
β¨ ALTER_DEFAULT_PRIVILEGES π
β the ALTER DEFAULT PRIVILEGES manual page (PostgreSQL 13.23, 7pg13 bestie, iykyk)
okay so quick vibe check: this is the page for ALTER_DEFAULT_PRIVILEGES, and it's genuinely one of the most underrated glow-ups in Postgres. it's giving "set it and forget it" but for permissions. let's go π«Ά
β¨ NAME β¨
ALTER_DEFAULT_PRIVILEGES β literally the command that defines your default access privileges. periodt. π
π SYNOPSIS π
ALTER DEFAULT PRIVILEGES
[ FOR { ROLE | USER } target_role [, ...] ]
[ IN SCHEMA schema_name [, ...] ]
abbreviated_grant_or_revoke
where abbreviated_grant_or_revoke is one of these bad boys:
GRANT { { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER }
[, ...] | ALL [ PRIVILEGES ] }
ON TABLES
TO { [ GROUP ] role_name | PUBLIC } [, ...] [ WITH GRANT OPTION ]
GRANT { { USAGE | SELECT | UPDATE }
[, ...] | ALL [ PRIVILEGES ] }
ON SEQUENCES
TO { [ GROUP ] role_name | PUBLIC } [, ...] [ WITH GRANT OPTION ]
GRANT { EXECUTE | ALL [ PRIVILEGES ] }
ON { FUNCTIONS | ROUTINES }
TO { [ GROUP ] role_name | PUBLIC } [, ...] [ WITH GRANT OPTION ]
GRANT { USAGE | ALL [ PRIVILEGES ] }
ON TYPES
TO { [ GROUP ] role_name | PUBLIC } [, ...] [ WITH GRANT OPTION ]
GRANT { { USAGE | CREATE }
[, ...] | ALL [ PRIVILEGES ] }
ON SCHEMAS
TO { [ GROUP ] role_name | PUBLIC } [, ...] [ WITH GRANT OPTION ]
REVOKE [ GRANT OPTION FOR ]
{ { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER }
[, ...] | ALL [ PRIVILEGES ] }
ON TABLES
FROM { [ GROUP ] role_name | PUBLIC } [, ...]
[ CASCADE | RESTRICT ]
REVOKE [ GRANT OPTION FOR ]
{ { USAGE | SELECT | UPDATE }
[, ...] | ALL [ PRIVILEGES ] }
ON SEQUENCES
FROM { [ GROUP ] role_name | PUBLIC } [, ...]
[ CASCADE | RESTRICT ]
REVOKE [ GRANT OPTION FOR ]
{ EXECUTE | ALL [ PRIVILEGES ] }
ON { FUNCTIONS | ROUTINES }
FROM { [ GROUP ] role_name | PUBLIC } [, ...]
[ CASCADE | RESTRICT ]
REVOKE [ GRANT OPTION FOR ]
{ USAGE | ALL [ PRIVILEGES ] }
ON TYPES
FROM { [ GROUP ] role_name | PUBLIC } [, ...]
[ CASCADE | RESTRICT ]
REVOKE [ GRANT OPTION FOR ]
{ { USAGE | CREATE }
[, ...] | ALL [ PRIVILEGES ] }
ON SCHEMAS
FROM { [ GROUP ] role_name | PUBLIC } [, ...]
[ CASCADE | RESTRICT ]
yes bestie, that's a lot of syntax. no, you cannot skip it. it's all load-bearing. π
π¦ DESCRIPTION π¦
so what does ALTER DEFAULT PRIVILEGES actually do? it lets you set the privileges that get applied to objects created in the future. future-you's objects. main character energy for objects that don't even exist yet, honestly. β¨
careful though: it does not affect privileges assigned to already-existing objects. those are set in stone (well, in the catalog) and you gotta go fix them separately.
right now, the only object types whose privileges you can alter here are: schemas, tables (and that includes views and foreign tables), sequences, functions, and types (including domains). π
for this command specifically, "functions" is a wide net β it includes aggregates and procedures too. and the words FUNCTIONS and ROUTINES are equivalent in this command. like, literally the same thing, no cap.
a lil note from the future:ROUTINESis preferred going forward, since it's the standard term for functions and procedures taken together. in earlier PostgreSQL releases, only the wordFUNCTIONSwas allowed. and ngl, it is not possible to set default privileges for functions and procedures separately. they're a package deal. π
you can only change default privileges for objects that will be created by yourself or by roles that you are a member of. that's the whole rule. no setting defaults on behalf of strangers. π
and you can set them two ways:
- globally β i.e. for all objects created in the current database
- per-schema β just for objects created in schemas you name
as explained in Section 5.7, the default privileges for any object type normally grant all grantable permissions to the object owner, and may grant some privileges to PUBLIC as well. but that behavior can be changed by altering the global default privileges with ALTER DEFAULT PRIVILEGES. so yeah, you can rewrite the rules. π
okay here's the part that trips literally everyone, so pay attention bestie:
per-schema default privileges get added to whatever the global default privileges are for that object type. that means you cannot revoke privileges per-schema if they're granted globally β whether that global grant comes from the default, or from a previousALTER DEFAULT PRIVILEGEScommand that didn't specify a schema. per-schemaREVOKEis only useful to reverse the effects of a previous per-schemaGRANT. that's it. that's the whole job. π
π Parameters
target_roleβ the name of an existing role of which the current role is a member. and listen: default access privileges are not inherited, so member roles must useSET ROLEto access these privileges, orALTER DEFAULT PRIVILEGESmust be run for each member role. doing it once for the parent does not cover the children, sorry. ifFOR ROLEis omitted, the current role is assumed. π«Άschema_nameβ the name of an existing schema. if specified, the default privileges are altered for objects later created in that schema. ifIN SCHEMAis omitted, the global default privileges are altered. also,IN SCHEMAis not allowed when setting privileges for schemas, because schemas can't be nested. we don't do schema-ception here. π«role_nameβ the name of an existing role to grant or revoke privileges for. this parameter, and all the other parameters inabbreviated_grant_or_revoke, act as described underGRANT(7)orREVOKE(7)β except that you're setting permissions for a whole class of objects rather than specific named objects. big-picture queen behavior. π
π₯ NOTES π₯
use psql(1)'s \ddp command to get info about existing assignments of default privileges. the meaning of the privilege display is the same as explained for \dp in Section 5.7. so if you already know \dp, you already know. iykyk. β
plot twist warning: if you wish to drop a role for which the default privileges have been altered, it is necessary to reverse the changes in its default privileges or use DROP OWNED BY to get rid of the default privileges entry for the role. you can't just drop it and vibe. the entry will haunt you. π
πΈ EXAMPLES πΈ
grant it π
grant SELECT privilege to everyone for all tables (and views) you subsequently create in schema myschema, and allow role webuser to INSERT into them too:
ALTER DEFAULT PRIVILEGES IN SCHEMA myschema GRANT SELECT ON TABLES TO PUBLIC;
ALTER DEFAULT PRIVILEGES IN SCHEMA myschema GRANT INSERT ON TABLES TO webuser;
undo it π
undo the above, so that subsequently-created tables won't have any more permissions than normal:
ALTER DEFAULT PRIVILEGES IN SCHEMA myschema REVOKE SELECT ON TABLES FROM PUBLIC;
ALTER DEFAULT PRIVILEGES IN SCHEMA myschema REVOKE INSERT ON TABLES FROM webuser;
the fun one π
remove the public EXECUTE permission that is normally granted on functions, for all functions subsequently created by role admin:
ALTER DEFAULT PRIVILEGES FOR ROLE admin REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;
note however that you cannot accomplish that effect with a command limited to a single schema. this command has no effect, unless it is undoing a matching GRANT:
ALTER DEFAULT PRIVILEGES IN SCHEMA public REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;
that's because per-schema default privileges can only add privileges to the global setting, not remove privileges granted by it. it's an adder, not a subtractor. it's giving "yes and", never "actually no". π β¨
π COMPATIBILITY π
there is no ALTER DEFAULT PRIVILEGES statement in the SQL standard. we're built different. π¦
π SEE ALSO π
that's the whole page bestie, go forth and set some defaults responsibly. π
PostgreSQL 13.23 β 2025 β ALTER DEF...RIVILEGES(7) π (yes the title got truncated in the header, no we're not fixing it, it's canon now)
π¦ where this page comes from
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms