login_hook
login_hook
login_hook : login_hook - hook to execute login_hook.login() at login time
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 7360 | login_hook | login_hook | 1.7 | SEC | GPL-3.0 | C |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--s-d-- | No | Yes | No | Yes | no | no |
| Relationships | |
|---|---|
| Schemas | login_hook |
| See Also | pg_auth_mon credcheck set_user pg_permissions passwordcheck_cracklib pgaudit auth_delay passwordcheck |
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | MIXED | 1.7 | 18 17 16 15 14 | login_hook | - |
| RPM | PGDG | 1.7 | 18 17 16 15 14 | login_hook_$v | - |
| DEB | PIGSTY | 1.7 | 18 17 16 15 14 | postgresql-$v-login-hook | - |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64 | PIGSTY 1.7 | PGDG 1.7 | PGDG 1.7 | PGDG 1.7 | PGDG 1.7 |
el8.aarch64 | PIGSTY 1.7 | PGDG 1.7 | PGDG 1.7 | PGDG 1.7 | PGDG 1.7 |
el9.x86_64 | PIGSTY 1.7 | PGDG 1.7 | PGDG 1.7 | PGDG 1.7 | PGDG 1.7 |
el9.aarch64 | PIGSTY 1.7 | PGDG 1.7 | PGDG 1.7 | PGDG 1.7 | PGDG 1.7 |
el10.x86_64 | PIGSTY 1.7 | PGDG 1.7 | PGDG 1.7 | PGDG 1.7 | PGDG 1.7 |
el10.aarch64 | PIGSTY 1.7 | PGDG 1.7 | PGDG 1.7 | PGDG 1.7 | PGDG 1.7 |
d12.x86_64 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 |
d12.aarch64 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 |
d13.x86_64 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 |
d13.aarch64 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 |
u22.x86_64 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 |
u22.aarch64 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 |
u24.x86_64 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 |
u24.aarch64 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 | PIGSTY 1.7 |
Source
pig build pkg login_hook; # build rpm/debInstall
Make sure PGDG and PIGSTY repo available:
pig repo add pgsql -u # add both repo and update cacheInstall this extension with pig:
pig install login_hook; # install via package name, for the active PG version
pig install login_hook -v 18; # install for PG 18
pig install login_hook -v 17; # install for PG 17
pig install login_hook -v 16; # install for PG 16
pig install login_hook -v 15; # install for PG 15
pig install login_hook -v 14; # install for PG 14Create this extension with:
CREATE EXTENSION login_hook;Usage
login_hook: Execute code on user login, comparable to Oracle’s after logon trigger
login_hook allows executing custom PL/pgSQL code whenever a user logs into the database.
CREATE EXTENSION login_hook;Configuration
Add to postgresql.conf:
session_preload_libraries = 'login_hook'Creating the Login Function
Define a login_hook.login() function that will execute on every login:
CREATE OR REPLACE FUNCTION login_hook.login() RETURNS VOID LANGUAGE PLPGSQL AS $$
BEGIN
IF NOT login_hook.is_executing_login_hook() THEN
RAISE EXCEPTION 'Should only be invoked by login_hook';
END IF;
-- Your login logic here:
RAISE NOTICE 'Hello %', current_user;
EXCEPTION
WHEN OTHERS THEN
RAISE LOG 'Error in login_hook.login(): %', SQLERRM;
END
$$;
GRANT EXECUTE ON FUNCTION login_hook.login() TO PUBLIC;The PUBLIC grant is required because the function runs for every connecting user.
Functions
| Function | Returns | Description |
|---|---|---|
login_hook.is_executing_login_hook() | boolean | Returns true only when called during login hook execution |
login_hook.get_login_hook_version() | text | Returns compiled version of login_hook |
login_hook.login() | void | User-provided function executed at login |
Important Notes
- The function is NOT invoked for background processes or during recovery mode
- Handle all exceptions within the function – failures will prevent normal users from logging in
- Superusers get a warning but can still log in when the function fails
- For PostgreSQL 17+, consider using the native login event trigger instead
Last updated on