Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql trigger on user logon

Tags:

postgresql

I'm trying to figure out of a way of knowing as soon as a user logs into a Postgres database. Is there a way to define a trigger to fire when a user logs into the DB? Or is there a table or a system view that gets updated whenever anyone logs into the DB?

like image 354
alessandro ferrucci Avatar asked Jan 26 '26 04:01

alessandro ferrucci


2 Answers

login_hook

Postgres database extension to execute som code on user login, comparable to Oracle's after logon trigger.

like image 177
Jan Sommer Avatar answered Jan 28 '26 21:01

Jan Sommer


I don't think there's anything at the SQL level which can identify a logon event (i.e. no view you can query, and no trigger you can create).

You can, however, write a server-side module (in C) which intercepts logon events via the ClientAuthentication_hook. This hook is invoked after the server has attempted to authenticate a user, but before it has sent its response.

There is a worked example of a ClientAuthentication_hook on the Postgres wiki. You can also find a couple of examples of this hook in action within Postgres' own contrib modules.

like image 29
Nick Barnes Avatar answered Jan 28 '26 21:01

Nick Barnes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!