5

My app which connects to PostgreSQL DB using same credentials (db provider requirement...) However I need to recognize what app user is connecting to DB (eg for udates auditing etc)

I wonder if there is any way I can set some variable (which life is limited to session only), which then I can somehow read via DDLs.

** EDIT **

Ive also did some experiments using application_name in conneciton string and putting there simple user login - but this is not most elegant way...

SET application_name TO 'user_login';
select current_setting('application_name');

Any idea how to archive that ?

Maciej
  • 10,423
  • 17
  • 64
  • 97

1 Answers1

11

Don't abuse application_name, there are custom variables ("customized options") for that:

SET my.app_user = 'user_login';
SELECT current_setting('my.app_user');

See:

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228