This page in other versions: 9.0 / 9.1 / 9.2 / 9.3 / 9.4  |  Development versions: devel  |  Unsupported versions: 7.1 / 7.2 / 7.3 / 7.4 / 8.0 / 8.1 / 8.2 / 8.3 / 8.4

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

Comments


May 14, 2002, 6:14 a.m.

Note that there is currently no way to grant permissions on functions. This means you can't create a table with fields that are only visible to some users through functions. E.g. if you're planning on creating an authentication system, your 'read'ers will have to be able to SELECT password fields directly or indirectly (through a view).


Oct. 13, 2002, 9:27 p.m.

If you want to grant all right on all tables in database 'demo1' to user 'www' you could use following shell trick:

$ psql -t -A -c "select tablename from pg_tables where tablename not like 'pg_%'" demo1 | xargs -i psql -c "grant all on {} to www" demo1


Sept. 11, 2003, 11:48 a.m.

Or even better, using plpgsql:

CREATE OR REPLACE FUNCTION grantall(varchar, varchar)
RETURNS boolean AS
\'BEGIN
EXECUTE (\'\'GRANT ALL ON TABLE \'\' || $1 || \'\' TO \'\' || $2 );
RETURN TRUE;
END;\'
LANGUAGE \'plpgsql\';

select grantall(tablename,\'lims\') from pg_tables where tablename not like \'pg_%\';


Jan. 19, 2005, 2:18 p.m.

I have made a simple shell script for giving privileges to all the tables (and sequences) of a DB:

#!/bin/sh
#By Claudio Fanelli
#tested only on psql v 7.4.6 on debian and v 7.2.4 on redhat 8.0
#use at your own risk
echo "You need to run this script as user 'postgres'"
echo
echo "We give one or more privileges to an user on all tables(and sequences) of a given DB"
#echo "diamo un certo privilegio ad un utente su tutte le tabelle e sequenze di$

echo -n "database:"
read DB
echo -n "user:"
read U
echo -n "privilege(s) (ex:select,update or all):"
read PRIV
psql -c '\z' $DB |\
sed -ne "/[|]/p" |\
sed -ne "2~1p" |\
awk -F'|' '{print $(NF-1)}' |\
xargs -i psql -c "grant $PRIV on {} to $U" $DB

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group