Siddharth M
Siddharth M

Reputation: 157

How to get port number of database in postgresql for ubuntu?

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?

Upvotes: 2

Views: 1360

Answers (3)

Craig Ringer
Craig Ringer

Reputation: 324395

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

Upvotes: 4

Vivek S.
Vivek S.

Reputation: 21905

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

    select inet_server_port();

Upvotes: 2

Perfect28
Perfect28

Reputation: 11317

Try with this :

netstat -tulpn | grep postgres

Upvotes: 5

Related Questions