Thread: Postgres listens on random port

Postgres listens on random port

From
Kal
Date:
Hi 

I have a scenario in which postgres listens on a random port different from the one which was specified in pg_ctl start command.

This occurs in windows. When postgres is started on port 5460, the logs denote that postgres is started on 5460. But the database actually listens on some random port like 20011

When the same postgres is started as a service,it listens on the intended port.

I have tried with both normal and admin users.

Any suggestions on this behaviour will be helpful. 


Regards 
Kal

Re: Postgres listens on random port

From
Greg Sabino Mullane
Date:
Start by seeing where the port is actually being set by running this:

select setting, source, sourcefile, sourceline from pg_settings where name = 'port';
Cheers,
Greg

Re: Postgres listens on random port

From
Kal
Date:


On Tue, 5 Nov 2024 at 7:42 PM, Greg Sabino Mullane <htamfids@gmail.com> wrote:
Start by seeing where the port is actually being set by running this:

select setting, source, sourcefile, sourceline from pg_settings where name = 'port';
Cheers,
Greg

Hi Greg,

The query output states the source as command line. Event the port value output from show port is same as given in pg_ctl command.

But the database is listening on some other random port.


Regards
Kal

Re: Postgres listens on random port

From
Saul Perdomo
Date:
" But the database actually listens on some random port like 20011"

How exactly did you determine this?

(If you used, say, netstat, can you share its output with us?)

On Tue, Nov 5, 2024 at 8:46 AM Kal <kalel.code11@gmail.com> wrote:
Hi 

I have a scenario in which postgres listens on a random port different from the one which was specified in pg_ctl start command.

This occurs in windows. When postgres is started on port 5460, the logs denote that postgres is started on 5460. But the database actually listens on some random port like 20011

When the same postgres is started as a service,it listens on the intended port.

I have tried with both normal and admin users.

Any suggestions on this behaviour will be helpful. 


Regards 
Kal

Re: Postgres listens on random port

From
Kal
Date:
Hi

The netstat command did not reveal any entries on the intended port (say 5460). The 5460 port was vacant, no processes were using it.
 I had to crawl through the postgres processes on task manager to find the listening port.


Could this be due interference from other services? But the port 5460 remains unused.


Regards, 
Kal

On Tue, 5 Nov 2024 at 7:57 PM, Saul Perdomo <saul.perdomo@gmail.com> wrote:
" But the database actually listens on some random port like 20011"

How exactly did you determine this?

(If you used, say, netstat, can you share its output with us?)

On Tue, Nov 5, 2024 at 8:46 AM Kal <kalel.code11@gmail.com> wrote:
Hi 

I have a scenario in which postgres listens on a random port different from the one which was specified in pg_ctl start command.

This occurs in windows. When postgres is started on port 5460, the logs denote that postgres is started on 5460. But the database actually listens on some random port like 20011

When the same postgres is started as a service,it listens on the intended port.

I have tried with both normal and admin users.

Any suggestions on this behaviour will be helpful. 


Regards 
Kal

Re: Postgres listens on random port

From
Ron Johnson
Date:
On Tue, Nov 5, 2024 at 9:22 AM Kal <kalel.code11@gmail.com> wrote:

On Tue, 5 Nov 2024 at 7:42 PM, Greg Sabino Mullane <htamfids@gmail.com> wrote:
Start by seeing where the port is actually being set by running this:

select setting, source, sourcefile, sourceline from pg_settings where name = 'port';
Cheers,
Greg

Hi Greg,

The query output states the source as command line. Event the port value output from show port is same as given in pg_ctl command.

But the database is listening on some other random port.
 
Client applications use a random port on their host:  In this case, 10.176.242.216 is the client, and 10.109.165.4, and 10.176.252.201 are the DB servers:

$ netstat -an | grep :5432
tcp        0      0 10.176.242.216:37086    10.109.165.4:5432       ESTABLISHED
tcp        0      0 10.176.242.216:43256    10.176.252.201:5432     TIME_WAIT  
tcp        0      0 10.176.242.216:36172    10.143.170.55:5432      TIME_WAIT  
tcp        0      0 10.176.242.216:36142    10.143.170.55:5432      TIME_WAIT  
tcp        0      0 10.176.242.216:36156    10.143.170.55:5432      TIME_WAIT  
tcp        0      0 10.176.242.216:36174    10.143.170.55:5432      TIME_WAIT  
tcp        0      0 10.176.242.216:38998    10.109.165.4:5432       TIME_WAIT  
tcp        0      0 10.176.242.216:36186    10.143.170.55:5432      TIME_WAIT  
tcp        0      0 10.176.242.216:36188    10.143.170.55:5432      ESTABLISHED
tcp       19      0 10.176.242.216:36196    10.143.170.55:5432      ESTABLISHED
tcp       19      0 10.176.242.216:36208    10.143.170.55:5432      ESTABLISHED
tcp        0      0 10.176.242.216:36210    10.143.170.55:5432      ESTABLISHED


