Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know that by default PostgreSQL listens on port 5432, but what is the command to actually determine PostgreSQL's port?

Configuration: Ubuntu 9.10 with PostgreSQL 8.4

share|improve this question
up vote 11 down vote accepted

lsof and nmap are solutions, but they're not installed by default. What you want is netstat(8).

sudo netstat -plunt |grep postgres
share|improve this answer

The PostgreSQL utility pg_lsclusters shows information about the configuration and status of all clusters, including the port number.

$ pg_lsclusters
Version Cluster   Port Status Owner    Data directory                     Log file
8.4     main      5433 online postgres /var/lib/postgresql/8.4/main       /var/log/postgresql/postgresql-8.4-main.log

This also has the advantage of not requiring 'sudo' privileges to run.

On Debian and Ubuntu systems, the pg_lsclusters command is provided by the package postgresql-common, which should be installed by default with the postgresql server.

share|improve this answer
Note that pg_lsclusters is an Ubuntu-ism, and is not a standard Postgres command. It will work for this case, but is not a general-purpose solution... – voretaq7 Oct 11 '12 at 20:45

If you want to do it from inside the database, just do "SHOW port". But that assumes you've been able to connect to it, at least locally...

share|improve this answer

If you are searching on the local machine, I would use the lsof command to check for the port postgresql is using

lsof -p <postgres_process_id>
share|improve this answer
You may have to run this as root. – Michael Mior Mar 1 '10 at 15:19

Here's one solution that I've found:

sudo apt-get install nmap
sudo nmap localhost | grep postgresql

If you're wanting to search a non-local machine, just change localhost to the server's IP address.

share|improve this answer

I have machines with multiple postgres instances running -- and so I also have the issue of trying to match up the correct database with each port. I tend to do:

$ ps aux | grep  postgres | grep -v 'postgres:'

And then, for each of instances returned look for the directory (-D argument) and:

$ sudo grep port $DIR/postgresql.conf
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.