Incident Response

PostgreSQL ERROR: must be owner of table

A migration that worked yesterday stops on ALTER TABLE or CREATE INDEX, and GRANT ALL does not help. In PostgreSQL some operations belong to the owner alone. Here is which ones, how to find who owns what, and the owner-role setup that stops this error for good.

· 13 min read · AG Data Team

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

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:

-- 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

PostgreSQL Errors Security Migrations
Work with us

Migrations keep tripping over ownership?

We design PostgreSQL role and ownership models for teams and run the clean-up on live databases, with no surprises for the application.

Get in touch More from the blog