Re: Trigger function always logs postgres as user name

From: Alexander Reichstadt <lxr(at)mac(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Trigger function always logs postgres as user name
Date: 2019-02-15 20:46:26
Message-ID: 5A0C2766-185B-44A4-B342-6FC7AE5E059B@mac.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

HI,

The answer to the question is that you need to use session_user instead of user or current_user.

Cheers,
Alex

> On 9 Feb 2019, at 10:08, Alexander Reichstadt <lxr(at)me(dot)com> wrote:
>
> Hi,
>
> I setup trigger functions for logging, and while they do work and get triggered, the current_user always insert “postgres” even when updates/deletes/inserts are caused by users of another name.
>
> How do I get it to use the name that caused the update? It seems current_user is the trigger’s user, so the server itself in some way. This is on PG10
>
> Here the function:
> BEGIN
>
> IF TG_OP = 'INSERT'
>
> THEN
>
> INSERT INTO logging (tabname, schemaname, who, operation, new_val)
>
> VALUES (TG_RELNAME, TG_TABLE_SCHEMA, current_user, TG_OP, row_to_json(NEW));
>
> RETURN NEW;
>
> ELSIF TG_OP = 'UPDATE'
>
> THEN
>
> INSERT INTO logging (tabname, schemaname, who, operation, new_val, old_val)
>
> VALUES (TG_RELNAME, TG_TABLE_SCHEMA, current_user, TG_OP,
>
> row_to_json(NEW), row_to_json(OLD));
>
> RETURN NEW;
>
> ELSIF TG_OP = 'DELETE'
>
> THEN
>
> INSERT INTO logging (tabname, schemaname, operation, who, old_val)
>
> VALUES (TG_RELNAME, TG_TABLE_SCHEMA, TG_OP, current_user, row_to_json(OLD));
>
> RETURN OLD;
>
> END IF;
>
> END;
>
>
>
> Cheers,
> Alex

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Andrew Gierth 2019-02-15 20:53:07 Re: Subquery to select max(date) value
Previous Message Nicklas Avén 2019-02-15 20:43:21 Re: Problems pushing down WHERE-clause to underlying view