In our database we have a login table featuring coloumns username and password. How can this username be retrieved inside a trigger function so that it can also be audited. user keyword inside the trigger function retrieves the postgresql username, what I need is the application username. Any suggestions?
In our database we have a login table featuring coloumns username and password .
Share
You can set a custom GUC up in
postgresql.conf. Use it to store the application username at login usingSET myapp_username. Access that in triggers withcurrent_setting(myapp_username)Alternately, create a
TEMPORARYtable at user login andINSERTthe user’s application username into it at login.SELECTit in triggers where required.Both of these require that the user cannot run custom SQL directly, of course, but that’s generally a requirement of application-level authentication like you’re using anyway.
More detail in Passing user id to PostgreSQL triggers