Sub topic 3
Ownership Versus Privileges
This chapter assumes you can connect to PostgreSQL, create tables, and use basic SELECT, INSERT, UPDATE, and DELETE statements. It also assumes you know that a database contains schemas, and schemas contain tables and other objects.
PostgreSQL separates ownership from privileges:
- Ownership identifies the role that controls an object.
- A privilege is a permission granted to a role.
- Ownership is attached to exactly one role at a time.
- Privileges can be granted to many roles.
What ownership means
When a role creates a table, that role normally becomes the table's owner. The owner has the object's built-in permissions and can perform administrative actions such as:
- Changing the object's definition.
- Dropping the object.
- Granting privileges on the object to other roles.
- Changing some security settings.
A GRANT does not make the recipient an owner. For example, granting SELECT lets a role read rows, but it does not let that role drop the table or add columns.
CREATE ROLE app_owner NOLOGIN;
CREATE ROLE app_readonly NOLOGIN;
CREATE ROLE analyst LOGIN PASSWORD 'change-me';
CREATE SCHEMA reports AUTHORIZATION app_owner;
SET ROLE app_owner;
CREATE TABLE reports.sales (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
amount numeric(10,2) NOT NULL
);
GRANT USAGE ON SCHEMA reports TO app_readonly;
GRANT SELECT ON reports.sales TO app_readonly;
GRANT app_readonly TO analyst;
RESET ROLE;The table is owned by app_owner. The analyst role can reach the schema through app_readonly and can read the table, but it does not own the table.
Ownership follows the role that creates the object. SET ROLE changes the effective role for subsequent commands, which is useful when a deployment process should create objects under a stable owner role instead of under an individual login.
Privileges do not replace ownership
Privileges describe allowed operations. Common table privileges include:
| Privilege | Allows |
|---|---|
| `SELECT` | Read rows or selected columns |
| `INSERT` | Add rows |
| `UPDATE` | Change rows or selected columns |
| `DELETE` | Remove rows |
| `TRUNCATE` | Remove all rows quickly |
| `REFERENCES` | Create foreign keys that refer to the table |
| `TRIGGER` | Create triggers on the table |
A role may receive several privileges without becoming the owner. PostgreSQL checks the effective privileges of the session role, including privileges inherited through memberships.
The owner is not the same as a superuser
A table owner has broad control over that table, but a normal owner does not automatically control every object in the database. A superuser bypasses most permission checks across the server. Superuser status is a role attribute, not an ownership relationship.
An owner can also grant privileges to other roles. A privilege grant may include WITH GRANT OPTION, allowing the recipient to grant that privilege onward. This still does not transfer ownership.
GRANT SELECT ON reports.sales
TO app_readonly
WITH GRANT OPTION;Granting this option should be deliberate. It makes privilege control harder to audit because the recipient can create additional grant paths.
Ownership and privilege paths
When ownership must change, use an ownership operation such as ALTER TABLE ... OWNER TO, and ensure the target role can own objects in that schema. A normal privilege grant cannot accomplish this.
Exercise
The role analyst has SELECT, INSERT, and UPDATE on reports.sales. Can analyst drop the table merely because it has those privileges?
Show answer
No. Those are object privileges, not ownership. The table owner, a superuser, or a role with the required administrative authority must drop it.