GRANT(7)
β¨ GRANT(7) β¨
okay bestie, buckle up β we're doing the PostgreSQL 14.24 Documentation page for GRANT, and it's literally the "who gets to touch my stuff" command π
. This is part 1 of 2, so don't you dare click away, there's more coming π«Ά
π NAME π
GRANT β define access privileges
like, officially: who's allowed to do what around here π
πΈ SYNOPSIS πΈ
ok so the grammar looks like a lot, and it IS a lot, but it's the same shape over and over: GRANT some privilege ON some thing TO somebody. Here's all of it, exactly as written, because I refuse to paraphrase syntax π§
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 ]
[ GRANTED BY role_specification ]
GRANT { { SELECT | INSERT | UPDATE | REFERENCES } ( column_name [, ...] )
[, ...] | ALL [ PRIVILEGES ] ( column_name [, ...] ) }
ON [ TABLE ] table_name [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]
[ GRANTED BY role_specification ]
GRANT { { USAGE | SELECT | UPDATE }
[, ...] | ALL [ PRIVILEGES ] }
ON { SEQUENCE sequence_name [, ...]
| ALL SEQUENCES IN SCHEMA schema_name [, ...] }
TO role_specification [, ...] [ WITH GRANT OPTION ]
[ GRANTED BY role_specification ]
GRANT { { CREATE | CONNECT | TEMPORARY | TEMP } [, ...] | ALL [ PRIVILEGES ] }
ON DATABASE database_name [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]
[ GRANTED BY role_specification ]
GRANT { USAGE | ALL [ PRIVILEGES ] }
ON DOMAIN domain_name [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]
[ GRANTED BY role_specification ]
GRANT { USAGE | ALL [ PRIVILEGES ] }
ON FOREIGN DATA WRAPPER fdw_name [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]
[ GRANTED BY role_specification ]
GRANT { USAGE | ALL [ PRIVILEGES ] }
ON FOREIGN SERVER server_name [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]
[ GRANTED BY role_specification ]
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 ]
[ GRANTED BY role_specification ]
GRANT { USAGE | ALL [ PRIVILEGES ] }
ON LANGUAGE lang_name [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]
[ GRANTED BY role_specification ]
GRANT { { SELECT | UPDATE } [, ...] | ALL [ PRIVILEGES ] }
ON LARGE OBJECT loid [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]
[ GRANTED BY role_specification ]
GRANT { { CREATE | USAGE } [, ...] | ALL [ PRIVILEGES ] }
ON SCHEMA schema_name [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]
[ GRANTED BY role_specification ]
GRANT { CREATE | ALL [ PRIVILEGES ] }
ON TABLESPACE tablespace_name [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]
[ GRANTED BY role_specification ]
GRANT { USAGE | ALL [ PRIVILEGES ] }
ON TYPE type_name [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]
[ GRANTED BY role_specification ]
GRANT role_name [, ...] TO role_specification [, ...]
[ WITH ADMIN OPTION ]
[ GRANTED BY role_specification ]
and where role_specification can be:
[ GROUP ] role_name
| PUBLIC
| CURRENT_ROLE
| CURRENT_USER
| SESSION_USER
π DESCRIPTION π
so GRANT has two basic variants, and likeβ¦ they're cousins, not twins π―
- the one that grants privileges on a database object β and by "object" we mean table, column, view, foreign table, sequence, database, foreign-data wrapper, foreign server, function, procedure, procedural language, schema, or tablespace π«
- the one that grants membership in a role π«
They're similar in many ways, but they're different enough that the docs describe them separately. So we're doing that too. periodt.
π GRANT on Database Objects π
This variant gives specific privileges on a database object to one or more roles. And here's the slay part: these privileges are added to the ones already granted, if any β it's additive, bestie, not a reset button β¨
The key word PUBLIC means the privileges go to all roles, including the ones that might be created later. Like, future roles too, they get it π. PUBLIC is basically an implicitly defined group that always includes every role, always. And any particular role will have the sum of:
- privileges granted directly to it
- privileges granted to any role it is 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 β like, they get to pass it on π. Without a grant option, the recipient cannot do that. And heads up: grant options cannot be granted to PUBLIC π«
if GRANTED BY is specified, the specified grantor must be the current user. This clause is currently present in this form only for SQL compatibility. (iykyk β it's there for the vibes/SQL, not for extra power π)
there is no need to grant privileges to the owner of an object (usually the user that created it), because the owner has all privileges by default π. The owner could, however, choose to revoke some of their own privileges for safety. cautious queen behavior.
and okay, this next bit is important: the right to drop an object, or to alter its definition in any way, is not treated as a grantable privilege. It's inherent in the owner, and cannot be granted or revoked. Period. (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. main character energy fr.
π¦ The possible privileges π¦
SELECT,INSERT,UPDATE,DELETE,TRUNCATE,REFERENCES,TRIGGER,CREATE,CONNECT,TEMPORARY,EXECUTE,USAGEβ these are the specific types of privileges, as defined in Section 5.7 βTEMPβ alternative spelling forTEMPORARYπ©·ALL PRIVILEGESβ grants all of the privileges available for the object's type. ThePRIVILEGESkey word is optional in PostgreSQL, though it is required by strict SQL π
the FUNCTION syntax works for plain functions, aggregate functions, and window functions β but not for 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, basically π§
there's also an option to grant privileges on all objects of the same type within one or more schemas. This functionality is currently supported only for tables, sequences, functions, and procedures π
ALL TABLESalso affects views and foreign tables, just like the specific-objectGRANTcommand πALL 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 π«Ά. Membership matters because it conveys the privileges granted to a role to each of its members β like, the perks get shared with the whole friend group π
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. And listen: 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
CREATEROLEprivilege 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 π
β οΈ and unlike the case with privileges, membership in a role cannot be granted toPUBLIC. Also note that this form of the command does not allow the noise wordGROUPinrole_specification. no cap, it's just not allowed there π«
π NOTES π
the REVOKE command is used to revoke access privileges π (yes, it's the opposite twin, we love a duo)
since PostgreSQL 8.1, the concepts of users and groups have been unified into a single kind of entity called a role. So it's 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 "we keep it around for the vibes")
a user may perform SELECT, INSERT, etc. on a column if they hold that privilege for either the specific column or its whole table. But β and this is 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. like, that's just not how it works bestie π
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 β
- 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 π« )
π₯ 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. As withroot, it's unwise to operate as a superuser except when absolutely necessary. like, do NOT just live in superuser mode, that's not the flex you think it is π
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's a whole vibe check: 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 π
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. do that, bestie, save yourself the mystery π
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. don't forget. I'm begging π₯Ί)
see Section 5.7 for more information about specific privilege types, as well as how to inspect objects' privileges π
β¨ okay that's part 1, there's more coming so don't go anywhere β¨
π EXAMPLES π
okay so hereβs how you actually use this thing, bestie β the examples are giving βcopy, paste, slayβ β¨
First one: 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;
π¨ bestie, read this note before you go wild: the above will indeed grant all privileges if itβs executed by a superuser or the owner of kinds. But when itβs executed by someone else, it will only grant those permissions for which the someone else has grant options. no cap.
Grant membership in role admins to user joe:
GRANT admins TO joe;
thatβs it, those are the examples, iconic behavior π
π¦ COMPATIBILITY π¦
okay, compatibility tea β β according to the SQL standard, the PRIVILEGES key word in ALL PRIVILEGES is required. Also, the SQL standard does not support setting the privileges on more than one object per command. periodt.
PostgreSQL, though? It 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, and TRUNCATE privileges. This is not possible according to the SQL standard. Why? Because 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. itβs giving loophole, but make it SQL π.
According to the SQL standard, grant options can be granted to PUBLIC; PostgreSQL only supports granting grant options to roles. sorry, PUBLIC, no grant-option era for you π.
The SQL standard allows the GRANTED BY option to specify only CURRENT_USER or CURRENT_ROLE. The other variants are PostgreSQL extensions. iykyk β¨.
The SQL standard provides for a USAGE privilege on other kinds of objects: character sets, collations, translations.
In the SQL standard, sequences only have a USAGE privilege, which controls the use of the NEXT VALUE FOR expression, which is equivalent to the function nextval in PostgreSQL. The sequence privileges SELECT and UPDATE are PostgreSQL extensions. The application of the sequence USAGE privilege to the currval function is also a PostgreSQL extension (as is the function itself).
Privileges on databases, tablespaces, schemas, and languages are PostgreSQL extensions.
π SEE ALSO π
go check out these other iconique pages, bestie: REVOKE(7), and ALTER DEFAULT PRIVILEGES (ALTER_DEFAULT_PRIVILEGES(7)). π«Ά
mwah π this page is from PostgreSQL 14.24, dated 2026, and itβs literally GRANT(7). bye bestie π
π¦ where this page comes from
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-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