How does postgres change table owner under a specific schema?

164    Asked by DavidEDWARDS in SQL Server , Asked on Jan 31, 2023

 I am trying to change the owner of all tables under the same schema in one command line. i.e: alter table schema_name.* owner to new owner. Is there a way to accomplish that?

Answered by David Piper

If you can query the table names in your schema, you can generate the queries to ALTER table ownership. Then you postgres change table owner under any specific schema.

For example:
select 'ALTER TABLE ' || t.tablename || ' OWNER TO new_owner;' from pg_tables t where t.tableowner != 'rdsadmin';
will return the query to change ownership of all tables:
ALTER TABLE schema_version OWNER TO ali; ALTER TABLE users OWNER TO ali; ALTER TABLE company OWNER TO ali; ALTER TABLE books OWNER TO ali; ...
then you can just run these

Your Answer

Interviews

Parent Categories