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

link|improve this question

80% accept rate
feedback

5 Answers

up vote 4 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
link|improve this answer
feedback

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.

link|improve this answer
feedback

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>
link|improve this answer
You may have to run this as root. – Michael Mior Mar 1 '10 at 15:19
feedback

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
link|improve this answer
feedback

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

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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