GRANT(7)
πΈ GRANT(7) β define access privileges πΈ
okay bestie, so this is the GRANT page from the PostgreSQL 13.23 Documentation and it's a big one β but don't panic, we're getting through it together. It's basically the "who gets to touch what" page. sit down, grab a snack, let's go π
β¨ NAME β¨
GRANT β define access privileges. that's it, that's the whole vibe. you're handing out permissions like party invites π
π¦ SYNOPSIS π¦
like, there are a lot of forms, because there are a lot of things you can grant on. here they all are, verbatim, no cap:
GRANT { { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER }
[, ...] | ALL [ PRIVILEGES ] }
ON { [ TABLE ] table_name [, ...]
| ALL TABLES IN SCHEMA schema_name [, ...] }
TO role_specification [, ...] [ WITH GRANT OPTION ]
GRANT { { SELECT | INSERT | UPDATE | REFERENCES } ( column_name [, ...] )
[, ...] | ALL [ PRIVILEGES ] ( column_name [, ...] ) }
ON [ TABLE ] table_name [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]
GRANT { { USAGE | SELECT | UPDATE }
[, ...] | ALL [ PRIVILEGES ] }
ON { SEQUENCE sequence_name [, ...]
| ALL SEQUENCES IN SCHEMA schema_name [, ...] }
TO role_specification [, ...] [ WITH GRANT OPTION ]
GRANT { { CREATE | CONNECT | TEMPORARY | TEMP } [, ...] | ALL [ PRIVILEGES ] }
ON DATABASE database_name [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]
GRANT { USAGE | ALL [ PRIVILEGES ] }
ON DOMAIN domain_name [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]
GRANT { USAGE | ALL [ PRIVILEGES ] }
ON FOREIGN DATA WRAPPER fdw_name [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]
GRANT { USAGE | ALL [ PRIVILEGES ] }
ON FOREIGN SERVER server_name [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]
GRANT { EXECUTE | ALL [ PRIVILEGES ] }
ON { { FUNCTION | PROCEDURE | ROUTINE } routine_name [ ( [ [ argmode ] [ arg_name ] arg_type [, ...] ] ) ] [, ...]
| ALL { FUNCTIONS | PROCEDURES | ROUTINES } IN SCHEMA schema_name [, ...] }
TO role_specification [, ...] [ WITH GRANT OPTION ]
GRANT { USAGE | ALL [ PRIVILEGES ] }
ON LANGUAGE lang_name [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]
GRANT { { SELECT | UPDATE } [, ...] | ALL [ PRIVILEGES ] }
ON LARGE OBJECT loid [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]
GRANT { { CREATE | USAGE } [, ...] | ALL [ PRIVILEGES ] }
ON SCHEMA schema_name [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]
GRANT { CREATE | ALL [ PRIVILEGES ] }
ON TABLESPACE tablespace_name [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]
GRANT { USAGE | ALL [ PRIVILEGES ] }
ON TYPE type_name [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]
GRANT role_name [, ...] TO role_specification [, ...]
[ WITH ADMIN OPTION ]
[ GRANTED BY role_specification ]
and here's what role_specification can actually be, like a little menu π:
[ GROUP ] role_name
| PUBLIC
| CURRENT_USER
| SESSION_USER
π DESCRIPTION π
okay so GRANT has two basic variants, and they're cousins, not twins:
- the one that grants privileges on a database object β that's tables, columns, views, foreign tables, sequences, databases, foreign-data wrappers, foreign servers, functions, procedures, procedural languages, schemas, or tablespaces
- the one that grants membership in a role
they're similar in lots of ways but different enough that the docs describe them separately, and honestly same, so let's do that π«Ά
π GRANT on Database Objects π
this variant gives specific privileges on a database object to one or more roles. and here's the cute part: these privileges get added to whatever was already granted. stacking, not replacing, periodt β¨
the key word PUBLIC means the privileges go out to all roles, including roles that might be created later. think of PUBLIC as an implicitly defined group that always contains every role β like the group chat everyone's automatically in. any particular role ends up with the sum of:
- privileges granted directly to it
- privileges granted to any role it's presently a member of
- privileges granted to
PUBLIC
if WITH GRANT OPTION is specified, the recipient can turn around and grant that privilege to others. without a grant option, they cannot do that, full stop. and no β grant options cannot be granted to PUBLIC. sorry bestie π
also there's genuinely no need to grant privileges to the owner of an object (usually whoever created it), because the owner has all privileges by default. (the owner could still choose to revoke some of their own privileges for safety, which is valid and lowkey responsible of them.)
the right to drop an object, or alter its definition in any way, is not treated as a grantable privilege β it's inherent in the owner and can't be granted or revoked. (however! a similar effect can be obtained by granting or revoking membership in the role that owns the object; see below π) the owner implicitly has all grant options for the object too.
here are the possible privileges, all of them, no skipping π:
SELECTINSERTUPDATEDELETETRUNCATEREFERENCESTRIGGERCREATECONNECTTEMPORARYEXECUTEUSAGE
^ those are the specific types of privileges, as defined in Section 5.7.
TEMPβ alternative spelling forTEMPORARY. same energy, shorter name πALL PRIVILEGESβ grants all of the privileges available for the object's type. thePRIVILEGESkey word is optional in PostgreSQL, though strict SQL requires it. soALLis fine here, but SQL is being strict about it, iykyk.
the FUNCTION syntax works for plain functions, aggregate functions, and window functions β but not procedures; use PROCEDURE for those. alternatively, use ROUTINE to refer to a function, aggregate function, window function, or procedure regardless of its precise type. one word to rule them all, iconic π
there's also an option to grant privileges on all objects of the same type within one or more schemas. heads up though: this functionality is currently supported only for tables, sequences, functions, and procedures. also:
ALL TABLESalso affects views and foreign tables, just like the specific-objectGRANTcommandALL FUNCTIONSalso affects aggregate and window functions, but not procedures β again, just like the specific-objectGRANTcommand- use
ALL ROUTINESto include procedures
π§ GRANT on Roles π§
this variant grants membership in a role to one or more other roles. and membership matters because it conveys the privileges granted to a role to each of its members. so you're not just joining a club, you're inheriting the club's whole wardrobe π
if WITH ADMIN OPTION is specified, the member can in turn grant membership in the role to others, and revoke membership in the role as well. without the admin option, ordinary users cannot do that. a role is not considered to hold WITH ADMIN OPTION on itself, but it may grant or revoke membership in itself from a database session where the session user matches the role. database superusers can grant or revoke membership in any role to anyone. roles having CREATEROLE privilege can grant or revoke membership in any role that is not a superuser.
if GRANTED BY is specified, the grant is recorded as having been done by the specified role. only database superusers may use this option β except when it names the same role executing the command.
unlike the case with privileges, membership in a role cannot be granted to PUBLIC. also note this form of the command does not allow the noise word GROUP in role_specification. not the vibe, apparently π
π₯ NOTES π₯
the REVOKE(7) command is used to revoke access privileges. (bestie's counterpart. the *un*invite.)
since PostgreSQL 8.1, the concepts of users and groups have been unified into a single kind of entity called a role. so it is therefore no longer necessary to use the keyword GROUP to identify whether a grantee is a user or a group. GROUP is still allowed in the command, but it is a noise word. it's giving "kept around for the aesthetic" π
a user may perform SELECT, INSERT, etc. on a column if they hold that privilege for either the specific column or its whole table. and here's the trap: granting the privilege at the table level and then revoking it for one column will not do what one might wish β the table-level grant is unaffected by a column-level operation. so don't do that and expect it to work, because it won't. π
when a non-owner of an object attempts to GRANT privileges on the object, the command will fail outright if the user has no privileges whatsoever on the object. as long as some privilege is available, the command will proceed β but it will grant only those privileges for which the user has grant options. and here's the warning behaviour, which is very specific so listen up:
- the
GRANT ALL PRIVILEGESforms will issue a warning message if no grant options are held - the other forms will issue a warning if grant options for any of the privileges specifically named in the command are not held
(in principle these statements apply to the object owner as well, but since the owner is always treated as holding all grant options, the cases can never occur. so, moot. π )
it should be noted that database superusers can access all objects regardless of object privilege settings. this is comparable to the rights ofrootin a Unix system. and as withroot, it's unwise to operate as a superuser except when absolutely necessary. like, please don't main character your way through prod as a superuser, bestie π
if a superuser chooses to issue a GRANT or REVOKE command, the command is performed as though it were issued by the owner of the affected object. in particular, privileges granted via such a command will appear to have been granted by the object owner. (for role membership, the membership appears to have been granted by the containing role itself.)
GRANT and REVOKE can also be done by a role that is not the owner of the affected object, but is a member of the role that owns the object, or is a member of a role that holds privileges WITH GRANT OPTION on the object. in this case the privileges will be recorded as having been granted by the role that actually owns the object or holds the privileges WITH GRANT OPTION. example time, because this one is a whole puzzle:
if table t1 is owned by role g1, of which role u1 is a member, then u1 can grant privileges on t1 to u2, but those privileges will appear to have been granted directly by g1. any other member of role g1 could revoke them later. chaotic but consistent β¨
if the role executing GRANT holds the required privileges indirectly via more than one role membership path, it is unspecified which containing role will be recorded as having done the grant. in such cases it is best practice to use SET ROLE to become the specific role you want to do the GRANT as. seriously, just do that, it's so much cleaner π«Ά
granting permission on a table does not automatically extend permissions to any sequences used by the table β including sequences tied to SERIAL columns. permissions on sequences must be set separately. yes, separately. every time. no shortcuts π₯
see Section 5.7 for more information about specific privilege types, as well as how to inspect objects' privileges. π
β EXAMPLES β
grant insert privilege to all users on table films:
GRANT INSERT ON films TO PUBLIC;
grant all available privileges to user manuel on view kinds:
GRANT ALL PRIVILEGES ON kinds TO manuel;
note that while the above will indeed grant all privileges if executed by a superuser or the owner of kinds, when executed by someone else it will only grant those permissions for which the someone else has grant options. sneaky little caveat, but we love her π
grant membership in role admins to user joe:
GRANT admins TO joe;
okay that's the first chunk bestie β more coming, don't go anywhere π
β¨ COMPATIBILITY β¨
Okay bestie, compatibility check time β hereβs where PostgreSQL does its own iconic thing compared to the SQL standard π
- According to the SQL standard, the
PRIVILEGESkey word inALL PRIVILEGESis required. Also, the SQL standard does not support setting the privileges on more than one object per command. iykyk π - PostgreSQL allows an object owner to revoke their own ordinary privileges: for example, a table owner can make the table read-only to themselves by revoking their own
INSERT,UPDATE,DELETE, andTRUNCATEprivileges. This is not possible according to the SQL standard. The reason is that PostgreSQL treats the owner's privileges as having been granted by the owner to themselves; therefore they can revoke them too. In the SQL standard, the owner's privileges are granted by an assumed entity β_SYSTEMβ. Not being β_SYSTEMβ, the owner cannot revoke these rights. the more you know π - According to the SQL standard, grant options can be granted to
PUBLIC; PostgreSQL only supports granting grant options to roles. roles only, bestie π - The SQL standard allows the
GRANTED BYoption to be used in all forms ofGRANT. PostgreSQL only supports it when granting role membership, and even then only superusers may use it in nontrivial ways. superuser-only unlock π₯ - The SQL standard provides for a
USAGEprivilege on other kinds of objects: character sets, collations, translations. cute extra objects π¦ - In the SQL standard, sequences only have a
USAGEprivilege, which controls the use of theNEXT VALUE FORexpression, which is equivalent to the functionnextvalin PostgreSQL. The sequence privilegesSELECTandUPDATEare PostgreSQL extensions. The application of the sequenceUSAGEprivilege to thecurrvalfunction is also a PostgreSQL extension (as is the function itself). sequence lore unlocked β - Privileges on databases, tablespaces, schemas, and languages are PostgreSQL extensions. PostgreSQL said βand I took that personallyβ π
π SEE ALSO π
Go peek at REVOKE(7) and ALTER DEFAULT PRIVILEGES (ALTER_DEFAULT_PRIVILEGES(7)) β the manual says these are the related reads, and honestly theyβre giving permissions saga continuation π«Ά
Thatβs the whole GRANT(7) moment, bestie β source/version PostgreSQL 13.23, dated 2025. Now go grant (or revoke) like the permissions icon you are π
πβ¨
π¦ 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