PostgreSQL GRANT
Summary: in this tutorial, you will learn how to use the PostgreSQL GRANT
statement to grant privileges on database objects to a role.
Introduction to PostgreSQL GRANT statement
After creating a role with the LOGIN
attribute, the role can log in to the PostgreSQL database server.
However, it cannot do anything to the database objects like tables, views, functions, etc. For example, the role cannot select data from a table or execute a specific function.
To allow a role to interact with database objects, you need to grant privileges on the database objects to the role using the GRANT
statement.
The following shows the simple form of the GRANT
statement that grants one or more privileges on a table to a role:
In this syntax:
- First, specify the
privilege_list
that can beSELECT
,INSERT
,UPDATE
,DELETE
,TRUNCATE
, etc. Use theALL
option to grant all privileges on a table to the role. - Second, provide the name of the table after the
ON
keyword. - Third, indicate the name of the role to which you want to grant privileges.
PostgreSQL GRANT statement examples
First, use the postgres
user to connect to the PostgreSQL server using any client tool of your choice, for example, psql:
Second, create a new user role called joe
that can log in to the PostgreSQL server:
Replace the YourPassword
with the one you want.
Third, create a new table called candidates
:
Fourth, use the role joe
to log in to the PostgreSQL server in a separate session.
Fifth, attempt to select data from the candidates
table from the joe
‘s session:
PostgreSQL issued an error:
The output indicates that the role joe does not have the privilege of retrieving data from the candidates
table.
To grant the SELECT
privilege on the candidates
table to the role joe
, you execute the following GRANT
statement in the postgres
‘ session:
Sixth, execute the SELECT
statement from the joe
‘s session:
PostgreSQL returns an empty result set instead of an error.
Seventh, execute the following INSERT
statement:
PostgreSQL issued the following error because joe
does not have the INSERT
privilege on the candidates
table:
Eighth, grant INSERT
, UPDATE
, and DELETE
privileges on the candidates
table to the role joe
:
Ninth, execute the INSERT
statement again from the joe
‘s session:
Now, joe
can insert data into the candidates
table. Additionally, it can update or delete data from the table.
More PostgreSQL GRANT statement examples
Let’s take some more examples of using the GRANT
statement.
1) Grant all privileges on a table to a role
The following statement grants all privileges on the candidates
table to the role joe
:
2) Grant all privileges on all tables in a schema to a role
The following statement grants all privileges on all tables in the public
schema of the dvdrental
sample database to the role joe
:
3) Grant SELECT on all tables
Sometimes, you want to create a readonly role that can only select data from all tables in a specified schema.
To do that, you can grant the SELECT
privilege on all tables in the public
schema like this:
So far, you have learned how to grant privileges on tables. To grant privileges to a role on other database objects, check the GRANT
statement syntax.
Summary
- Use the
GRANT
statement to grant privileges on database objects to a role.