264

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?

21 Answers 21

344
SELECT *
FROM pg_settings
WHERE name = 'port';
Sign up to request clarification or add additional context in comments.

5 Comments

This doesn't work if you have multiple servers in the same cluster, unfortunately...
This actually gives you the port number not the server host.
@hd1: And? The question was (also) how to get the port number. And that is what the query returns.
yes, but if you can't connect to the database, you can't execute that :P
Any way to check it directly in the PgAdmin tool ?
289

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

2 Comments

it is coming as postmaster for 5432.
this is the right answer. the chosen answer is true but not related to the question.
82

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.

5 Comments

I encountered this problem too, and trying 5433 port, and this is working
This doesn't actually answer the question.
@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 instead of y is perfectly fine and in this case has helped hundreds of people.
I 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
I have the solution. But thinking how can I push this in the context.
61

select inet_server_addr(); gives you the ip address of the server.

3 Comments

regrettably this does not give the "live" hostname if you're connecting via ssh proxy - I keep getting "127.0.0.1"
I did not get anything: $postgres=# select inet_server_addr(); inet_server_addr ------------------ (1 row)
Did not give the host name
29

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.

enter image description here

Comments

28
select inet_server_addr( ), inet_server_port( );

2 Comments

How is this any different from Kaarel Kitsemets's answer and Andromida's answer?
It answers the question that was asked as a single copy and paste solution.
24

select inet_server_port(); gives you the port of the server.

1 Comment

On bluehost this query returns a blank field when in reality the port was 5432. This query does not always return the port number.
23

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.

Comments

19

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.

2 Comments

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.
It's irrelevant when you are using PostgreSQL as a Docker container, then you define both the host port and the container port in the -p hostport:dockerport part of the docker run command
18

From the terminal you can do:

\conninfo

I would suggest reading a documentation on their exhaustive list of all commands using:

\?

Comments

14

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".

Comments

11

To find the port number you can run this command (assuming you are on localhost)

select setting from pg_settings where name='port';

Comments

7
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

1 Comment

If you're connecting locally, HOST will be the directory in which your UNIX domain socket resides, eg /tmp.
1

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';;

Comments

0
service postgresql status

returns: 10/main (port 5432): online

I'm running Ubuntu 18.04

Comments

0

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.

Comments

0

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();

Comments

0

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';

Comments

0

Right click your SQL server. Mine is PostgresSQL 13 and select properties -> connection. This has:

  • host name
  • port ID
  • Username.

Comments

0

For Windows, inside below file, search for port

C:\Program Files\PostgreSQL\[version]\data\postgresql.conf

or if you are already have access to DB, try below command in POSTGRES:

SHOW config_file;

Comments

-1

go to the "Terminal" and just type

service postgres status

In the results you can get the port detailsRunning postgres details

In my case it's running on port "5432" (default).
I'm using CentOS 7.Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.