How to get hostname in postgresql?
Is there any way where I can get hostname/servername in postgresql like @@ServerName in SQL Server?
4 Answers 4
You need to execute following query to get the hostname:
PostgreSQL doesn’t provide a builtin function returning the server’s hostname.
A pg_gethostname() function written in C (a wrapper to POSIX’s gethostname() ) has been contributed on the wiki and made available as an extension on PGXN:
If you can’t install that on the server, or an equivalent in another language supporting calls to the system such as plperlu or plpythonu, you’re probably going to be limited to inet_server_addr().
I use such a solution. No plugins required. Temporary tables are not needed. Only for unix.
![]()
I’ve solve this issue after creating a temp table and filling it with the result of copy from
And I can use the result like this, to check, for example, if I’m running on a production server.
After reading the comments, the answer is right if you’re executing on Pg server. That’s the case for me. Of course, the hostname result is where you’re executing it. Maybe there’s a way with HOST variable value using it in psql.
Find the host name and port using PSQL commands
I have PSQL running, and am trying to get a perl application connecting to the database. Is there a command to find the current port and host that the database is running on?
![]()
20 Answers 20
![]()
This command will give you postgres port number
If Postgres is running on a Linux server, you can also use the following command
OR (if it comes as postmaster)
and you will see something similar as this
In this case, the port number is 5432 which is also the default port number
The default PostgreSQL port is 5432 . The host that the database is operating on should have been provided by your hosting provider; I’d guess it would be the same host as the web server if one wasn’t specified. Typically this would be configured as localhost, assuming your web server and database server are on the same host.
![]()
select inet_server_addr(); gives you the ip address of the server.
This is non-sql method. Instructions are given on the image itself. Select the server that you want to find the info about and then follow the steps.

select inet_server_port(); gives you the port of the server.
![]()
From the terminal you can simply do a "postgres list clusters":
It will return Postgres version number, cluster names, ports, status, owner, and the location of your data directories and log file.
![]()
The postgresql port is defined in your postgresql.conf file.
For me in Ubuntu 14.04 it is: /etc/postgresql/9.3/main/postgresql.conf
Inside there is a line:
Changing the number there requires restart of postgresql for it to take effect.
![]()
From the terminal you can do:
I would suggest reading a documentation on their exhaustive list of all commands using:
![]()
You can use the command in psql \conninfo you will get You are connected to database «your_database» as user «user_name» on host «host_name» at port «port_number».
![]()
To find the port number you can run this command (assuming you are on localhost)
![]()
This uses psql’s built in HOST variable, documented here
And postgres System Information Functions, documented here
returns: 10/main (port 5432): online
I’m running Ubuntu 18.04
![]()
An addition to the @a_horse_with_no_name answer. To get the hostname:
go to the «Terminal» and just type
In the results you can get the port details
In my case it’s running on port «5432» (default).
I’m using CentOS 7.Hope this helps.
![]()
Because you said you (yourself) have postgresql running, I’ll assume:
- you’re on Linux,
- have at least one account with superuser privileges and/or can access the postgres role, and
- (just for fun) you need to access both values within a single transaction from within the database itself
will give you both, executing the $pgm$-delimited code as a shell script and returning the values to the server-side COPY API’s stdin. Unfortunately, this method needs a table target to invoke the server-side shell.
PostgreSQL : Get my database server name
I was looking for a build-in function to get the hostname of the server hosting my PostgreSQL cluster.
But seems that there is no build-in function. Looking in the extensions, I find the extension hostname which can allow to get the database server host name.
In this this blog I am explaining how to install and how to use it. The installation is very easy. The first step is to download it here .
After let’s go to the directory where the archive was decompressed and let’s run the command make
Как узнать имя сервера postgresql
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:
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:

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:
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:
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:
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”.