get user info on log

Started by Marcos Pegoraroover 3 years ago6 messagesgeneral
Jump to latest
#1Marcos Pegoraro
marcos@f10.com.br

When using set role or set session authorization and an error occurs I get
user name which logged in and not the one I´ve changed to.

There is a way to get session_user and current_user on log ?

Thanks
Marcos

#2Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Marcos Pegoraro (#1)
Re: get user info on log

On 9/13/22 12:45, Marcos Pegoraro wrote:

When using set role or set session authorization and an error occurs I
get user name which logged in and not the one I´ve changed to.

There is a way to get session_user and current_user on log ?

When I do:

test(5432)=# set role maura;
ERROR: role "maura" does not exist
test(5432)=# SET SESSION AUTHORIZATION 'maura';
ERROR: role "maura" does not exist

I get:

psql-postgres-2022-09-15 08:53:41.204 PDT-0ERROR: role "maura" does not
exist
psql-postgres-2022-09-15 08:53:41.204 PDT-0STATEMENT: set role maura;
psql-postgres-2022-09-15 08:55:26.325 PDT-0ERROR: role "maura" does not
exist
psql-postgres-2022-09-15 08:55:26.325 PDT-0STATEMENT: SET SESSION
AUTHORIZATION 'maura';

in the logs.

Are you not seeing that or do you want something different?

If you want something different then you will need to provide a more
detailed description of what that is.

Thanks
Marcos

--
Adrian Klaver
adrian.klaver@aklaver.com

#3Marcos Pegoraro
marcos@f10.com.br
In reply to: Adrian Klaver (#2)
Re: get user info on log

Em qui., 15 de set. de 2022 às 12:59, Adrian Klaver <
adrian.klaver@aklaver.com> escreveu:

test(5432)=# set role maura;
ERROR: role "maura" does not exist
test(5432)=# SET SESSION AUTHORIZATION 'maura';
ERROR: role "maura" does not exist

No, I was asking about an error occurring later, not on set session

authorization command.

set role maura;
--user Maura exists and set was done correctly
--now I´m working as Maura
select 1/0; -- I would like to see this exception on log being logged as
maura

thanks
Marcos

#4Ganesh Korde
ganeshakorde@gmail.com
In reply to: Marcos Pegoraro (#3)
Re: get user info on log

Hi,

You can use log_line_prefix in postgresql.conf to log the user name into
the logs.

Regards,
Ganesh Korde.

On Fri, 16 Sep 2022, 6:31 pm Marcos Pegoraro, <marcos@f10.com.br> wrote:

Show quoted text

Em qui., 15 de set. de 2022 às 12:59, Adrian Klaver <
adrian.klaver@aklaver.com> escreveu:

test(5432)=# set role maura;
ERROR: role "maura" does not exist
test(5432)=# SET SESSION AUTHORIZATION 'maura';
ERROR: role "maura" does not exist

No, I was asking about an error occurring later, not on set session

authorization command.

set role maura;
--user Maura exists and set was done correctly
--now I´m working as Maura
select 1/0; -- I would like to see this exception on log being logged as
maura

thanks
Marcos

#5Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Ganesh Korde (#4)
Re: get user info on log

On 9/16/22 19:22, Ganesh Korde wrote:

Hi,

  You can use log_line_prefix in postgresql.conf to log the user name
into the logs.

That uses the session user, the user that established the connection.
What the OP is after is the role name that is set by SET ROLE or SET
SESSION AUTHORIZATION.

To illustrate:

psql -d test -U postgres

test(5432)=# select session_user, current_user;
session_user | current_user
--------------+--------------
postgres | postgres

test(5432)=# set role aklaver;
SET
test(5432)=> select session_user, current_user;
session_user | current_user
--------------+--------------
postgres | aklaver
(1 row)

test(5432)=> select 1/0;
ERROR: division by zero

From log with log_line_prefix of log_line_prefix = '%a-%u-%m-%x'
:

psql-postgres-2022-09-17 14:36:06.635 PDT-0ERROR: division by zero
psql-postgres-2022-09-17 14:36:06.635 PDT-0STATEMENT: select 1/0;

Ganesh is looking for psql-<current_user> not psql-<session_user>, in
this case psql-aklaver.

Regards,
Ganesh Korde.

On Fri, 16 Sep 2022, 6:31 pm Marcos Pegoraro, <marcos@f10.com.br
<mailto:marcos@f10.com.br>> wrote:

Em qui., 15 de set. de 2022 às 12:59, Adrian Klaver
<adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>> escreveu:

test(5432)=# set role maura;
ERROR:  role "maura" does not exist
test(5432)=# SET SESSION AUTHORIZATION 'maura';
ERROR:  role "maura" does not exist

No, I was asking about an error occurring later, not on set session
authorization command.

set role maura;
--user Maura exists and set was done correctly
--now I´m working as Maura
select 1/0; -- I would like to see this exception on log being
logged as maura

thanks
Marcos

--
Adrian Klaver
adrian.klaver@aklaver.com

#6Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Marcos Pegoraro (#3)
Re: get user info on log

On 9/16/22 06:00, Marcos Pegoraro wrote:

Em qui., 15 de set. de 2022 às 12:59, Adrian Klaver
<adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>> escreveu:

test(5432)=# set role maura;
ERROR:  role "maura" does not exist
test(5432)=# SET SESSION AUTHORIZATION 'maura';
ERROR:  role "maura" does not exist

No, I was asking about an error occurring later, not on set session
authorization command.

set role maura;
--user Maura exists and set was done correctly
--now I´m working as Maura
select 1/0; -- I would like to see this exception on log being logged as
maura

I don't know of a way to get this.

thanks
Marcos

--
Adrian Klaver
adrian.klaver@aklaver.com