PostgreSQL 7.2.1 Documentation
Prev |  | Next

# GRANT

## Name

GRANT -- define access privileges

## Synopsis

    GRANT { { SELECT | INSERT | UPDATE | DELETE | RULE | REFERENCES | TRIGGER } [,...] | ALL [ PRIVILEGES ] }
        ON [ TABLE ] objectname [, ...]
        TO { username | GROUP groupname | PUBLIC } [, ...]

## Description

The GRANT command gives specific permissions on an object (table, view, sequence) to one or more users or groups of users. These permissions are added to those already granted, if any.

The key word `PUBLIC` indicates that the privileges are to be granted to all users, including those that may be created later. `PUBLIC` may be thought of as an implicitly defined group that always includes all users. Note that any particular user will have the sum of privileges granted directly to him, privileges granted to any group he is presently a member of, and privileges granted to `PUBLIC`.

Users other than the creator of an object do not have any access privileges to the object unless the creator grants permissions. There is no need to grant privileges to the creator of an object, as the creator automatically holds all privileges. (The creator could, however, choose to revoke some of his own privileges for safety. Note that the ability to grant and revoke privileges is inherent in the creator and cannot be lost. The right to drop the object is likewise inherent in the creator, and cannot be granted or revoked.)

The possible privileges are:

SELECT

  Allows SELECT from any column of the specified table, view, or sequence. Also allows the use of COPY FROM.

INSERT

  Allows INSERT of a new row into the specified table. Also allows COPY TO.

UPDATE

  Allows UPDATE of any column of the specified table. `SELECT ... FOR UPDATE` also requires this privilege (besides the `SELECT` privilege). For sequences, this privilege allows the use of `nextval`, `currval` and `setval`.

DELETE

  Allows DELETE of a row from the specified table.

RULE

  Allows the creation of a rule on the table/view. (See CREATE RULE statement.)

REFERENCES

  To create a table with a foreign key constraint, it is necessary to have this privilege on the table with the referenced key.

TRIGGER

  Allows the creation of a trigger on the specified table. (See CREATE TRIGGER statement.)

ALL PRIVILEGES

  Grant all of the above privileges at once. The `PRIVILEGES` key word is optional in PostgreSQL, though it is required by strict SQL.

The privileges required by other commands are listed on the reference page of the respective command.

## Notes

It should be noted that database superusers can access all objects regardless of object privilege settings. This is comparable to the rights of `root` in a Unix system. As with `root`, it's unwise to operate as a superuser except when absolutely necessary.

Currently, to grant privileges in PostgreSQL to only a few columns, you must create a view having the desired columns and then grant privileges to that view.

Use psql's \z command to obtain information about privileges on existing objects:

              Database    = lusitania
       +------------------+---------------------------------------------+
       |  Relation        |        Grant/Revoke Permissions             |
       +------------------+---------------------------------------------+
       | mytable          | {"=rw","miriam=arwdRxt","group todos=rw"}   |
       +------------------+---------------------------------------------+
       Legend:
             uname=arwR -- privileges granted to a user
       group gname=arwR -- privileges granted to a group
                  =arwR -- privileges granted to PUBLIC

                      r -- SELECT ("read")
                      w -- UPDATE ("write")
                      a -- INSERT ("append")
                      d -- DELETE
                      R -- RULE
                      x -- REFERENCES
                      t -- TRIGGER
                arwdRxt -- ALL PRIVILEGES

The REVOKE command is used to revoke access privileges.

## Examples

Grant insert privilege to all users on table films:

    GRANT INSERT ON films TO PUBLIC;

Grant all privileges to user `manuel` on view `kinds`:

    GRANT ALL PRIVILEGES ON kinds TO manuel;

## Compatibility

### SQL92

The `PRIVILEGES` key word in `ALL PRIVILEGES` is required. SQL does not support setting the privileges on more than one table per command.

The SQL92 syntax for GRANT allows setting privileges for individual columns within a table, and allows setting a privilege to grant the same privileges to others:

    GRANT privilege [, ...]
        ON object [ ( column [, ...] ) ] [, ...]
        TO { PUBLIC | username [, ...] } [ WITH GRANT OPTION ]

SQL allows to grant the USAGE privilege on other kinds of objects: CHARACTER SET, COLLATION, TRANSLATION, DOMAIN.

The TRIGGER privilege was introduced in SQL99. The RULE privilege is a PostgreSQL extension.

## See Also

REVOKE

Prev | Home | Next
FETCH | Up | INSERT