在触发函数中检索用户名 [英] Retrieve username inside a trigger function
问题描述
在我们的数据库中,我们有一个登录
表,其中包含用户名用户名
和密码
。如何在触发器函数中检索此用户名,以便也可以对其进行审核。触发器函数内的 user
关键字检索postgresql用户名,我需要的是应用程序用户名。
In our database we have a login
table featuring coloumns username
and password
. How can this username be retrieved inside a trigger function so that it can also be audited. user
keyword inside the trigger function retrieves the postgresql username, what I need is the application username. Any suggestions?
推荐答案
您可以在 postgresql.conf $ c $中设置自定义GUC。 c>。使用它在登录时使用
SET myapp_username
存储应用程序用户名。使用 current_setting(myapp_username)
You can set a custom GUC up in postgresql.conf
. Use it to store the application username at login using SET myapp_username
. Access that in triggers with current_setting(myapp_username)
访问触发器,或者创建 TEMPORARY 用户登录时输入code>表,登录时将
INSERT
用户的应用程序用户名插入表中。 SELECT
可以在需要的地方在触发器中使用。
Alternately, create a TEMPORARY
table at user login and INSERT
the user's application username into it at login. SELECT
it in triggers where required.
这两个都要求用户不能直接运行自定义SQL。 ,但这通常是应用程序级身份验证的要求,就像您一直在使用一样。
Both of these require that the user cannot run custom SQL directly, of course, but that's generally a requirement of application-level authentication like you're using anyway.
这篇关于在触发函数中检索用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!