Change table owner in PostgreSQL

In this article, we will see how to change owner of table in PostgreSQL. To change owner of the table, you must be owner of the table or must have alter table or superuser permissions.

It is database administrator routine task to change table owner in PostgreSQL. This can be done with ALTER TABLE statement.

Syntax to Change table owner in PostgreSQL:

ALTER TABLE public.table_name OWNER TO another_username;

Verify the owner of the table by running below command:

\dt table_name;

Example:

lonprod1=# \dt customer List of relations Schema | Name | Type | Owner --------+----------+-------+---------- public | customer | table | postgres (1 row)

Examples to Change table owner in PostgreSQL: In this example, we are changing the owner of customer table to new owner ‘johnd’.

ALTER TABLE public.customer OWNER TO johnd;

Verify the owner after above statement:

lonprod1=# \dt customer List of relations Schema | Name | Type | Owner --------+----------+-------+------- public | customer | table | johnd (1 row)

Before owner of table ‘customer’ is postgres, now its changed to johnd.