Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/supported-databases/postgres.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Usage Example
Available Fixtures
------------------

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

Configuration
-------------

PostgreSQL services can be configured using environment variables:

* ``POSTGRES_HOST``: The host address for the PostgreSQL container (default: "127.0.0.1")

Example usage with custom host:

.. code-block:: bash

export POSTGRES_HOST="192.168.1.100"
pytest

Service API
-----------
Expand Down
3 changes: 2 additions & 1 deletion src/pytest_databases/_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def run(
image: str,
container_port: int,
name: str,
container_host: str = "127.0.0.1",
command: str | None = None,
env: dict[str, Any] | None = None,
exec_after_start: str | list[str] | None = None,
Expand Down Expand Up @@ -187,7 +188,7 @@ def run(
container.ports[next(k for k in container.ports if k.startswith(str(container_port)))][0]["HostPort"]
)
service = ServiceContainer(
host="127.0.0.1",
host=container_host,
port=host_port,
)

Expand Down
28 changes: 28 additions & 0 deletions src/pytest_databases/docker/postgres.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import dataclasses
import os
from contextlib import contextmanager
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -32,6 +33,11 @@ class PostgresService(ServiceContainer):
user: str


@pytest.fixture(autouse=False, scope="session")
def postgres_host() -> str:
return os.environ.get("POSTGRES_HOST", "127.0.0.1")


@pytest.fixture(autouse=False, scope="session")
def postgres_password() -> str:
return "super-secret"
Expand All @@ -47,6 +53,7 @@ def _provide_postgres_service(
docker_service: DockerService,
image: str,
name: str,
host: str,
user: str,
password: str,
xdist_postgres_isolate: XdistIsolationLevel,
Expand Down Expand Up @@ -79,6 +86,7 @@ def check(_service: ServiceContainer) -> bool:
with docker_service.run(
image=image,
check=check,
container_host=host,
container_port=5432,
name=name,
env={
Expand All @@ -100,6 +108,7 @@ def check(_service: ServiceContainer) -> bool:
def postgres_11_service(
docker_service: DockerService,
xdist_postgres_isolation_level: XdistIsolationLevel,
postgres_host: str,
postgres_user: str,
postgres_password: str,
) -> Generator[PostgresService, None, None]:
Expand All @@ -108,6 +117,7 @@ def postgres_11_service(
image="postgres:11",
name="postgres-11",
xdist_postgres_isolate=xdist_postgres_isolation_level,
host=postgres_host,
user=postgres_user,
password=postgres_password,
) as service:
Expand All @@ -118,6 +128,7 @@ def postgres_11_service(
def postgres_12_service(
docker_service: DockerService,
xdist_postgres_isolation_level: XdistIsolationLevel,
postgres_host: str,
postgres_user: str,
postgres_password: str,
) -> Generator[PostgresService, None, None]:
Expand All @@ -126,6 +137,7 @@ def postgres_12_service(
image="postgres:12",
name="postgres-12",
xdist_postgres_isolate=xdist_postgres_isolation_level,
host=postgres_host,
user=postgres_user,
password=postgres_password,
) as service:
Expand All @@ -136,6 +148,7 @@ def postgres_12_service(
def postgres_13_service(
docker_service: DockerService,
xdist_postgres_isolation_level: XdistIsolationLevel,
postgres_host: str,
postgres_user: str,
postgres_password: str,
) -> Generator[PostgresService, None, None]:
Expand All @@ -144,6 +157,7 @@ def postgres_13_service(
image="postgres:13",
name="postgres-13",
xdist_postgres_isolate=xdist_postgres_isolation_level,
host=postgres_host,
user=postgres_user,
password=postgres_password,
) as service:
Expand All @@ -154,6 +168,7 @@ def postgres_13_service(
def postgres_14_service(
docker_service: DockerService,
xdist_postgres_isolation_level: XdistIsolationLevel,
postgres_host: str,
postgres_user: str,
postgres_password: str,
) -> Generator[PostgresService, None, None]:
Expand All @@ -162,6 +177,7 @@ def postgres_14_service(
image="postgres:14",
name="postgres-14",
xdist_postgres_isolate=xdist_postgres_isolation_level,
host=postgres_host,
user=postgres_user,
password=postgres_password,
) as service:
Expand All @@ -172,6 +188,7 @@ def postgres_14_service(
def postgres_15_service(
docker_service: DockerService,
xdist_postgres_isolation_level: XdistIsolationLevel,
postgres_host: str,
postgres_user: str,
postgres_password: str,
) -> Generator[PostgresService, None, None]:
Expand All @@ -180,6 +197,7 @@ def postgres_15_service(
image="postgres:15",
name="postgres-15",
xdist_postgres_isolate=xdist_postgres_isolation_level,
host=postgres_host,
user=postgres_user,
password=postgres_password,
) as service:
Expand All @@ -190,6 +208,7 @@ def postgres_15_service(
def postgres_16_service(
docker_service: DockerService,
xdist_postgres_isolation_level: XdistIsolationLevel,
postgres_host: str,
postgres_user: str,
postgres_password: str,
) -> Generator[PostgresService, None, None]:
Expand All @@ -198,6 +217,7 @@ def postgres_16_service(
image="postgres:16",
name="postgres-16",
xdist_postgres_isolate=xdist_postgres_isolation_level,
host=postgres_host,
user=postgres_user,
password=postgres_password,
) as service:
Expand All @@ -208,6 +228,7 @@ def postgres_16_service(
def postgres_17_service(
docker_service: DockerService,
xdist_postgres_isolation_level: XdistIsolationLevel,
postgres_host: str,
postgres_user: str,
postgres_password: str,
) -> Generator[PostgresService, None, None]:
Expand All @@ -216,6 +237,7 @@ def postgres_17_service(
image="postgres:17",
name="postgres-17",
xdist_postgres_isolate=xdist_postgres_isolation_level,
host=postgres_host,
user=postgres_user,
password=postgres_password,
) as service:
Expand Down Expand Up @@ -344,6 +366,7 @@ def postgres_service(
docker_service: DockerService,
postgres_image: str,
xdist_postgres_isolation_level: XdistIsolationLevel,
postgres_host: str,
postgres_user: str,
postgres_password: str,
) -> Generator[PostgresService, None, None]:
Expand All @@ -352,6 +375,7 @@ def postgres_service(
image=postgres_image,
name="postgres",
xdist_postgres_isolate=xdist_postgres_isolation_level,
host=postgres_host,
user=postgres_user,
password=postgres_password,
) as service:
Expand Down Expand Up @@ -384,6 +408,7 @@ def pgvector_service(
docker_service: DockerService,
pgvector_image: str,
xdist_postgres_isolation_level: XdistIsolationLevel,
postgres_host: str,
postgres_user: str,
postgres_password: str,
) -> Generator[PostgresService, None, None]:
Expand All @@ -392,6 +417,7 @@ def pgvector_service(
image=pgvector_image,
name="pgvector",
xdist_postgres_isolate=xdist_postgres_isolation_level,
host=postgres_host,
user=postgres_user,
password=postgres_password,
) as service:
Expand Down Expand Up @@ -424,6 +450,7 @@ def alloydb_omni_service(
docker_service: DockerService,
pgvector_image: str,
xdist_postgres_isolation_level: XdistIsolationLevel,
postgres_host: str,
postgres_user: str,
postgres_password: str,
) -> Generator[PostgresService, None, None]:
Expand All @@ -432,6 +459,7 @@ def alloydb_omni_service(
image=pgvector_image,
name="alloydb-omni",
xdist_postgres_isolate=xdist_postgres_isolation_level,
host=postgres_host,
user=postgres_user,
password=postgres_password,
) as service:
Expand Down
Loading