简体   繁体   中英

how to alter all view's owner in postgresql

In database there are 125 views stored.Among them 75 views owner is sa .

So,is there any method can I apply to alter the view's having owner sa to postgres .?

To find out the view associated with the owner sa

select 
      viewname 
from 
      pg_catalog.pg_views
where
      schemaname NOT IN ('pg_catalog', 'information_schema') 
and 
      viewowner = 'sa'

To ALTER view's owner we can use : ALTER VIEW <view_name> OWNER TO <owner_name>
As per documentation:

ALTER VIEW [ IF EXISTS ] name ALTER [ COLUMN ] column_name SET DEFAULT expression
ALTER VIEW [ IF EXISTS ] name ALTER [ COLUMN ] column_name DROP DEFAULT
ALTER VIEW [ IF EXISTS ] name OWNER TO new_owner
ALTER VIEW [ IF EXISTS ] name RENAME TO new_name
ALTER VIEW [ IF EXISTS ] name SET SCHEMA new_schema
ALTER VIEW [ IF EXISTS ] name SET ( view_option_name [= view_option_value] [, ... ] )
ALTER VIEW [ IF EXISTS ] name RESET ( view_option_name [, ... ] )

finally, to find out the view associated with the owner sa and ALTER use the following

do $$
declare
    myrow record;
begin
for myrow in
select 
     'ALTER VIEW '||quote_ident(v.viewname)||' OWNER TO "postgres";' as viewq
from 
    (select 
      viewname 
from 
      pg_catalog.pg_views
where
      schemaname NOT IN ('pg_catalog', 'information_schema') 
and 
      viewowner = 'sa'
    ) v
loop
execute myrow.viewq;
end loop;
end;
$$;

To change all the objects owned by sa, you can use Reassign Owned .

To change individual views, you can use Alter View .

You should be able to script the procedure in the second link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM