While working with databases, obtaining information about the hostname and port number is crucial. This is because a Postgres database requires a hostname and port number to establish a connection. Host names indicate the location of the database server, while port numbers indicate how we can access a database.
This write-up illustrates various methods of finding the hostname and port number in PostgreSQL.
How to Check/Find the Hostname and Port Number in PostgreSQL?
The below-listed methods will be discussed in this post to check the hostname and port number:
- Using \conninfo
- Using pg_settings
- Using “inet_server_addr()” and “inet_server_port()”
- Using “postgresql.conf” File
How to Check/Find the Hostname and Port Number Using \conninfo?
“\conninfo” is a meta-command that retrieves connection details, such as database name, user name, port number, and hostname. To utilize this command, open the psql utility, provide the login privileges, and execute the below-provided meta-command:
\conninfo
The following snippet demonstrates that we are connected to “localhost” at the default port, which is “5432”:
How to Check/Find the Port Number Using pg_settings?
Postgres provide a pre-defined view named "pg_settings" that keeps detailed information about the current configuration settings of the Postgres database server. Users can utilize this view to query the hostname and port number:
SELECT * FROM pg_settings WHERE name = 'port';
How to Check/Find the Hostname and “inet_server_addr()”?
PostgreSQL provides an inbuilt function named “inet_server_addr()” that retrieves the server’s IP address (hostname). To get the hostname using the inet_server_addr() function, query the following command:
SELECT inet_server_addr() AS hostname;
The stated function retrieves “::1”, which is equivalent to "127.0.0.1":
How to Check/Find the Port Number Using “inet_server_port()”?
In Postgres, a built-in function named “inet_server_port()” is used to get the server’s port number. For this purpose, query the following command:
SELECT inet_server_port() AS portNumber;
The given function retrieves the server's port number as "5432":
How to Check/Find the Hostname and Port Number Using “postgresql.conf” File?
The "postgresql.conf" is a configuration file that assists Postgres in managing different settings and parameters for the database server. Execute the following command to see the location where the "postgresql.conf" file is located:
show config_file;
The below snippet shows that the configuration file is located at “C:\Program Files\PostgreSQL\15\data\postgresql.conf” path:
Navigate to the stated path, open the configuration file in any text editor, and scroll down a little bit to reach the “CONNECTIONS AND AUTHENTICATION” section:
The output snippet demonstrates that we are connected to “localhost” at the default port, which is “5432”.
Conclusion
In PostgreSQL, several methods are used to check/find the hostnames and port numbers, such as the “\conninfo” command, the “pg_settings” view, the “inet_server_addr()” and “inet_server_port()” functions, and the “postgresql.conf” file. This post has demonstrated all these methods with practical examples.