class PostgresContainer(image: str = 'postgres:latest', port: int = 5432, user: str | None = None, password: str | None = None, dbname: str | None = None, driver: str = 'psycopg2', **kwargs)ΒΆ

Postgres database container.

Example

The example spins up a Postgres database and connects to it using the psycopg driver.

>>> from testcontainers.postgres import PostgresContainer
>>> import sqlalchemy

>>> postgres_container = PostgresContainer("postgres:9.5")
>>> with postgres_container as postgres:
...     engine = sqlalchemy.create_engine(postgres.get_connection_url())
...     with engine.begin() as connection:
...         result = connection.execute(sqlalchemy.text("select version()"))
...         version, = result.fetchone()
>>> version
'PostgreSQL 9.5...'