如何在Postgres中更改函数的所有者 [英] How to alter owner of a function in postgres
问题描述
我正在编写一个脚本来更改postgres的所有功能(更改每个功能的所有者)。我可以使用postgres查询列出所有函数名称,但不能列出每个函数的参数。
I am writing a script to alter all functions of postgres(changing owner of each function). I am able to list down all the function names using postgres query but not able to list parameters for each of those functions.
如果我找到解决方案,我的问题将得到解决。以下提到的任何问题:
My problem will be resolved if I get solution for any of the below mentioned problems:
- 有没有办法列出每个函数中参数的数据类型。
- 我们有什么方法可以更改功能,而不是传递参数类型,我可以发送一些通配符。
例如,我可以将
ALTER FUNCTION schemaname.func(*)OWNER写入'newowner'来作为
ALTER FUNCTION schemaname.func(*)OWNER到'newowner'。
推荐答案
有什么方法可以列出
是,请使用 pg_get_function_identity_arguments()
函数:
下面的代码将创建一个SQL脚本,以更改 someschema
模式:
The following will create a SQL script to alter all functions from the someschema
schema:
select 'alter function '||nsp.nspname||'.'||p.proname||'('||pg_get_function_identity_arguments(p.oid)||') owner to newowner;'
from pg_proc p
join pg_namespace nsp ON p.pronamespace = nsp.oid
where nsp.nspname = 'someschema';
您可以将其输出假脱机到文件中,然后运行该生成的脚本。
You can spool the output of that into a file and then run that generated script.
如果您有需要引用的函数名,则可能需要使用 quote_ident
来串联函数名。
If you have function names that would require quoting, you probably need to use quote_ident
to concatenate the function names.
您可以将所有内容包装到一个函数中,并在需要定期使用动态SQL时使生活更轻松。
You can wrap all that into a function and use dynamic SQL to make life easier if you need this on a regular basis.
这篇关于如何在Postgres中更改函数的所有者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!