Skip to content

Commit bc02b8d

Browse files
am1tercofin
andauthored
feat: Enhance Postgres Services with configurable container host (#87)
Co-authored-by: Cody Fincher <cody@litestar.dev>
1 parent 461f898 commit bc02b8d

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

docs/supported-databases/postgres.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Usage Example
3636
Available Fixtures
3737
------------------
3838

39+
* ``postgres_host``: The PostgreSQL host address (defaults to "127.0.0.1", can be overridden with ``POSTGRES_HOST`` environment variable).
3940
* ``postgres_user``: The PostgreSQL user.
4041
* ``postgres_password``: The PostgreSQL password.
4142
* ``postgres_database``: The PostgreSQL database name to use.
@@ -55,6 +56,19 @@ The following version-specific fixtures are also available:
5556
* ``postgres_17_image``, ``postgres_17_service``, ``postgres_17_connection``: PostgreSQL 17.x
5657
* ``pgvector_image``, ``pgvector_service``. ``pgvector_connection``: Latest Available pgvector Docker image.
5758

59+
Configuration
60+
-------------
61+
62+
PostgreSQL services can be configured using environment variables:
63+
64+
* ``POSTGRES_HOST``: The host address for the PostgreSQL container (default: "127.0.0.1")
65+
66+
Example usage with custom host:
67+
68+
.. code-block:: bash
69+
70+
export POSTGRES_HOST="192.168.1.100"
71+
pytest
5872
5973
Service API
6074
-----------

src/pytest_databases/_service.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ def run(
126126
image: str,
127127
container_port: int,
128128
name: str,
129+
container_host: str = "127.0.0.1",
129130
command: str | None = None,
130131
env: dict[str, Any] | None = None,
131132
exec_after_start: str | list[str] | None = None,
@@ -187,7 +188,7 @@ def run(
187188
container.ports[next(k for k in container.ports if k.startswith(str(container_port)))][0]["HostPort"]
188189
)
189190
service = ServiceContainer(
190-
host="127.0.0.1",
191+
host=container_host,
191192
port=host_port,
192193
)
193194

src/pytest_databases/docker/postgres.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import dataclasses
4+
import os
45
from contextlib import contextmanager
56
from typing import TYPE_CHECKING
67

@@ -32,6 +33,11 @@ class PostgresService(ServiceContainer):
3233
user: str
3334

3435

36+
@pytest.fixture(autouse=False, scope="session")
37+
def postgres_host() -> str:
38+
return os.environ.get("POSTGRES_HOST", "127.0.0.1")
39+
40+
3541
@pytest.fixture(autouse=False, scope="session")
3642
def postgres_password() -> str:
3743
return "super-secret"
@@ -47,6 +53,7 @@ def _provide_postgres_service(
4753
docker_service: DockerService,
4854
image: str,
4955
name: str,
56+
host: str,
5057
user: str,
5158
password: str,
5259
xdist_postgres_isolate: XdistIsolationLevel,
@@ -79,6 +86,7 @@ def check(_service: ServiceContainer) -> bool:
7986
with docker_service.run(
8087
image=image,
8188
check=check,
89+
container_host=host,
8290
container_port=5432,
8391
name=name,
8492
env={
@@ -100,6 +108,7 @@ def check(_service: ServiceContainer) -> bool:
100108
def postgres_11_service(
101109
docker_service: DockerService,
102110
xdist_postgres_isolation_level: XdistIsolationLevel,
111+
postgres_host: str,
103112
postgres_user: str,
104113
postgres_password: str,
105114
) -> Generator[PostgresService, None, None]:
@@ -108,6 +117,7 @@ def postgres_11_service(
108117
image="postgres:11",
109118
name="postgres-11",
110119
xdist_postgres_isolate=xdist_postgres_isolation_level,
120+
host=postgres_host,
111121
user=postgres_user,
112122
password=postgres_password,
113123
) as service:
@@ -118,6 +128,7 @@ def postgres_11_service(
118128
def postgres_12_service(
119129
docker_service: DockerService,
120130
xdist_postgres_isolation_level: XdistIsolationLevel,
131+
postgres_host: str,
121132
postgres_user: str,
122133
postgres_password: str,
123134
) -> Generator[PostgresService, None, None]:
@@ -126,6 +137,7 @@ def postgres_12_service(
126137
image="postgres:12",
127138
name="postgres-12",
128139
xdist_postgres_isolate=xdist_postgres_isolation_level,
140+
host=postgres_host,
129141
user=postgres_user,
130142
password=postgres_password,
131143
) as service:
@@ -136,6 +148,7 @@ def postgres_12_service(
136148
def postgres_13_service(
137149
docker_service: DockerService,
138150
xdist_postgres_isolation_level: XdistIsolationLevel,
151+
postgres_host: str,
139152
postgres_user: str,
140153
postgres_password: str,
141154
) -> Generator[PostgresService, None, None]:
@@ -144,6 +157,7 @@ def postgres_13_service(
144157
image="postgres:13",
145158
name="postgres-13",
146159
xdist_postgres_isolate=xdist_postgres_isolation_level,
160+
host=postgres_host,
147161
user=postgres_user,
148162
password=postgres_password,
149163
) as service:
@@ -154,6 +168,7 @@ def postgres_13_service(
154168
def postgres_14_service(
155169
docker_service: DockerService,
156170
xdist_postgres_isolation_level: XdistIsolationLevel,
171+
postgres_host: str,
157172
postgres_user: str,
158173
postgres_password: str,
159174
) -> Generator[PostgresService, None, None]:
@@ -162,6 +177,7 @@ def postgres_14_service(
162177
image="postgres:14",
163178
name="postgres-14",
164179
xdist_postgres_isolate=xdist_postgres_isolation_level,
180+
host=postgres_host,
165181
user=postgres_user,
166182
password=postgres_password,
167183
) as service:
@@ -172,6 +188,7 @@ def postgres_14_service(
172188
def postgres_15_service(
173189
docker_service: DockerService,
174190
xdist_postgres_isolation_level: XdistIsolationLevel,
191+
postgres_host: str,
175192
postgres_user: str,
176193
postgres_password: str,
177194
) -> Generator[PostgresService, None, None]:
@@ -180,6 +197,7 @@ def postgres_15_service(
180197
image="postgres:15",
181198
name="postgres-15",
182199
xdist_postgres_isolate=xdist_postgres_isolation_level,
200+
host=postgres_host,
183201
user=postgres_user,
184202
password=postgres_password,
185203
) as service:
@@ -190,6 +208,7 @@ def postgres_15_service(
190208
def postgres_16_service(
191209
docker_service: DockerService,
192210
xdist_postgres_isolation_level: XdistIsolationLevel,
211+
postgres_host: str,
193212
postgres_user: str,
194213
postgres_password: str,
195214
) -> Generator[PostgresService, None, None]:
@@ -198,6 +217,7 @@ def postgres_16_service(
198217
image="postgres:16",
199218
name="postgres-16",
200219
xdist_postgres_isolate=xdist_postgres_isolation_level,
220+
host=postgres_host,
201221
user=postgres_user,
202222
password=postgres_password,
203223
) as service:
@@ -208,6 +228,7 @@ def postgres_16_service(
208228
def postgres_17_service(
209229
docker_service: DockerService,
210230
xdist_postgres_isolation_level: XdistIsolationLevel,
231+
postgres_host: str,
211232
postgres_user: str,
212233
postgres_password: str,
213234
) -> Generator[PostgresService, None, None]:
@@ -216,6 +237,7 @@ def postgres_17_service(
216237
image="postgres:17",
217238
name="postgres-17",
218239
xdist_postgres_isolate=xdist_postgres_isolation_level,
240+
host=postgres_host,
219241
user=postgres_user,
220242
password=postgres_password,
221243
) as service:
@@ -344,6 +366,7 @@ def postgres_service(
344366
docker_service: DockerService,
345367
postgres_image: str,
346368
xdist_postgres_isolation_level: XdistIsolationLevel,
369+
postgres_host: str,
347370
postgres_user: str,
348371
postgres_password: str,
349372
) -> Generator[PostgresService, None, None]:
@@ -352,6 +375,7 @@ def postgres_service(
352375
image=postgres_image,
353376
name="postgres",
354377
xdist_postgres_isolate=xdist_postgres_isolation_level,
378+
host=postgres_host,
355379
user=postgres_user,
356380
password=postgres_password,
357381
) as service:
@@ -384,6 +408,7 @@ def pgvector_service(
384408
docker_service: DockerService,
385409
pgvector_image: str,
386410
xdist_postgres_isolation_level: XdistIsolationLevel,
411+
postgres_host: str,
387412
postgres_user: str,
388413
postgres_password: str,
389414
) -> Generator[PostgresService, None, None]:
@@ -392,6 +417,7 @@ def pgvector_service(
392417
image=pgvector_image,
393418
name="pgvector",
394419
xdist_postgres_isolate=xdist_postgres_isolation_level,
420+
host=postgres_host,
395421
user=postgres_user,
396422
password=postgres_password,
397423
) as service:
@@ -424,6 +450,7 @@ def alloydb_omni_service(
424450
docker_service: DockerService,
425451
pgvector_image: str,
426452
xdist_postgres_isolation_level: XdistIsolationLevel,
453+
postgres_host: str,
427454
postgres_user: str,
428455
postgres_password: str,
429456
) -> Generator[PostgresService, None, None]:
@@ -432,6 +459,7 @@ def alloydb_omni_service(
432459
image=pgvector_image,
433460
name="alloydb-omni",
434461
xdist_postgres_isolate=xdist_postgres_isolation_level,
462+
host=postgres_host,
435463
user=postgres_user,
436464
password=postgres_password,
437465
) as service:

0 commit comments

Comments
 (0)