2

I'm using postgresql 9.3 on ubuntu 14.04 LTS. I know that the default port number for postgres is 5432. But, in case if it is different from the default one, then how can I get it?

1
  • 1
    this will find more answers on serverfault.com Commented Jul 7, 2014 at 11:36

3 Answers 3

5

Try with this :

netstat -tulpn | grep postgres
Sign up to request clarification or add additional context in comments.

1 Comment

Yes it works! But it is showing only when I'm logging-in as root.
4

It depends on how you installed PostgreSQL and how it was configured.

There might even be multiple instances of PostgreSQL 9.3 on the machine - zero or more from Ubuntu packages, managed via pg_wrapper, and zero or more from other sources like the EDB graphical installer, compiled from source, etc.

Assuming you're only interested in packaged versions managed by pg_wrapper and you only expect there to be one version: use pg_lsclusters.

$ pg_lsclusters
Ver Cluster Port Status Owner    Data directory               Log file
9.2 main    5433 down   postgres /var/lib/postgresql/9.2/main /var/log/postgresql/postgresql-9.2-main.log
9.3 main    5432 online postgres /var/lib/postgresql/9.3/main /var/log/postgresql/postgresql-9.3-main.log

e.g.

pg_lsclusters -h | awk '/^9.3/ { if ($2 == "main") { print $3; } }'

Comments

2
select setting from pg_settings where name = 'port'
  • if you have multiple servers in the same cluster then use

    select inet_server_port();

1 Comment

That's only useful if you already know the port to connect to, though. So it's kind of a catch-22. You can't assume that the default port is the one you want - it depends on how the machine is set up, and it might well connect to some other PostgreSQL instance by default.

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.