简体   繁体   English

postgresql 触发器或用户创建存储功能

[英]postgresql trigger or functions for user creation storation

I am not able to find user creation, password changed, userid updated or last login details in postgresql and for the same I want to create trigger or functions so that it will store database user creation time and when a database user was logged in, along with the password updated time.我无法在 postgresql 中找到用户创建、密码更改、用户 ID 更新或上次登录详细信息,同样我想创建触发器或函数,以便它将存储数据库用户创建时间和数据库用户何时登录,以及密码更新时间。 I want to implement this at database level and not on table level.我想在数据库级别而不是在表级别实现这一点。

I am using below function:我在下面使用 function:

CREATE FUNCTION public.trigger_update_created_and_modified_date()
RETURNS trigger
LANGUAGE 'plpgsql'
COST 100
VOLATILE NOT LEAKPROOF
AS $BODY$

BEGIN
IF (TG_OP = 'INSERT') THEN
EXECUTE format(E'update "%s" set date_created = \'%s\', date_modified = \'%s\'
WHERE id = %s', TG_TABLE_NAME,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,NEW.id);
ELSEIF (TG_OP = 'UPDATE') THEN
EXECUTE format(E'update "%s" set date_modified = \'%s\'
WHERE id = %s', TG_TABLE_NAME,CURRENT_TIMESTAMP,NEW.id);
END IF;
RETURN NEW;
END

$BODY$;

Is there any other way?还有其他方法吗?

DML triggers only fire on INSERT , UPDATE or DELETE statements, so they cannot be used. DML 触发器仅在INSERTUPDATEDELETE语句上触发,因此它们不能被使用。

But there are event triggers that fire on CREATE , ALTER or DROP statements.但是有些事件触发器会CREATEALTERDROP语句上触发。 Unfortunately for you, the event trigger firing matrix shows that you cannot have event triggers on CREATE/ALTER/DROP ROLE , so there is no way to get what you want.对您来说不幸的是, 事件触发器触发矩阵显示您不能在CREATE/ALTER/DROP ROLE上拥有事件触发器,因此无法获得您想要的。

What you can get, and what I recommend you do, is to use the log file.你能得到的,我建议你做的,是使用日志文件。 Set

log_statement = 'ddl'
log_connections = on

and you will get these events logged.您将记录这些事件。

You can use the csvlog format if you want to parse and digest these log files in an automated fashion.如果您想以自动方式解析和消化这些日志文件,可以使用csvlog格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM