Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Databases - Practical PostgreSQL
Previous Page Home Next Page

Granting privileges with GRANT

To assign a privilege to a user or group, use SQL's GRANT command. Here is the syntax for GRANT:

  GRANT 
privilege
 [, ...] ON 
object
 [, ...]
        TO { PUBLIC | 
username
 | GROUP 
groupname
 }

In this syntax, privilege is any of the privileges listed in Table 10-2, object is the name of the database object (table, view or sequence) that a privilege is being granted on, and the token following the TO keyword describes who the privilege is being granted to. Multiple privileges and objects may be listed, separated from one another by commas.

Only one of the terms following TO may be used in a single GRANT statement. Granting rights with the PUBLIC keyword indiscriminately grants the intended privilege to the special "public" target. PUBLIC privileges are shared by all users. Specifying a username grants the privilege to specific user. Likewise, specifying a groupname grants the privilege to a specific group.

Suppose, for example, that the manager user needs all rights to the customers, books, editions and publishers tables. Example 10-17 gives the manager user those rights, a single GRANT statement.

Example 10-17. Granting user privileges

booktown=# 
GRANT ALL ON customers, books, editions, publishers

booktown-# 
          TO manager;

CHANGE

The use of the ALL keyword in Example 10-17 grants all possible ACL rights (SELECT, UPDATE, etc.) for the specified objects to the user manager. The CHANGE message from the server indicates that the privileges were correctly modified. Remember that you can use the \z command in psql in order to verify permissions on a database object.

booktown=# 
\z publishers

Access permissions for database "booktown"
  Relation  |  Access permissions
------------+----------------------
 publishers | {"=","manager=arwR"}
(1 row)

As another example, let's look at the use of the GROUP keyword to grant privileges to members of a group groupname . For instance, the entire sales department at the Book Town should be given permission to view the customers table, but not to modify it. Example 10-18 grants SELECT access on the customers table to any member of the sales group.

Example 10-18. Granting group privileges

booktown=# 
GRANT SELECT ON customers TO GROUP sales;

CHANGE
booktown=# 
\z customers

 Access permissions for database "booktown"
 Relation  |       Access permissions
-----------+---------------------------------
 customers | {"=","manager=arwR","group sales=r"}
(1 row)
Databases - Practical PostgreSQL
Previous Page Home Next Page

 
 
  Published under the terms of the Open Publication License Design by Interspire