Search code examples
.netpostgresqlnpgsqlauditing

PostgreSQL set session variable


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 ?


Solution

  • 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: