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
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 theCREATEDBprivilege. Superusers, such aspostgres, 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:
CREATE ROLE app_user;
CREATE ROLE
CREATE DATABASE blog;
CREATE DATABASE
Step 2: Change the Owner
ALTER DATABASE blog OWNER TO app_user;
ALTER DATABASE
Step 3: See the New Owner
psql’s \l with a name lists just that database, with its owner:
\l blog
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:
DROP DATABASE blog;
DROP DATABASE
DROP DATABASE wiki;
DROP DATABASE
DROP ROLE app_user;
DROP ROLE
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
A misspelled role: OWNER TO app_usr | ERROR: role "app_usr" does not exist | Check the spelling, or create the role first with CREATE ROLE. |
| Dropping a role that still owns a database | ERROR: role "app_user" cannot be dropped because some objects depend on it, with DETAIL: owner of database blog | Drop 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:
CREATE DATABASE wiki OWNER app_user;
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
- PostgreSQL 19 documentation: ALTER DATABASE (opens in a new tab)
- PostgreSQL 19 documentation: CREATE ROLE (opens in a new tab)
- PostgreSQL 19 documentation: Creating a Database (opens in a new tab)
Related
- PostgreSQL Tutorial — Part 4: Create a Database
- How to Rename a Database in PostgreSQL
- How to Drop a Database in PostgreSQL
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