Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 230
Maciej Avatar asked Jun 10 '26 21:06

Maciej


1 Answers

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:

  • User-defined variables in PostgreSQL
like image 59
Erwin Brandstetter Avatar answered Jun 13 '26 19:06

Erwin Brandstetter