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
SELECT *
FROM pg_settings
WHERE name = 'port';

- 22,834
- 10
- 68
- 88
-
3This doesn't work if you have multiple servers in the same cluster, unfortunately... – Vitor Baptista Mar 01 '13 at 22:57
-
15This actually gives you the port number not the server host. – hd1 May 08 '13 at 18:12
-
3@hd1: And? The question was (also) how to get the port number. And that is what the query returns. – May 08 '13 at 22:26
-
7yes, but if you can't connect to the database, you can't execute that :P – jaydel May 05 '20 at 16:08
This command will give you postgres port number
\conninfo
If Postgres is running on a Linux server, you can also use the following command
sudo netstat -plunt |grep postgres
OR (if it comes as postmaster)
sudo netstat -plunt |grep postmaster
and you will see something similar as this
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 140/postgres
tcp6 0 0 ::1:5432 :::* LISTEN 140/postgres
In this case, the port number is 5432 which is also the default port number
credit: link

- 3,774
- 1
- 27
- 36
-
1
-
14this is the right answer. the chosen answer is true but not related to the question. – Luis Martins Apr 20 '18 at 05:52
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.

- 146,994
- 96
- 417
- 335

- 19,267
- 19
- 110
- 137
-
1I encountered this problem too, and trying 5433 port, and this is working – Jonny Manowar Aug 07 '18 at 12:10
-
85
-
2@s.k Read the context. The OP didn't know the host and port for their postgres database. They can connect with a basic `psql` command, so that implies `localhost:5432`. Unless someone got clever with env vars/aliases/etc. I don't love my answer a decade later, but [solving the x](http://xyproblem.info/) instead of y is perfectly fine and in this case has helped hundreds of people. – Brad Koch Apr 17 '20 at 12:43
-
1I agree with @s.k I'm on a remote server where the database is hosted so of course can connect locally there via localhost, but I wish for an external application on a different server to access the database where of course the database doesn't exist locally, and hence knowing the answer to the original question would be useful for me – user4779 Mar 11 '21 at 05:09
-
select inet_server_addr();
gives you the ip address of the server.

- 3,958
- 1
- 18
- 16
-
4regrettably this does not give the "live" hostname if you're connecting via ssh proxy - I keep getting "127.0.0.1" – Andrew Wolfe Jan 05 '15 at 18:28
-
18I did not get anything: ```$postgres=# select inet_server_addr(); inet_server_addr ------------------ (1 row)``` – trex Apr 14 '17 at 05:37
-
1
select inet_server_addr( ), inet_server_port( );

- 5,822
- 46
- 39
-
3How is this any different from Kaarel Kitsemets's [answer](http://stackoverflow.com/a/12454501/369450) and Andromida's [answer](http://stackoverflow.com/a/22986571/369450)? – Uyghur Lives Matter Jun 02 '16 at 19:53
-
1It answers the question that was asked as a single copy and paste solution. – gerardw Jun 03 '16 at 11:41
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.

- 14,268
- 1
- 48
- 91
select inet_server_port();
gives you the port of the server.

- 1,095
- 1
- 11
- 28
-
3On bluehost this query returns a blank field when in reality the port was 5432. This query does not always return the port number. – Eric Leschinski Apr 06 '15 at 21:14
From the terminal you can simply do a "postgres list clusters":
pg_lsclusters
It will return Postgres version number, cluster names, ports, status, owner, and the location of your data directories and log file.

- 361
- 2
- 7
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:
port = 5432
Changing the number there requires restart of postgresql for it to take effect.

- 146,994
- 96
- 417
- 335
-
This is a way better answer than all of the above. Additionally, it is possible to show the current path by using `sudo -u postgres psql -c 'SHOW config_file'` and also the path is nowadays 14 of course. – TheLexoPlexx Feb 03 '23 at 15:10
From the terminal you can do:
\conninfo
I would suggest reading a documentation on their exhaustive list of all commands using:
\?

- 341
- 4
- 11
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".

- 177
- 1
- 2
- 7
To find the port number you can run this command (assuming you are on localhost)
select setting from pg_settings where name='port';

- 17,750
- 17
- 113
- 128
SELECT CURRENT_USER usr, :'HOST' host, inet_server_port() port;
This uses psql's built in HOST variable, documented here
And postgres System Information Functions, documented here

- 125
- 5
-
2If you're connecting locally, `HOST` will be the directory in which your UNIX domain socket resides, eg `/tmp`. – bishop Sep 10 '19 at 17:29
service postgresql status
returns: 10/main (port 5432): online
I'm running Ubuntu 18.04

- 219
- 3
- 7
An addition to the @a_horse_with_no_name answer. To get the hostname:
SELECT boot_val,reset_val FROM pg_settings WHERE name='listen_addresses';;

- 322
- 1
- 3
- 9
go to the "Terminal" and just type
service postgres status
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.

- 720
- 13
- 27
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
/* SQL CODE */
CREATE TEMP TABLE tmp ( hostname text, port bigint ) ON COMMIT DROP;
COPY tmp FROM PROGRAM $pgm$ printf "$HOSTNAME\t$(i=1 && until [[ "$(psql -U postgres -p $i -qt -c "SELECT 'true'" 2>/dev/null | sed -e '$d' | xargs | tr -d "\n")" == "true" ]]; do i=$(($i+1)) && if [ $i == "65535" ]; then break ; fi ; done && echo $i)"$pgm$ ( format 'text', delimiter '\t' );
SELECT host, port FROM tmp;
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.
If you need to be able to call without a temp table, i.e. as a function invocation, try implementing the above shell in the plsh language.

- 99
- 7
I think PostgreSQL didn't provide an in-built function to get the hostname of server so we might need to write an extension to get the information from server.
I found there is a PostgreSQL extension pg-hostname which can get the hostname from server.
When we have installed the extension we can enable that and query information by inet_server_port
& hostname
function.
CREATE EXTENSION hostname;
SELECT hostname(),inet_server_port();

- 44,943
- 6
- 31
- 51
I use such a solution. No plugins required. Temporary tables are not needed. Only for unix.
select pg_read_file('/etc/hostname') as hostname, setting as port from pg_settings where name='port';

- 579
- 1
- 8
- 17
Right click your SQL server. Mine is PostgresSQL 13 and select properties -> connection. This has:
- host name
- port ID
- Username.

- 142
- 2
- 11