inet_server_port() is a system function which returns the IP port which the current client is connected to.
inet_server_port() was added in PostgreSQL 8.0.
inet_server_port () →
inet_server_port() is called without any arguments, and returns the client IP address as an
NULL if the current connection is via a Unix-domain socket.
In PostgreSQL 13 and earlier,
inet_server_port() will always return
NULL if executed by a parallel worker. This has been fixed for PostgreSQL 14 (commit 5a6f9bce), but has not been backpatched to earlier versions.
Example output when connected via TCP/IP:
postgres=# SELECT inet_server_port(); inet_server_port ------------------ 5433 (1 row)
Example output when connected via a Unix-domain socket:
postgres=# SELECT inet_server_port(); inet_server_port ------------------ (1 row)