【问题标题】:How to get DB username in PL/Python function如何在 PL/Python 函数中获取数据库用户名
【发布时间】:2018-03-30 04:37:06
【问题描述】:

我有一个如下所示的 PL/Python 函数

create function pl_python (database character)
returns character varying as

$BODY$

import subprocess
import getpass
import os

   return getpass.getuser()

$BODY$
 language plpythonu volatile

我通过pgadmin III以XYZ用户身份连接到greenplum数据库来执行上述功能

Select * from pl_python ('database')

上述函数返回的是管理员用户名,而不是执行该函数的用户的用户名。我需要执行函数的用户的用户名

当前结果:gpadmin

预期结果:XYZ

提前致谢

添加 postgresql 标记作为语法可能在 greenplum 中有效

【问题讨论】:

    标签: python postgresql greenplum


    【解决方案1】:

    您应该使用Database Access Functions。请注意,您必须连接到数据库才能获取当前用户,因此函数的参数有点意义。

    create or replace function py_current_user()
        returns text
        language plpython3u
    as $$
        res = plpy.execute("select current_user")
        return res[0]["current_user"]
    $$;
    

    【讨论】:

    • 完美运行。谢谢
    猜你喜欢
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    • 2011-12-03
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多