Determining PostgreSQL's port
Solution 1:
lsof and nmap are solutions, but they're not installed by default. What you want is netstat(8).
sudo netstat -plunt |grep postgres
Solution 2:
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.
Solution 3:
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...
Solution 4:
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>
Solution 5:
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