/ Adam's Pile of Hacks / blog

Can't start Postgres 12 on Windows 10

August 14, 2021

I installed Postgres on my Windows 10 21H1 machine and was not able to start the database server - it insisted that port 5432 was in use.

Event Viewer showed the following:

2021-08-14 13:03:04.273 AEST [19608] FATAL:  could not create any TCP/IP sockets

I got suspicious when running the server manually on different ports:

PS C:\Users\Adam>  & "C:\Program Files\PostgreSQL\12\bin\postgres.exe" -D "B:\db" -p5430
2021-08-14 13:12:44.164 AEST [19176] LOG:  starting PostgreSQL 12.7, compiled by Visual C++ build 1914, 64-bit
2021-08-14 13:12:44.165 AEST [19176] LOG:  could not bind IPv4 address "127.0.0.1": Permission denied
2021-08-14 13:12:44.165 AEST [19176] HINT:  Is another postmaster already running on port 5430? If not, wait a few secon2021-08-14 13:12:44.165 AEST [19176] WARNING:  could not create listen socket for "127.0.0.1"
2021-08-14 13:12:44.165 AEST [19176] FATAL:  could not create any TCP/IP sockets
2021-08-14 13:12:44.166 AEST [19176] LOG:  database system is shut down
PS C:\Users\Adam>  & "C:\Program Files\PostgreSQL\12\bin\postgres.exe" -D "B:\db" -p5400
2021-08-14 13:14:11.689 AEST [19560] LOG:  starting PostgreSQL 12.7, compiled by Visual C++ build 1914, 64-bit
2021-08-14 13:14:11.690 AEST [19560] LOG:  could not bind IPv4 address "127.0.0.1": Permission denied
2021-08-14 13:14:11.690 AEST [19560] HINT:  Is another postmaster already running on port 5400? If not, wait a few seconds and retry.
2021-08-14 13:14:11.690 AEST [19560] WARNING:  could not create listen socket for "127.0.0.1"
PS C:\Users\Adam>  & "C:\Program Files\PostgreSQL\12\bin\postgres.exe" -D "B:\db" -p5300
2021-08-14 13:14:15.096 AEST [19660] LOG:  redirecting log output to logging collector process

(I’d already changed postgresql.conf to have 127.0.0.1 as the host just in case * or localhost was causing issues)

So 5432, 5430, 5400 are out but 5300 works? Right, Windows is lying to me again.

Infuriatingly, all the posts about this told me to run netstat or TCPView, which is great except the port wasn’t in use. Eventually I was led to the following command:

netsh int ipv4 show excludedportrange protocol=tcp

Protocol tcp Port Exclusion Ranges

Start Port    End Port
----------    --------
      1063        1162
      1163        1262
      1263        1362
      1363        1462
      1463        1562
      1678        1777
      1778        1877
      1878        1977
      1978        2077
      2207        2306
      2400        2499
      2500        2599
      2600        2699
      2906        3005
      3006        3105
      3106        3205
      3206        3305
      3390        3489
      3490        3589
      3590        3689
      4001        4100
      4101        4200
      4201        4300
      4301        4400
      4401        4500
      4501        4600
      4601        4700
      4912        5011
      5041        5140
      5141        5240
      5357        5357
      5358        5457
     50000       50059     *

* - Administered port exclusions.

So there’s an “Administered port exclusion” for TCP ports 5358-5457, which happens to include Postgres’ default 5432.

Of course, Googling doesn’t bring you to a Microsoft explanation straight away, only a StackOverflow answer. If you really want to use 5432 you’ll need to disable Hyper-V, change the port exclusion, then re-enable Hyper-V.

These links will help:

https://stackoverflow.com/a/65234135/229631

https://pomeroy.me/2020/09/solved-windows-10-forbidden-port-bind/

https://support.microsoft.com/en-us/topic/you-cannot-exclude-ports-by-using-the-reservedports-registry-key-in-windows-server-2008-or-in-windows-server-2008-r2-a68373fd-9f64-4bde-9d68-c5eded74ea35

For my use, changing Postgres’ listen port to something else was fine (i.e. port = 55432 in postgresql.conf)