From bacd05d99e371fabed8ce201b1d3d3d5f9347f28 Mon Sep 17 00:00:00 2001 From: Alexander Kukushkin Date: Mon, 1 Jun 2015 17:00:15 +0200 Subject: [PATCH] Determine preferable local address to connect through. If listen contains '*' or 0.0.0.0 - connect via localhost In all other cases pick the first one. --- helpers/postgresql.py | 10 ++++++++-- tests/test_postgresql.py | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/helpers/postgresql.py b/helpers/postgresql.py index 06e8b1a1..7f027b2b 100644 --- a/helpers/postgresql.py +++ b/helpers/postgresql.py @@ -57,8 +57,14 @@ class Postgresql: self.members = [] # list of already existing replication slots def get_local_address(self): - # TODO: try to get unix_socket_directory from postmaster.pid - return self.listen_addresses.split(',')[0].strip() + ':' + self.port + listen_addresses = self.listen_addresses.split(',') + local_address = listen_addresses[0].strip() # take first address from listen_addresses + + for la in listen_addresses: + if la.strip() in ['*', '0.0.0.0']: # we are listening on * + local_address = 'localhost' # connection via localhost is preferred + break + return local_address + ':' + self.port def connection(self): if not self._connection or self._connection.closed != 0: diff --git a/tests/test_postgresql.py b/tests/test_postgresql.py index 4fcec552..4a4b7416 100644 --- a/tests/test_postgresql.py +++ b/tests/test_postgresql.py @@ -91,7 +91,7 @@ class TestPostgresql(unittest.TestCase): def set_up(self): subprocess.call = subprocess_call - self.p = Postgresql({'name': 'test0', 'data_dir': 'data/test0', 'listen': '127.0.0.1, 127.0.0.2:5432', + self.p = Postgresql({'name': 'test0', 'data_dir': 'data/test0', 'listen': '127.0.0.1, *:5432', 'connect_address': '127.0.0.2:5432', 'pg_hba': [{'type': 'hostssl', 'database': 'all', 'user': 'all', 'address': '0.0.0.0/0', 'method': 'md5'}, {'type': 'host', 'database': 'all', 'user': 'all',