Skip to content

fix(postgres): Use with_bind_ports for postgres container #853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion modules/postgres/testcontainers/postgres/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions modules/postgres/tests/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -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