赞
踩
注意这里指的是每个数据库对应一个用户,具体操作方法请参阅再谈PostgreSQL创建数据库.
在数据库用户创建完成后执行下面的命令获取数据库用户oid并记录.
注意获取数据库用户oid只能使用postgres用户登录数据库.
psql -h host -U postgres;
select oid from pg_authid where rolname='core';
```# 2 查看数据库用户创建的函数、过程、触发器函数```bash
psql -h host -U postgres;
psql -h host -U 数据库用户 -d 数据库;
select * from pg_proc where proowner=187178
psql -h host -U 数据库用户 -d 数据库;
select * from pg_tables where tableowner='数据库用户'
do $$ declare v_owner oid; v_rec record; v_rec1 record; v_argtypes text; begin v_owner := 16385; --用postgres用户查询获取 select oid from pg_authid where rolname='数据库用户名'; for v_rec in select oid,proname,pronargs,proargtypes,prorettype from pg_proc as t1 where proowner=v_owner loop v_argtypes := ''; if( 2279 = v_rec.prorettype ) then --触发器函数 for v_rec1 in select t2.relname, t1.tgname from pg_trigger as t1 inner join pg_class as t2 on t2.oid = t1.tgrelid where tgfoid=v_rec.oid loop execute format('drop trigger if exists %s on %s;',v_rec1.tgname, v_rec1.relname); execute format('drop function if exists %s();',v_rec.proname); --raise notice '%', format('drop trigger if exists %s on %s;',v_rec1.tgname, v_rec1.relname); --raise notice '%', format('drop function if exists %s();',v_rec.proname); end loop; else if( v_rec.pronargs > 0 ) then --有参数的函数 for i in 1..array_length(v_rec.proargtypes,1) loop v_argtypes := format('%s%s,',v_argtypes,(select typname from pg_type where oid=v_rec.proargtypes[i-1])); end loop; v_argtypes := regexp_replace(v_argtypes,',$',''); execute format('drop function if exists %s(%s);',v_rec.proname,v_argtypes); --raise notice '%', format('drop function if exists %s(%s);%s',v_rec.proname,v_argtypes,v_rec.proargtypes); else execute format('drop function if exists %s();',v_rec.proname); --raise notice '%', format('drop function if exists %s();',v_rec.proname); end if; end if; end loop; end; $$;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。