Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit 074c051

Browse files
authored
Allow setting min_size and max_size in postgres DSN (#210)
Overtakes #129 Closes #78
1 parent d822465 commit 074c051

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

databases/backends/postgres.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,14 @@ def _get_connection_kwargs(self) -> dict:
6262
async def connect(self) -> None:
6363
assert self._pool is None, "DatabaseBackend is already running"
6464
kwargs = self._get_connection_kwargs()
65-
self._pool = await asyncpg.create_pool(str(self._database_url), **kwargs)
65+
self._pool = await asyncpg.create_pool(
66+
host=self._database_url.hostname,
67+
port=self._database_url.port,
68+
user=self._database_url.username,
69+
password=self._database_url.password,
70+
database=self._database_url.database,
71+
**kwargs,
72+
)
6673

6774
async def disconnect(self) -> None:
6875
assert self._pool is not None, "DatabaseBackend is not running"

tests/test_connection_options.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from databases.backends.aiopg import AiopgBackend
66
from databases.backends.mysql import MySQLBackend
77
from databases.backends.postgres import PostgresBackend
8+
from databases.core import DatabaseURL
9+
from tests.test_databases import DATABASE_URLS, async_adapter
810

911

1012
def test_postgres_pool_size():
@@ -13,6 +15,16 @@ def test_postgres_pool_size():
1315
assert kwargs == {"min_size": 1, "max_size": 20}
1416

1517

18+
@async_adapter
19+
async def test_postgres_pool_size_connect():
20+
for url in DATABASE_URLS:
21+
if DatabaseURL(url).dialect != "postgresql":
22+
continue
23+
backend = PostgresBackend(url + "?min_size=1&max_size=20")
24+
await backend.connect()
25+
await backend.disconnect()
26+
27+
1628
def test_postgres_explicit_pool_size():
1729
backend = PostgresBackend("postgres://localhost/database", min_size=1, max_size=20)
1830
kwargs = backend._get_connection_kwargs()

0 commit comments

Comments
 (0)