PostgreSQL Roles & Privileges
A database is usually shared by many people and programs: the web application, a reporting tool, an administrator. PostgreSQL controls who may do what with roles and privileges. A role can be a user (it can log in) or a group (it collects privileges that other roles inherit), and the same command, CREATE ROLE, makes both.
Creating roles
CREATE ROLE reporting; -- a group: cannot log in
CREATE ROLE anna LOGIN PASSWORD 'change-me'; -- a user
CREATE USER ben; -- CREATE USER = CREATE ROLE ... LOGIN
SELECT rolname, rolcanlogin, rolsuper
FROM pg_roles
WHERE rolname IN ('reporting', 'anna', 'ben')
ORDER BY rolname;CREATE ROLE
CREATE ROLE
CREATE ROLE
rolname | rolcanlogin | rolsuper
-----------+-------------+----------
anna | t | f
ben | t | f
reporting | f | f
(3 rows)
| Attribute | Allows |
|---|---|
LOGIN |
connecting to the server |
SUPERUSER |
everything, bypassing all checks (use sparingly!) |
CREATEDB, CREATEROLE |
creating databases / roles |
PASSWORD '…' |
password authentication |
VALID UNTIL '2026-12-31' |
password expiry |
Granting privileges
A new role can do almost nothing. GRANT gives privileges on specific objects; REVOKE takes them away:
| Privilege | On tables means |
|---|---|
SELECT |
read rows |
INSERT, UPDATE, DELETE |
change rows |
TRUNCATE, REFERENCES, TRIGGER |
the corresponding operations |
ALL PRIVILEGES |
all of the above |
SET ROLE lets a superuser act as another role, which is perfect for testing privileges:
CREATE ROLE clerk;
GRANT SELECT ON books, authors TO clerk;
GRANT UPDATE (stock) ON books TO clerk;
SET ROLE clerk;
SELECT current_user;
SELECT title, stock FROM books WHERE id = 1;
UPDATE books SET stock = 20 WHERE id = 1;
UPDATE books SET price = 1 WHERE id = 1;
SELECT count(*) FROM customers;
RESET ROLE;CREATE ROLE
GRANT
GRANT
SET
current_user
--------------
clerk
(1 row)
title | stock
---------------+-------
The Salt Road | 12
(1 row)
UPDATE 1
ERROR: permission denied for table books
ERROR: permission denied for table customers
RESET
clerk may read books and change only the stock column. Changing the price or reading customers is refused with permission denied.
Groups and inheritance
Instead of granting privileges to every user, grant them to a group role and make users members of it. Members inherit the group's privileges:
CREATE ROLE reporting;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO reporting;
CREATE ROLE analyst LOGIN;
GRANT reporting TO analyst;
SET ROLE analyst;
SELECT count(*) AS orders_visible FROM orders;
DELETE FROM orders WHERE id = 1;
RESET ROLE;
REVOKE reporting FROM analyst;
SET ROLE analyst;
SELECT count(*) FROM orders;
RESET ROLE;CREATE ROLE
GRANT
CREATE ROLE
GRANT ROLE
SET
orders_visible
----------------
20
(1 row)
ERROR: permission denied for table orders
RESET
REVOKE ROLE
SET
ERROR: permission denied for table orders
RESET
After REVOKE, the analyst lost everything that came from the group.
Note:
GRANT … ON ALL TABLES IN SCHEMAonly covers tables that exist now. For future tables, useALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO reporting;.
Checking privileges
has_table_privilege answers "may this role do that?" and information_schema.role_table_grants lists the grants:
CREATE ROLE clerk;
GRANT SELECT, INSERT ON orders TO clerk;
SELECT has_table_privilege('clerk', 'orders', 'SELECT') AS can_read,
has_table_privilege('clerk', 'orders', 'DELETE') AS can_delete,
has_table_privilege('clerk', 'books', 'SELECT') AS can_read_books;
SELECT grantee, table_name, privilege_type
FROM information_schema.role_table_grants
WHERE grantee = 'clerk'
ORDER BY privilege_type;CREATE ROLE
GRANT
can_read | can_delete | can_read_books
----------+------------+----------------
t | f | f
(1 row)
grantee | table_name | privilege_type
---------+------------+----------------
clerk | orders | INSERT
clerk | orders | SELECT
(2 rows)
Ownership and schemas
The role that creates an object owns it and can always do anything with it, including DROP. Ownership can be changed with ALTER TABLE … OWNER TO role. Schemas have their own privileges: USAGE (may look inside) and CREATE (may create objects there).
Row-level security
Privileges work on whole tables or columns. Row-level security (RLS) goes further and filters rows per user with policies:
CREATE ROLE uk_staff;
GRANT SELECT ON customers TO uk_staff;
ALTER TABLE customers ENABLE ROW LEVEL SECURITY;
CREATE POLICY only_uk ON customers FOR SELECT TO uk_staff
USING (country = 'UK');
SET ROLE uk_staff;
SELECT first_name, city FROM customers ORDER BY first_name;
RESET ROLE;
SELECT count(*) AS all_customers FROM customers;CREATE ROLE
GRANT
ALTER TABLE
CREATE POLICY
SET
first_name | city
------------+---------
Ben | Leeds
Isla | Glasgow
Olivia | Leeds
(3 rows)
RESET
all_customers
---------------
15
(1 row)
The table owner (and superusers) are not affected by the policy, which is why the last count still sees everybody.
Principle of least privilege
- Let applications connect with a role that has only the privileges they need, never as a superuser.
- Use group roles (
app_read,app_write) and grant them to login roles. - Revoke the default
CREATEprivilege onpublicfromPUBLICin older databases (PostgreSQL 15+ already does this for new databases).
Test yourself
What is the difference between CREATE USER and CREATE ROLE?
- None at all
- CREATE USER adds LOGIN by default
- CREATE USER creates a superuser
- Roles can't own tables
How do you let a role read every existing table in schema public?
- GRANT READ ON public
- GRANT SELECT ON ALL TABLES IN SCHEMA public TO role
- ALTER ROLE role SELECT
- GRANT USAGE ON public
What does row-level security add on top of GRANT?
- Column permissions
- Per-row filtering through policies
- Password encryption
- Faster queries