Sometimes we need to get the system information in PostgreSQL. This information can be related to the user/owner, versions, client and server ports and their addresses, etc. To get this information, PostgreSQL provides many functions. This blog will particularly be covering the two functions that are used to get the client and server port numbers. These functions are inet_client_port() and inet_server_port(). Let’s discover the functionality of these functions.
How to Use inet_client_port() Function in PostgreSQL
The function inet_client_port() gives the IP port number of the present/current client. The basic syntax of this function is:
inet_client_port()
The function does not need any argument and returns the IP port number of the current/present client. The returned IP port number has an INTEGER data type.
Now we will see the implementation of this function for a better understanding of the working of the inet_client_port() function. For this you simply need to run the following query in the psql/SQL Shell:
SELECT inet_client_port();
The query returns the IP port number of the current client. The output is as follows:
We can see that the above output has returned the IP port number of the client.
So this is how this function works. We can also get the server’s IP port number. Let’s see how we can do this.
How to Use inet_server_port() Functions in PostgreSQL
The function inet_server_port() gives the IP port number of the server that accepts/receives the current connection. The basic syntax of this function is:
inet_server_port()
The function does not need any argument and returns the IP port number of the server that accepts the current connection. The returned IP port number has an INTEGER data type.
Now we will see the implementation of this function for a better understanding of the working of the inet_server_port() function. For this, you simply need to run the following query in the psql/SQL shell:
SELECT inet_server_port();
The query returns the IP port number of the server that accepts the current connection. The output is as follows:
We can see that the above output has returned the IP port number of the server.
Conclusion
The inet_client_port() and inet_server_port() functions come under the category of system information function in PostgreSQL. These functions give an Integer. The inet_client_port() function gives the IP port number of the present/current client and the inet_server_port() function returns the IP port number of the server that accepts the establishment of the connection. This article comprised the working and usage of both these functions with their implementation.