🐥 Changing ownership of all objects in a PostgreSQL database
yellowduck.be·15h
Flag this post

312 words, 2 min read

When managing databases across environments or rotating credentials, it’s common to need to transfer ownership of all objects in a PostgreSQL database — for example, after creating a new user like app_user. Doing this manually for each table or sequence is tedious, but PostgreSQL provides a clean and efficient way to handle it.

In PostgreSQL, only the owner of an object (or a superuser) can modify or drop it. This means if a migration runs as a different role than the one that owns the tables, you’ll see errors such as:

ERROR: must be owner of table users

To fix this permanently, you can transfer ownership of all objects to the correct role.

Starting from PostgreSQL 15, the easiest way to transfer all database objects from one role to another is…

Similar Posts

Loading similar posts...