Skip to content

Commit 01c0db7

Browse files
AndrewJackson2020CommanderKeynes
and
CommanderKeynes
authored
Fix multi port connection string issue (#1222)
Co-authored-by: CommanderKeynes <[email protected]>
1 parent 07e163f commit 01c0db7

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

asyncpg/connect_utils.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,15 @@ def _read_password_from_pgpass(
168168

169169

170170
def _validate_port_spec(hosts, port):
171-
if isinstance(port, list):
171+
if isinstance(port, list) and len(port) > 1:
172172
# If there is a list of ports, its length must
173173
# match that of the host list.
174174
if len(port) != len(hosts):
175175
raise exceptions.ClientConfigurationError(
176176
'could not match {} port numbers to {} hosts'.format(
177177
len(port), len(hosts)))
178+
elif isinstance(port, list) and len(port) == 1:
179+
port = [port[0] for _ in range(len(hosts))]
178180
else:
179181
port = [port for _ in range(len(hosts))]
180182

tests/test_connect.py

+14
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,20 @@ class TestConnectParams(tb.TestCase):
10881088
}
10891089
)
10901090
},
1091+
{
1092+
'name': 'multi_host_single_port',
1093+
'dsn': 'postgres:///postgres?host=127.0.0.1,127.0.0.2&port=5432&user=postgres',
1094+
'result': (
1095+
[
1096+
('127.0.0.1', 5432),
1097+
('127.0.0.2', 5432)
1098+
], {
1099+
'user': 'postgres',
1100+
'database': 'postgres',
1101+
'target_session_attrs': 'any',
1102+
}
1103+
)
1104+
},
10911105
]
10921106

10931107
@contextlib.contextmanager

0 commit comments

Comments
 (0)