Skip to main content
0 reactions

How to Change the Owner of a Database in PostgreSQL

Card for How to Change the Owner of a Database in PostgreSQL, with the steps CREATE ROLE app_user;, ALTER DATABASE blog, OWNER TO app_user; and \l blog: app_user.
How to Change the Owner of a Database in PostgreSQL.

Every PostgreSQL database has an owner: the role that created it, unless you chose another. The owner can change the database’s settings and drop it. Here’s how to change the owner of a database in PostgreSQL, for example to hand it to the role your app connects as.

Every command and output on this page is real, from PostgreSQL 19 on macOS.

Syntax

SyntaxSQL
ALTER DATABASE name OWNER TO new_owner;
  • name: the database.
  • new_owner: the role that will own it. Roles are PostgreSQL’s users and groups of users.
  • You must be able to act as the new owner (SET ROLE) and have the CREATEDB privilege. Superusers, such as postgres, always can.

This is one of the forms of ALTER DATABASE (opens in a new tab) in the PostgreSQL documentation.

Step 1: Create the Role and the Database

Connect to your server with psql as the postgres user, as Part 2 of the PostgreSQL Tutorial shows. psql connects you to the postgres database, and its prompt is postgres=#.

This guide hands a database called blog to a role called app_user. To follow along, create both. CREATE ROLE makes a role; this one can’t log in, which is fine for an owner in an example:

postgres=#SQL
CREATE ROLE app_user;
OutputPlain text
CREATE ROLE
postgres=#SQL
CREATE DATABASE blog;
OutputPlain text
CREATE DATABASE

Step 2: Change the Owner

postgres=#SQL
ALTER DATABASE blog OWNER TO app_user;
OutputPlain text
ALTER DATABASE

Step 3: See the New Owner

psql’s \l with a name lists just that database, with its owner:

postgres=#Plain text
\l blog
OutputPlain text
                                            List of databases
 Name |  Owner   | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges
------+----------+----------+-----------------+---------+-------+--------+-----------+-------------------
 blog | app_user | UTF8     | libc            | C       | C     |        |           |
(1 row)

The Owner column shows app_user.

Clean Up

Drop the example databases before the role, because a role that still owns a database can’t be dropped. Skip wiki if you didn’t create it in the FAQ below:

postgres=#SQL
DROP DATABASE blog;
OutputPlain text
DROP DATABASE
postgres=#SQL
DROP DATABASE wiki;
OutputPlain text
DROP DATABASE
postgres=#SQL
DROP ROLE app_user;
OutputPlain text
DROP ROLE

Common Mistakes

MistakeWhat happensDo this instead
A misspelled role: OWNER TO app_usrERROR: role "app_usr" does not existCheck the spelling, or create the role first with CREATE ROLE.
Dropping a role that still owns a databaseERROR: role "app_user" cannot be dropped because some objects depend on it, with DETAIL: owner of database blogDrop the database first, or give it to another owner.

FAQ

Can I set the owner when I create the database?

Yes. Add OWNER to CREATE DATABASE:

postgres=#SQL
CREATE DATABASE wiki OWNER app_user;
OutputPlain text
CREATE DATABASE

Who owns a database I create?

You do: the role you’re connected as becomes the owner, unless you add OWNER.

Conclusion

Run ALTER DATABASE name OWNER TO new_owner; as a superuser or as a role that can act as the new owner, and check the result with \l name.

Additional Resources

Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

0 comments