--

Death to <Redacted>, and butter sauce.
Don't boil me, I'm still alive.
<Redacted> crustacean!

Re: Postgres listens on random port

From
Erik Wienhold
Date:
On 2024-11-05 15:22 +0100, Kal wrote:
> On Tue, 5 Nov 2024 at 7:42 PM, Greg Sabino Mullane <htamfids@gmail.com>
> wrote:
> 
> > Start by seeing where the port is actually being set by running this:
> >
> > select setting, source, sourcefile, sourceline from pg_settings where name
> > = 'port';
> > Cheers,
> > Greg
> >
> > Hi Greg,
> 
> The query output states the source as command line. Event the port value
> output from show port is same as given in pg_ctl command.
> 
> But the database is listening on some other random port.

Please provide more details:

1) The complete pg_ctl command that you've executed.

2) Which Postgres version?

3) Which OS?

-- 
Erik



Re: Postgres listens on random port

From
Saul Perdomo
Date:
Agreed - it sounds like OP found a client process and mistook it for a server process, which is why I wanted to know where exactly he saw this.

On Tue, Nov 5, 2024 at 9:41 AM Ron Johnson <ronljohnsonjr@gmail.com> wrote:
On Tue, Nov 5, 2024 at 9:22 AM Kal <kalel.code11@gmail.com> wrote:

On Tue, 5 Nov 2024 at 7:42 PM, Greg Sabino Mullane <htamfids@gmail.com> wrote:
Start by seeing where the port is actually being set by running this:

select setting, source, sourcefile, sourceline from pg_settings where name = 'port';
Cheers,
Greg

Hi Greg,

The query output states the source as command line. Event the port value output from show port is same as given in pg_ctl command.

But the database is listening on some other random port.
 
Client applications use a random port on their host:  In this case, 10.176.242.216 is the client, and 10.109.165.4, and 10.176.252.201 are the DB servers:

$ netstat -an | grep :5432
tcp        0      0 10.176.242.216:37086    10.109.165.4:5432       ESTABLISHED
tcp        0      0 10.176.242.216:43256    10.176.252.201:5432     TIME_WAIT  
tcp        0      0 10.176.242.216:36172    10.143.170.55:5432      TIME_WAIT  
tcp        0      0 10.176.242.216:36142    10.143.170.55:5432      TIME_WAIT  
tcp        0      0 10.176.242.216:36156    10.143.170.55:5432      TIME_WAIT  
tcp        0      0 10.176.242.216:36174    10.143.170.55:5432      TIME_WAIT  
tcp        0      0 10.176.242.216:38998    10.109.165.4:5432       TIME_WAIT  
tcp        0      0 10.176.242.216:36186    10.143.170.55:5432      TIME_WAIT  
tcp        0      0 10.176.242.216:36188    10.143.170.55:5432      ESTABLISHED
tcp       19      0 10.176.242.216:36196    10.143.170.55:5432      ESTABLISHED
tcp       19      0 10.176.242.216:36208    10.143.170.55:5432      ESTABLISHED
tcp        0      0 10.176.242.216:36210    10.143.170.55:5432      ESTABLISHED


--

Death to <Redacted>, and butter sauce.
Don't boil me, I'm still alive.
<Redacted> crustacean!

Re: Postgres listens on random port

From
Adrian Klaver
Date:
On 11/5/24 06:22, Kal wrote:
> 
> 
> On Tue, 5 Nov 2024 at 7:42 PM, Greg Sabino Mullane <htamfids@gmail.com 
> <mailto:htamfids@gmail.com>> wrote:
> 
>     Start by seeing where the port is actually being set by running this:
> 
>     select setting, source, sourcefile, sourceline from pg_settings
>     where name = 'port';
>     Cheers,
>     Greg
> 
> Hi Greg,
> 
> The query output states the source as command line. Event the port value 
> output from show port is same as given in pg_ctl command.

How did you connect to the database to run the query if the port is random?

In other words what client and connection settings did you use to 
connect to the server and run the query?

> 
> But the database is listening on some other random port.
> 
> 
> Regards
> Kal

-- 
Adrian Klaver
adrian.klaver@aklaver.com