Users and permissions
Overview
So far you’ve used the database as its all-powerful owner. In a real organisation many people and programs connect to the same database, and they shouldn’t all be able to do everything. An analyst needs to read sales data but never delete it; the web application needs to insert orders but not drop tables; a support agent should change an order’s status but not its price. PostgreSQL’s roles and privileges let you say exactly who may do what — and the database enforces it for every statement.
Roles: users and groups
PostgreSQL has one concept, the role, for both users and groups:
- a role with the
LOGINattribute can connect — it’s a user; - a role without it can’t connect, but can be granted privileges and have other roles as members — it’s a group.
CREATE ROLE analyst; -- a group
CREATE ROLE maya LOGIN PASSWORD 'change-me' IN ROLE analyst; -- a user in that group
CREATE ROLE omar LOGIN PASSWORD 'change-me' IN ROLE analyst;
SELECT r.rolname, r.rolcanlogin,
ARRAY(SELECT g.rolname FROM pg_auth_members m
JOIN pg_roles g ON g.oid = m.roleid
WHERE m.member = r.oid) AS member_of
FROM pg_roles AS r
WHERE r.rolname IN ('analyst', 'maya', 'omar')
ORDER BY r.rolname;CREATE ROLE
CREATE ROLE
CREATE ROLE
rolname | rolcanlogin | member_of
---------+-------------+-----------
analyst | f | {}
maya | t | {analyst}
omar | t | {analyst}
(3 rows)In psql, \du lists roles more conveniently. Roles belong to the whole server, not to one database. (In real life, never put a real password in a script; set it interactively with psql’s \password maya.)
Granting privileges
A new role can connect but do almost nothing. GRANT gives it privileges on specific objects:
The analysts should be able to read everything in the shop:
GRANT USAGE ON SCHEMA public TO analyst;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO analyst;GRANT
GRANTTwo levels are involved: USAGE on a schema lets a role look inside it (the public schema allows this for everyone by default), and SELECT on the tables lets it read them. ON ALL TABLES IN SCHEMA is a shortcut for every table that exists now; tables created later need their own grant, or a default:
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO analyst;Testing privileges with SET ROLE
As a superuser, SET ROLE lets you act as another role to check what it can do — very useful when setting up permissions. Maya can read:
SET ROLE maya;
SELECT current_user, count(*) AS products FROM products;
RESET ROLE;SET
current_user | products
--------------+----------
maya | 12
(1 row)
RESETShe inherited SELECT from the analyst group. But she can’t change anything:
SET ROLE maya;
UPDATE products SET price = 0 WHERE id = 1;
DELETE FROM orders WHERE id = 1;
RESET ROLE;SET
ERROR: permission denied for table products
ERROR: permission denied for table orders
RESETBecause privileges come from the group, adding a new analyst is one line (GRANT analyst TO new_person;), and removing someone’s access is REVOKE analyst FROM maya; or DROP ROLE maya; — no hunting down individual grants.
The privileges
| Privilege | Allows |
|---|---|
SELECT |
reading rows (and COPY … TO) |
INSERT |
adding rows |
UPDATE |
changing rows (can be limited to columns) |
DELETE |
removing rows |
TRUNCATE |
emptying the table |
REFERENCES |
creating foreign keys that point at the table |
USAGE |
on a schema: accessing objects in it; on a sequence: using it |
CREATE |
on a schema: creating objects in it |
ALL |
everything that applies to the object |
An application role
The web shop’s backend needs to read products and customers and to create and update orders — but it never deletes orders, and it certainly never drops tables:
CREATE ROLE shop_app LOGIN PASSWORD 'change-me';
GRANT SELECT ON products, categories, customers TO shop_app;
GRANT SELECT, INSERT, UPDATE ON orders, order_items TO shop_app;
GRANT INSERT, UPDATE ON customers TO shop_app;
SET ROLE shop_app;
INSERT INTO orders (customer_id, ordered_at, status) VALUES (9, '2026-12-04 10:00', 'pending')
RETURNING id, status;
DELETE FROM orders WHERE id = 1;
DROP TABLE products;
RESET ROLE;CREATE ROLE
GRANT
GRANT
GRANT
SET
id | status
----+---------
25 | pending
(1 row)
INSERT 0 1
ERROR: permission denied for table orders
ERROR: must be owner of table products
RESETThe insert works (identity columns don’t need a separate grant). The delete and the drop are refused — so even if the application had a bug, or an attacker found a way to run SQL through it, the damage would be limited. Only the table’s owner (and superusers) can ALTER or DROP it, which is why schema changes are run by a separate, more powerful migration role.
Column privileges and views
Privileges can be narrower than a whole table. UPDATE (status) lets a role change one column only, and SELECT (id, first_name, city) lets it read only some columns. Often a cleaner way to expose part of a table is a view: grant access to the view and not to the table, and the role sees exactly what the view shows:
CREATE VIEW customer_directory AS
SELECT id, first_name, city, country FROM customers;
CREATE ROLE reporting;
GRANT SELECT ON customer_directory TO reporting;
SET ROLE reporting;
SELECT * FROM customer_directory WHERE country = 'UK';
SELECT email FROM customers LIMIT 1;
RESET ROLE;CREATE VIEW
CREATE ROLE
GRANT
SET
id | first_name | city | country
----+------------+-------+---------
2 | Ben | Leeds | UK
(1 row)
ERROR: permission denied for table customers
RESETThe reporting role can read names and cities through the view but has no way to reach emails or phone numbers. The view runs with its owner’s privileges on the underlying table, which is what makes this work.
For rules about rows rather than columns — “each salesperson sees only their own customers” — PostgreSQL offers row-level security (ALTER TABLE … ENABLE ROW LEVEL SECURITY plus CREATE POLICY), widely used in multi-tenant applications.
REVOKE
REVOKE removes privileges, with the same syntax:
REVOKE UPDATE ON orders FROM shop_app;
SELECT grantee, privilege_type
FROM information_schema.role_table_grants
WHERE table_name = 'orders' AND grantee = 'shop_app'
ORDER BY privilege_type;REVOKE
grantee | privilege_type
----------+----------------
shop_app | INSERT
shop_app | SELECT
(2 rows)information_schema.role_table_grants (or \dp orders in psql) shows who has which privileges on a table.
Least privilege in practice
- One role per application, with only the privileges it uses. Never let an application connect as
postgresor another superuser. - Group roles for people (
analyst,support), and membership for individuals. - A separate migration role that owns the tables and runs
CREATE/ALTER, used only by your deployment process. - Read-only access for reporting, ideally through views — or on a read replica, so heavy reports don’t slow the shop down.
- Secrets out of code: passwords in environment variables or a secrets manager, and
scram-sha-256authentication (the default in current PostgreSQL).
Tip: when someone asks “can you give me access to the database?”, ask what they need to do. “Read last month’s orders” is a
SELECTon a view, not the owner password.
A role for the support team
The support team needs to look up orders and customers and to change an order's status — nothing else. Create a role support (no login needed). Grant it SELECT on orders and customers, and UPDATE on only the status column of orders. Then test it: switch to the role with SET ROLE support, change order 21's status to 'shipped' (allowed), try to set order 21's shipping_fee to 0 (must be denied), try to delete order 21 (must be denied), and switch back with RESET ROLE. Finally, as yourself, select order 21's id, status and shipping_fee.
CREATE ROLE support;
-- grants, then test with SET ROLE support … RESET ROLECREATE ROLE support;
GRANT SELECT ON orders, customers TO support;
GRANT UPDATE (status) ON orders TO support;
SET ROLE support;
UPDATE orders SET status = 'shipped' WHERE id = 21;
UPDATE orders SET shipping_fee = 0 WHERE id = 21;
DELETE FROM orders WHERE id = 21;
RESET ROLE;
SELECT id, status, shipping_fee FROM orders WHERE id = 21;Summary
- PostgreSQL has roles: a role with LOGIN is a user; a role without it is usually a group that other roles are members of.
- GRANT gives privileges — SELECT, INSERT, UPDATE, DELETE and more — on tables, columns, views and schemas; REVOKE takes them away.
- Privileges given to a group role are inherited by its members, so manage access by group rather than per person.
- Grant access to a view instead of its tables to expose only some columns or rows.
- Follow least privilege: applications and people get only what they need, and nobody connects as a superuser for everyday work.