Search code examples
postgresqlconnection

Change PostgreSQL CLIENT Ports


I would like to limit PostgreSQL on what ports he allocates for client connections.

What I mean is having the server listening on 5432, when a connection comes in it will allocate a port to that connection, examples currently on my local test server are 61943, 61949, 61951, 61952, 61957, etc.

In a production server what happens is those client ports are around the high 30000s which is a problem since we have some services running on those, sometimes, before our service has a chance to start, a random client connection will steal the port from it.

How would I go about configuring what the client ports should be for PostgreSQL?


Solution

  • PostgreSQL does not have a way to change this, it's up to the Operating System.

    If using a unix system you can tell the system not to reserve ports in the range of your application by doing this (x and y being the port numbers):

    sysctl -w net.ipv4.ip_local_port_range="x y"

    Edit /etc/sysctl.conf with your preferred text editor and add net.ipv4.ip_local_port_range = x y

    Then to reload the file do sysctl -p

    This will not kill ongoing connections on the old ports, so a reboot is suggested.