Updated on 2024-08-20 GMT+08:00

REASSIGN OWNED

Description

Changes the owner of the database object.

REASSIGN OWNED changes the database object owner of an old role to a new role.

Precautions

  • REASSIGN OWNED is often executed before role deletion.
  • To run the REASSIGN OWNED statement, you must have the permissions of the original and target roles.

Syntax

REASSIGN OWNED BY old_role [, ...] TO new_role;

Parameters

  • old_role

    Specifies the role name of the old owner.

  • new_role

    Specifies the role name of the new owner. Note: Only the initial user can use the REASSIGN OWNED syntax to change the owner to the initial user.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
-- Create the test_jim and test_tom users.
gaussdb=# CREATE USER test_jim PASSWORD '********';
gaussdb=# CREATE USER test_tom PASSWORD '********';

-- View the user with the same name as owner of the automatically created schema.
gaussdb=# \dn test*
   List of schemas
   Name   |  Owner   
----------+----------
 test_jim | test_jim
 test_tom | test_tom
(2 rows)

-- Change the owner of all database objects owned by test_jim to test_tom.
gaussdb=# REASSIGN OWNED BY test_jim TO test_tom;

-- View the schema information. The owner of the test_jim schema is changed to test_tom.
gaussdb=# \dn test*
   List of schemas
   Name   |  Owner   
----------+----------
 test_jim | test_tom
 test_tom | test_tom
(2 rows)

-- Delete the test_jim and test_tom users.
gaussdb=# DROP USER test_jim, test_tom CASCADE;

Helpful Links

DROP OWNED