Sub topic 3
Ownership versus privileges
This chapter assumes you can create roles with CREATE ROLE and can read basic GRANT syntax. It separates two ideas that are easy to blur: who owns an object, and what a role has been granted on it. Mixing them up causes many permission problems, such as an application that can drop its own tables or a migration that fails with "must be owner of table".
Every object has exactly one owner
Each database object has an owner. That includes databases, schemas, tables, sequences, views, functions, types, and more. The owner is the role that created the object, unless you say otherwise at creation time or change it later.
CREATE ROLE app_owner NOLOGIN;
CREATE ROLE app_readonly NOLOGIN;
CREATE SCHEMA shop AUTHORIZATION app_owner;
SET ROLE app_owner;
CREATE TABLE shop.orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer text NOT NULL,
sales_rep name NOT NULL,
total numeric(10,2) NOT NULL
);
RESET ROLE;
SELECT tablename, tableowner
FROM pg_tables
WHERE schemaname = 'shop'; tablename | tableowner
-----------+------------
orders | app_owner
(1 row)Run this as a superuser or as a role that can SET ROLE app_owner. CREATE SCHEMA ... AUTHORIZATION makes app_owner the schema's owner. SET ROLE makes the session act as app_owner, so the table is created with that owner. Without SET ROLE, the table would belong to whoever ran the script, usually a superuser or a personal login. That is a common mistake.
You can see ownership in psql with \dt shop.* or \dn+. In SQL, look at pg_tables.tableowner, pg_namespace.nspowner, or pg_database.datdba.
What ownership gives you
An owner has all applicable privileges on the object automatically. The owner also holds rights that are not privileges and cannot be granted to anyone else:
| Action | Who can do it | Can it be granted? |
|---|---|---|
| `SELECT`, `INSERT`, `UPDATE`, `DELETE` and the other privilege types | Owner by default; others after `GRANT` | Yes |
| `ALTER` the object, including changing its definition | Owner (and superusers) | No |
| `DROP` the object | Owner, the schema's owner, and superusers | No |
| `GRANT` and `REVOKE` on the object | Owner, or a role holding a privilege `WITH GRANT OPTION` | Only through `GRANT OPTION` |
| Change the owner with `ALTER ... OWNER TO` | Owner, if it can also act as the new owner | No |
The consequence is practical. If an application connects as the table owner, a bug or an SQL injection can run DROP TABLE or ALTER TABLE. If the application connects as a role that was merely granted SELECT, INSERT, UPDATE, it cannot, because those rights stop at data manipulation.
Superusers skip permission checks entirely, so ownership rarely matters to them. Everything in this chapter describes the behavior of ordinary roles.
Ownership is not the same as privilege
Privileges are stored in the object's access control list (ACL). Ownership is stored separately, as a single owner column. The owner's rights come from a default ACL that PostgreSQL assumes when the ACL is empty. When you inspect a new table with \dp, the "Access privileges" column is blank. Blank means the defaults apply: the owner holds everything and nobody else holds anything.
The first GRANT or REVOKE on an object writes the ACL out explicitly.
GRANT USAGE ON SCHEMA shop TO app_readonly;
GRANT SELECT ON shop.orders TO app_readonly; Schema | Name | Type | Access privileges
--------+--------+-------+-------------------------------
shop | orders | table | app_owner=arwdDxt/app_owner +
| | | app_readonly=r/app_ownerEach entry reads grantee=privileges/grantor. The letter r is SELECT. The owner's string arwdDxt lists the table privileges INSERT (a), SELECT (r), UPDATE (w), DELETE (d), TRUNCATE (D), REFERENCES (x), and TRIGGER (t). PostgreSQL 17 and later add m for MAINTAIN. The grantor field records which role issued the grant, and it matters later when you revoke.
The owner can revoke its own privileges
Because owner privileges sit in the ACL like any other, the owner can remove them. This is rarely a good idea, but it shows that ownership is more than a privilege.
SET ROLE app_owner;
REVOKE SELECT ON shop.orders FROM app_owner;
SELECT count(*) FROM shop.orders;ERROR: permission denied for table ordersThe owner can still run ALTER TABLE, DROP TABLE, and GRANT, because those rights do not come from the ACL. It can also give itself the privilege back.
GRANT SELECT ON shop.orders TO app_owner;
SELECT count(*) FROM shop.orders;
RESET ROLE;The second SELECT succeeds. A table owner is therefore never locked out of its own table for good.
How PostgreSQL decides
When a role runs a statement against an object, PostgreSQL checks in a fixed order. The same order explains most surprises.
For ownership-only actions, membership in the owning role generally counts as ownership. The exact rule depends on how inheritance is set for that membership, which a later chapter covers. For DROP, the owner of the containing schema also qualifies.
Access to an object also requires access to its container. The first SELECT by app_readonly in this chapter would have failed with permission denied for schema shop until the schema USAGE grant above. Table privileges alone are not enough.
Changing an owner
Use ALTER ... OWNER TO. Each object type has its own form.
ALTER TABLE shop.orders OWNER TO app_owner;
ALTER SCHEMA shop OWNER TO app_owner;
ALTER DATABASE appdb OWNER TO app_owner;The role running the command must own the object, or be able to act as its owner. It must also be able to act as the new owner, either by being a member of that role or by being a superuser. For tables and other schema objects, the new owner must hold CREATE on the schema that contains the object.
The change does more than rewrite one column.
- ACL entries that name the old owner are rewritten to name the new owner.
- Indexes, TOAST tables, and sequences owned by the table (including identity sequences) move with it.
- Other grants stay in place. Grants made by the old owner keep their recorded grantor, which is rewritten to the new owner.
- Dependent objects that are separate, such as a view built on the table or a function that uses it, keep their own owners. Change them separately.
Changing owners one object at a time does not scale. For bulk changes, REASSIGN OWNED (covered in the chapter on dropping roles) moves everything a role owns in the current database.
Practical rules
- Create objects as a dedicated owner role, not as a superuser and not as a personal login. Use
SET ROLE app_ownerin scripts, or connect as that role for migrations. - Make the owner a
NOLOGINgroup role if possible. Migration tools and administrators then useSET ROLE, and no password is tied to the owner. - Never run the application as the owner. Grant it only the privileges it needs.
- Check for objects owned by the wrong role after restores and imports.
pg_dumpoutput restores owners faithfully, but a restore run with--no-ownermakes the restoring role own everything.
SELECT n.nspname AS schema, c.relname AS object, r.rolname AS owner
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
JOIN pg_roles r ON r.oid = c.relowner
WHERE n.nspname = 'shop'
AND r.rolname <> 'app_owner'
ORDER BY c.relname;An empty result means every relation (tables, indexes, sequences, views) in shop belongs to app_owner. Any row is an owner that needs fixing.
Exercise
The table shop.invoices was created by the login role alice, and app_owner now needs to run ALTER TABLE on it. Which statement fixes this, who must run it, and what two conditions must hold for it to succeed?
Show answer
Run ALTER TABLE shop.invoices OWNER TO appowner; as alice, a member of alice, or a superuser. The running role must be able to act as appowner (by membership or as a superuser), and appowner must have CREATE on schema shop. Because appowner owns the schema, the second condition already holds.
Exercise
The owner of shop.orders runs REVOKE ALL ON shop.orders FROM app_owner; Can it still drop the table, and can it query it again? Explain.
Show answer
It can still drop the table, because DROP is a right of ownership and is not stored in the ACL. It cannot query the table until it grants itself SELECT again, which it is allowed to do with GRANT SELECT ON shop.orders TO app_owner; because granting is also a right of ownership.