diff --git a/modules/postgres/testcontainers/postgres/__init__.py b/modules/postgres/testcontainers/postgres/__init__.py index bde21d5b3..1a368442a 100644 --- a/modules/postgres/testcontainers/postgres/__init__.py +++ b/modules/postgres/testcontainers/postgres/__init__.py @@ -63,7 +63,7 @@ def __init__( self.port = port self.driver = f"+{driver}" if driver else "" - self.with_exposed_ports(self.port) + self.with_bind_ports("5432/tcp", self.port) def _configure(self) -> None: self.with_env("POSTGRES_USER", self.username) diff --git a/modules/postgres/tests/test_postgres.py b/modules/postgres/tests/test_postgres.py index 93b99d25f..ff0573e4a 100644 --- a/modules/postgres/tests/test_postgres.py +++ b/modules/postgres/tests/test_postgres.py @@ -151,3 +151,14 @@ def test_psycopg_versions(): with engine.begin() as connection: result = connection.execute(sqlalchemy.text("SELECT 1 as test")) assert result.scalar() == 1 + + +def test_port_binding(): + """Test that binding to a different port works as expected.""" + + postgres_container = PostgresContainer("postgres:16-alpine", port=12345) + with postgres_container as postgres: + engine = sqlalchemy.create_engine(postgres.get_connection_url()) + with engine.begin() as connection: + result = connection.execute(sqlalchemy.text("SELECT 1 as test")) + assert result.scalar() == 1