Return the IP address of the client that opened the current session.
INET_CLIENT_ADDR()
## Overview Returns the IP address of the client that opened the current session, as a string. Use this function for audit logging, access tracing, or row-level predicates tied to the client origin. Returns NULL when the client connected over a local socket or when the address is otherwise unavailable. ## Session context - The value is fixed at connection establishment and does not change during the session. - Takes no arguments. - Returns NULL for local (non-IP) connections. ## Behavior - Returns a STRING containing either an IPv4 or IPv6 address. - Deterministic for the duration of the session. - Side effect free. ## Compatibility - PG-compat alias for `inet_client_addr` function.
-- Read the client IP address
SELECT INET_CLIENT_ADDR();
-- Log client IP alongside the session user
SELECT CURRENT_USER() AS user_name, INET_CLIENT_ADDR() AS client_ip;
-- Include both client and server IPs in an audit row
SELECT INET_CLIENT_ADDR() AS source_ip, INET_SERVER_ADDR() AS dest_ip;
-- Restrict rows to sessions originating from a specific subnet (admin use)
SELECT * FROM ops.audit.queries WHERE INET_CLIENT_ADDR() LIKE '10.%';