pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alexander Reichstadt <lxr@me.com>
To: pgsql-general@postgresql.org
Subject: Trigger function always logs postgres as user name
Date: Sat, 9 Feb 2019 10:08:53 +0000
Message-ID: <7726A65A-7A13-4C1D-82C5-AE95F2F34EB7@me.com> (raw)

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



view thread (3+ messages)  latest in thread

Message-ID: <7726A65A-7A13-4C1D-82C5-AE95F2F34EB7@me.com>
Permalink:  ../7726A65A-7A13-4C1D-82C5-AE95F2F34EB7@me.com/
Also on:    postgresql.org/message-id/7726A65A-7A13-4C1D-82C5-AE95F2F34EB7@me.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: lxr@me.com
  Subject: Re: Trigger function always logs postgres as user name
  In-Reply-To: <7726A65A-7A13-4C1D-82C5-AE95F2F34EB7@me.com>
  References: <7726A65A-7A13-4C1D-82C5-AE95F2F34EB7@me.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox