Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gukNY-0000YK-2q for pgsql-general@arkaria.postgresql.org; Fri, 15 Feb 2019 20:46:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gukNW-0000wa-P1 for pgsql-general@arkaria.postgresql.org; Fri, 15 Feb 2019 20:46:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gukNW-0000wT-Bl for pgsql-general@lists.postgresql.org; Fri, 15 Feb 2019 20:46:34 +0000 Received: from st43p00im-zteg10072001.me.com ([17.58.63.167]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gukNU-0005L2-Ca for pgsql-general@postgresql.org; Fri, 15 Feb 2019 20:46:33 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mac.com; s=04042017; t=1550263589; bh=hyJ740o51uUdDCciQW69xbo4dVKlB2LKNskDsKGV77Y=; h=From:Content-Type:Mime-Version:Subject:Date:To:Message-Id; b=RhQpSO8lXrS+VpO4ZekZsGeVo9vB8ppJ9Kb2HfSsCwKI6xlqQAGfQoRE335fby4X2 tSpdYGxywn+1GqK26HdNCyFaVrxhuMohSZcHIrasJdAlq/B5dyKyok4tQEOa29ZH2g NhnbBbkSOilkGaQrDKjxkE9RgA+p4TWirH/Y+cvoH6BGZXBWE9gOGpMSYUSrX4dhgo QVeB3y0T9SEppFS2dmVM9qhKgQAFrG2ZWQally69RHoRvpn4p+HousnFTY4jsnVYMq wR9Sw1G6KOGQG9uUoz9+7s4XtVB6+zVT9zrk2UJ3EPVpInfgP21puzfDkLWECjkHcu QIuOdmT4bllSQ== Received: from [17.168.82.97] (unknown [17.168.82.97]) by st43p00im-zteg10072001.me.com (Postfix) with ESMTPSA id CD3936E0116 for ; Fri, 15 Feb 2019 20:46:28 +0000 (UTC) From: Alexander Reichstadt Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.2\)) Subject: Re: Trigger function always logs postgres as user name Date: Fri, 15 Feb 2019 20:46:26 +0000 References: <7726A65A-7A13-4C1D-82C5-AE95F2F34EB7@me.com> To: pgsql-general@postgresql.org In-Reply-To: <7726A65A-7A13-4C1D-82C5-AE95F2F34EB7@me.com> Message-Id: <5A0C2766-185B-44A4-B342-6FC7AE5E059B@mac.com> X-Mailer: Apple Mail (2.3445.104.2) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-02-15_14:,, signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=1 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 mlxscore=0 mlxlogscore=516 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1812120000 definitions=main-1902150137 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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 wrote: >=20 > Hi, >=20 > I setup trigger functions for logging, and while they do work and get = triggered, the current_user always insert =E2=80=9Cpostgres=E2=80=9D = even when updates/deletes/inserts are caused by users of another name. >=20 > How do I get it to use the name that caused the update? It seems = current_user is the trigger=E2=80=99s user, so the server itself in some = way. This is on PG10 >=20 > Here the function: > BEGIN >=20 > IF TG_OP =3D 'INSERT' >=20 > THEN >=20 > INSERT INTO logging (tabname, schemaname, who, = operation, new_val) >=20 > VALUES (TG_RELNAME, TG_TABLE_SCHEMA, = current_user, TG_OP, row_to_json(NEW)); >=20 > RETURN NEW; >=20 > ELSIF TG_OP =3D 'UPDATE' >=20 > THEN >=20 > INSERT INTO logging (tabname, schemaname, who, = operation, new_val, old_val) >=20 > VALUES (TG_RELNAME, TG_TABLE_SCHEMA, = current_user, TG_OP, >=20 > row_to_json(NEW), = row_to_json(OLD)); >=20 > RETURN NEW; >=20 > ELSIF TG_OP =3D 'DELETE' >=20 > THEN >=20 > INSERT INTO logging (tabname, schemaname, = operation, who, old_val) >=20 > VALUES (TG_RELNAME, TG_TABLE_SCHEMA, = TG_OP, current_user, row_to_json(OLD)); >=20 > RETURN OLD; >=20 > END IF; >=20 > END; >=20 >=20 >=20 > Cheers, > Alex