Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres REASSIGN OWNED for only 1 database

I want to use the REASSIGN OWNED query to change all objects in 1 database from owner A to owner B.

Let say I have the following databases:

 postgres              | postgres        | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0             | postgres        | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
                       |                 |          |             |             | postgres=CTc/postgres
 template1             | postgres        | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
                       |                 |          |             |             | postgres=CTc/postgres
 db1                   | user1           | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 db2                   | user1           | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 db3                   | user2           | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 

I want to db1 and all objects inside so they are owned by user2. I run:

postgres=# \c db1
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
You are now connected to database "db1" as user "postgres".
db1=# REASSIGN OWNED BY user1 TO user2;
REASSIGN OWNED

The owner changed as it should for db1 and all of its objects. But the command also changed the owner of db2. Not the objects in db2, just the database (like and ALTER DATABASE statement):

 postgres              | postgres        | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0             | postgres        | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
                       |                 |          |             |             | postgres=CTc/postgres
 template1             | postgres        | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
                       |                 |          |             |             | postgres=CTc/postgres
 db1                   | user2           | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 db2                   | user2           | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 db3                   | user2           | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 

Is this the normal behaviour? How can I run the REASSIGN OWNED without altering other databases?

like image 728
Carlos Vásquez Avatar asked Nov 23 '22 08:11

Carlos Vásquez


1 Answers

The documentation quoted in the comment by Daniel Vérité states:

old_role

The name of a role. The ownership of all the objects within the current database, and of all shared objects (databases, tablespaces), owned by this role will be reassigned to new_role.

so this is per spec. If this is not what you want, I think you need to state your use case more fully.

like image 108
alvherre Avatar answered Nov 24 '22 22:11

alvherre