如何在 postgresql 中更改所有视图的所有者

1 sql postgresql views dynamic-sql

数据库125 意见stored.Among他们75个看法 所有者sa

那么,有没有什么方法,我可以申请改变视图的主人 sapostgres。?

Viv*_* S. 6

找出与所有者关联的视图 sa

select 
      viewname 
from 
      pg_catalog.pg_views
where
      schemaname NOT IN ('pg_catalog', 'information_schema') 
and 
      viewowner = 'sa'
Run Code Online (Sandbox Code Playgroud)

ALTER查看所有者,我们可以使用:根据文档:ALTER VIEW <view_name> OWNER TO <owner_name>

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 [, ... ] )
Run Code Online (Sandbox Code Playgroud)

最后,找出与所有者关联的视图saALTER使用以下命令

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;
$$;
Run Code Online (Sandbox Code Playgroud)