Skip to content

Commit f2ebf7b

Browse files
authored
Fix potential issue around resolving localhost to IPv6 on Windows (patroni#3380)
When specifying `0.0.0.0` or `127.0.0.1` in `listen_addresses`, Postgres won't listen on IPv6 (in other words, these addresses imply IPv4 only). However, the typical Windows system will have a preference to resolve `localhost` to `::1`. While one can change these preferences, it's unclear if it has unwanted side effects. Instead of this, the proposed small change fixes this behavior.
1 parent 863bd6a commit f2ebf7b

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

patroni/postgresql/config.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,8 +1102,11 @@ def _get_tcp_local_address(self) -> str:
11021102
listen_addresses = self._server_parameters['listen_addresses'].split(',')
11031103

11041104
for la in listen_addresses:
1105-
if la.strip().lower() in ('*', '0.0.0.0', '127.0.0.1', 'localhost'): # we are listening on '*' or localhost
1105+
if la.strip().lower() in ('*', 'localhost'): # we are listening on '*' or localhost
11061106
return 'localhost' # connection via localhost is preferred
1107+
if la.strip() in ('0.0.0.0', '127.0.0.1'): # Postgres listens only on IPv4
1108+
# localhost, but don't allow Windows to resolve to IPv6
1109+
return '127.0.0.1' if os.name == 'nt' else 'localhost'
11071110
return listen_addresses[0].strip() # can't use localhost, take first address from listen_addresses
11081111

11091112
def resolve_connection_addresses(self) -> None:

0 commit comments

Comments
 (0)