Diagnostic Queries
Symptoms
A role attempted an operation reserved for the object owner (or a superuser) — such as ALTER, DROP, TRUNCATE, or changing ownership — on a table it does not own. PostgreSQL reports SQLSTATE 42501.
- DDL like ALTER/DROP fails even when the role can read and write the table.
- The message names the relation the role must own.
- Common when application and migration roles differ.
What the server log shows
ERROR: must be owner of relation orders
Why PostgreSQL raises this — what the manual says
the ALTER TABLE reference (Description):
“You must own the table to use ALTER TABLE.”
Schema-changing commands require ownership rather than a grantable privilege. The catalog records the owner in pg_class.relowner; if the current role is neither the owner (directly or via role membership) nor a superuser, the command is rejected with 42501.
Common causes
- Migrations run as the application role instead of the owner role.
- Object owned by a different team/role.
- Ownership not transferred after a restore or role change.
How to fix it
- Run DDL as the owning role, or grant membership:
GRANT owner_role TO migration_role;. - Reassign ownership where appropriate:
ALTER TABLE orders OWNER TO app_owner;. - Standardize on a single owner role for each schema and run migrations as that role.
Diagnostic query
-- Who owns the relation?
SELECT c.relname, pg_get_userbyid(c.relowner) AS owner
FROM pg_class c WHERE c.relname = 'orders';
Related & next steps
Reference: PostgreSQL 18 — ALTER TABLE.
Thanks — noted. This helps keep the database accurate.