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

Postgres database container.

To get a URL without a driver, pass in driver=None.

Example

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

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

>>> with PostgresContainer("postgres:16") 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 16...'