Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the Postgres port

Tags:

postgresql

I just installed Postgres newly and I try to check if its running and on what port, although I know what default port it normally has these commands are not showing me the port.

service postgresql status

I get this

postgresql.service - PostgreSQL RDBMS
   Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor prese
   Active: active (exited) since Tue 2018-09-18 14:17:14 CEST; 22min ago
  Process: 2632 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
 Main PID: 2632 (code=exited, status=0/SUCCESS)
    Tasks: 0
   Memory: 0B
      CPU: 0
   CGroup: /system.slice/postgresql.service

Sep 18 14:17:14 user-HP-Pavilion-TS-15-Notebook-PC systemd[1]: Starting PostgreS
Sep 18 14:17:14 user-HP-Pavilion-TS-15-Notebook-PC systemd[1]: Started PostgreSQ
Sep 18 14:39:19 user-HP-Pavilion-TS-15-Notebook-PC systemd[1]: Started PostgreSQ

And when I just try to simply check process and see what port it shows this after the command

 sudo netstat -plunt |grep postgres

tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      2592/postgres   
tcp        0      0 127.0.0.1:9187          0.0.0.0:*               LISTEN      1065/postgres_expor

How can I get the port to show and what is going on?

like image 492
valik Avatar asked Sep 18 '18 12:09

valik


Video Answer


2 Answers

This answer on serverfault needs you to connect to the machine and no other packages or root user.

Just connect and run the command.

If you can connect to PostgreSQL from the original machine you can see what port it's listening on:

SHOW port;

like image 55
Vikram Avatar answered Oct 08 '22 00:10

Vikram


First, find the “postmaster” process, the parent of all other PostgreSQL processes.

You can get it from the postmaster.pid file in the PostgreSQL data directory if the database is started.

Then you can get the port with lsof. Assuming the process ID is 23521, run

lsof -P -sTCP:LISTEN -i TCP -a -p 23521

That will show you the port where PostgreSQL is listening.

like image 21
Laurenz Albe Avatar answered Oct 08 '22 00:10

Laurenz Albe