ERROR: must be owner of table ... means you tried an operation that only the table's owner (or a superuser) may run, and your role is not the owner. It usually appears in migrations: the table was created by one role, and now another role, often a separate deploy user, tries to change it. Granting more privileges does not fix it, because ownership is not a privilege you can grant.
migrator=> ALTER TABLE orders ADD COLUMN note text;
ERROR: must be owner of table orders
migrator=> CREATE INDEX orders_created_idx ON orders (created_at);
ERROR: must be owner of table orders
The SQLSTATE is 42501, insufficient_privilege in the error codes table. The same family of messages exists for other object types: must be owner of view, sequence, schema, function, database and so on, all defined in aclchk.c.
Why GRANT ALL is not enough
The privileges chapter is clear: the right to modify or destroy an object is inherent in being its owner and cannot be granted or revoked in itself. GRANT ALL ON orders TO migrator gives SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER and, from PostgreSQL 17, MAINTAIN. It does not give the right to ALTER or DROP the table.
There is one way to share ownership rights: role membership. The same chapter notes that, like all privileges, the owner's rights can be inherited by members of the owning role. That is the basis of the fix below.
What needs ownership and what does not
- Owner only:
ALTER TABLEin all its forms ("You must own the table to use ALTER TABLE"),DROP TABLE,CREATE INDEXon the table, and for most objectsCOMMENT ON, according to the COMMENT documentation. - A normal privilege is enough:
TRUNCATEneeds the TRUNCATE privilege, not ownership. Reading and writing data need the usualSELECT,INSERTand so on. If those are missing, you get permission denied for table instead, covered in our separate article. - Maintenance, PostgreSQL 17 and later: the PostgreSQL 17 release added the
MAINTAINprivilege and the pg_maintain role. They allowVACUUM,ANALYZE,REINDEX,CLUSTER,REFRESH MATERIALIZED VIEWandLOCK TABLEwithout owning the table. On older versionsVACUUM,ANALYZE,REINDEX,CLUSTERandREFRESH MATERIALIZED VIEWneed ownership (database owners may also vacuum their databases), whileLOCK TABLEhas always worked with ordinary table privileges. Note thatVACUUMdoes not fail on such tables: it skips the tables you may not vacuum.
Find out who owns what
Most of the time the answer is a surprise: postgres, because someone created a table by hand, or a developer's personal login. Check everything at once:
-- who owns what in a schema (tables, views, sequences, ...)
SELECT c.relname, c.relkind, pg_get_userbyid(c.relowner) AS owner
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = 'public'
AND c.relkind IN ('r', 'p', 'v', 'm', 'S', 'f')
ORDER BY owner, c.relname;
-- in psql: \dt+ orders shows the owner too
If you see several owners in one application schema, the error will come back with the next migration that touches a table owned by someone else.
The fix: one owner role, used by migrations
The pattern that works for teams is a dedicated owner role nobody logs in as. All application objects belong to it. The migration user and DBAs are members:
-- one owner role that nobody logs in as
CREATE ROLE app_owner NOLOGIN;
-- the migration user and DBAs become members
GRANT app_owner TO migrator; -- PostgreSQL 16+: INHERIT and SET options apply
-- hand existing objects to the owner role (run as the current owner or a superuser)
ALTER TABLE public.orders OWNER TO app_owner; -- its serial/identity sequence follows
ALTER SEQUENCE public.invoice_no_seq OWNER TO app_owner; -- standalone sequences only
The rules for ALTER ... OWNER TO come from the ALTER TABLE documentation. You must own the table, or inherit the owning role's privileges. You must be able to SET ROLE to the new owner, and the new owner needs CREATE on the table's schema. A superuser can change any owner. The old owner's privileges on the object pass to the new owner.
Then make sure new objects get the right owner from the start. An object belongs to whoever creates it, so migrations should switch role first:
-- at the start of every migration
SET ROLE app_owner;
CREATE TABLE invoices (...); -- owned by app_owner, not by migrator
RESET ROLE;
Since PostgreSQL 16, membership has separate options. INHERIT lets the member use the owner's rights directly, and SET allows SET ROLE, as described in role membership. If a grant was made WITH INHERIT FALSE, the member must run SET ROLE app_owner before ALTER TABLE works. If it was made WITH SET FALSE, SET ROLE is refused, and so is ALTER ... OWNER TO that role.
Do not make the app a superuser to get past this. It fixes the error and removes every safety net at once. A NOLOGIN owner role, with the runtime application account holding only data privileges, keeps migrations working without handing the application the keys to the cluster.
Default privileges: the other half
When app_owner creates a table, other roles get no rights on it until someone grants them. ALTER DEFAULT PRIVILEGES sets the privileges for objects created in the future; it does not touch existing ones. Run it for the role that actually creates the objects (FOR ROLE app_owner); default privileges set for a different role do nothing here. This is a common reason why a new table works for the migrator and fails for the application with permission denied.
On large production databases
On a test database with five tables, changing owners is one command. On a production database with thousands of tables, partitions and sequences, plan for these:
- ALTER ... OWNER TO takes an ACCESS EXCLUSIVE lock. The ALTER TABLE documentation says this lock is used unless a form is listed as needing less, and
OWNER TOis not. The change itself is quick, but it waits behind long queries and blocks everything queued behind it. Set a shortlock_timeoutand retry. - Generate statements, run them in batches. One transaction for thousands of objects holds thousands of locks until commit. In our experience, batches of a few hundred objects with commits in between are much easier on a busy server.
- REASSIGN OWNED is a blunt tool.
REASSIGN OWNED BY old TO newmoves everything the role owns in the current database, which may be more than one application. Our article on dropping objects and roles explains how it works withDROP OWNED. - Do not forget sequences and views. A sequence linked to a column (
serialor identity) changes owner together with its table;ALTER SEQUENCE ... OWNER TOon it fails with cannot change owner of sequence, Sequence ... is linked to table .... Standalone sequences and views need their own statements, which the generator below produces. - Check after each release. Add the owner query to your deployment checks and alert when a new object appears with an unexpected owner.
-- generate ALTER ... OWNER statements for everything not yet owned by app_owner
SELECT format('ALTER %s %I.%I OWNER TO app_owner;',
CASE c.relkind WHEN 'v' THEN 'VIEW'
WHEN 'm' THEN 'MATERIALIZED VIEW'
WHEN 'S' THEN 'SEQUENCE'
WHEN 'f' THEN 'FOREIGN TABLE'
ELSE 'TABLE' END,
n.nspname, c.relname)
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = 'public'
AND c.relkind IN ('r', 'p', 'v', 'm', 'S', 'f')
AND pg_get_userbyid(c.relowner) <> 'app_owner'
-- skip sequences linked to a column: they follow their table
AND NOT EXISTS (SELECT 1 FROM pg_depend d
WHERE d.classid = 'pg_class'::regclass AND d.objid = c.oid
AND d.refclassid = 'pg_class'::regclass
AND c.relkind = 'S' AND d.deptype IN ('a', 'i'));
-- PostgreSQL 17+: maintenance without ownership
GRANT MAINTAIN ON TABLE public.orders TO ops_job; -- one table
GRANT pg_maintain TO ops_job; -- all relations
Key takeaways
- "must be owner of table" means the command needs ownership, which no GRANT can give.
ALTER TABLE,DROPandCREATE INDEXneed the owner;TRUNCATEand data access need ordinary privileges.- Use a NOLOGIN owner role, make migrators members, and
SET ROLEto it in migrations. - PostgreSQL 16 split membership into INHERIT and SET options; PostgreSQL 17 added
MAINTAINandpg_maintainfor maintenance without ownership. - Change owners in batches with a short
lock_timeout, and check owners after each release.