PostgreSQL

Integration with PostgreSQL using the PostgreSQL Docker Image or pgvector Docker Image

Installation

pip install pytest-databases[postgres]

Usage Example

import psycopg
from pytest_databases.docker.postgres import PostgresService

pytest_plugins = ["pytest_databases.docker.postgres"]

def test(postgres_service: PostgresService) -> None:
    with psycopg.connect(
        f"postgresql://{postgres_service.user}:{postgres_service.password}@{postgres_service.host}:{postgres_service.port}/{postgres_service.database}"
    ) as conn:
        db_open = conn.execute("SELECT 1").fetchone()
        assert db_open is not None and db_open[0] == 1

def test(postgres_connection: psycopg.Connection) -> None:
    postgres_connection.execute("CREATE TABLE if not exists simple_table as SELECT 1")
    result = postgres_connection.execute("select * from simple_table").fetchone()
    assert result is not None and result[0] == 1

Available Fixtures

  • postgres_image: The Docker image to use for PostgreSQL.

  • postgres_service: A fixture that provides a PostgreSQL service.

  • postgres_connection: A fixture that provides a PostgreSQL connection.

The following version-specific fixtures are also available:

  • postgres_11_image, postgres_11_service, postgres_11_connection: PostgreSQL 11.x

  • postgres_12_image, postgres_12_service, postgres_12_connection: PostgreSQL 12.x

  • postgres_13_image, postgres_13_service, postgres_13_connection: PostgreSQL 13.x

  • postgres_14_image, postgres_14_service, postgres_14_connection: PostgreSQL 14.x

  • postgres_15_image, postgres_15_service, postgres_15_connection: PostgreSQL 15.x

  • postgres_16_image, postgres_16_service, postgres_16_connection: PostgreSQL 16.x

  • postgres_17_image, postgres_17_service, postgres_17_connection: PostgreSQL 17.x

  • pgvector_image: The Docker image to use for pgvector.

  • pgvector_service: A fixture that provides a pgvector service.

  • pgvector_connection: A fixture that provides a pgvector connection.

Service API

pytest_databases.docker.postgres.xdist_postgres_isolation_level()[source]
Return type:

Literal['database', 'server']

class pytest_databases.docker.postgres.PostgresService(host, port, database, password, user)[source]

Bases: ServiceContainer

database: str
password: str
user: str
__init__(host, port, database, password, user)
pytest_databases.docker.postgres.postgres_11_service(docker_service, xdist_postgres_isolation_level)[source]
Return type:

Generator[PostgresService, None, None]

pytest_databases.docker.postgres.postgres_12_service(docker_service, xdist_postgres_isolation_level)[source]
Return type:

Generator[PostgresService, None, None]

pytest_databases.docker.postgres.postgres_13_service(docker_service, xdist_postgres_isolation_level)[source]
Return type:

Generator[PostgresService, None, None]

pytest_databases.docker.postgres.postgres_14_service(docker_service, xdist_postgres_isolation_level)[source]
Return type:

Generator[PostgresService, None, None]

pytest_databases.docker.postgres.postgres_15_service(docker_service, xdist_postgres_isolation_level)[source]
Return type:

Generator[PostgresService, None, None]

pytest_databases.docker.postgres.postgres_16_service(docker_service, xdist_postgres_isolation_level)[source]
Return type:

Generator[PostgresService, None, None]

pytest_databases.docker.postgres.postgres_17_service(docker_service, xdist_postgres_isolation_level)[source]
Return type:

Generator[PostgresService, None, None]

pytest_databases.docker.postgres.postgres_11_connection(postgres_11_service)[source]
Return type:

Generator[Connection, None, None]

pytest_databases.docker.postgres.postgres_12_connection(postgres_12_service)[source]
Return type:

Generator[Connection, None, None]

pytest_databases.docker.postgres.postgres_13_connection(postgres_13_service)[source]
Return type:

Generator[Connection, None, None]

pytest_databases.docker.postgres.postgres_14_connection(postgres_14_service)[source]
Return type:

Generator[Connection, None, None]

pytest_databases.docker.postgres.postgres_15_connection(postgres_15_service)[source]
Return type:

Generator[Connection, None, None]

pytest_databases.docker.postgres.postgres_16_connection(postgres_16_service)[source]
Return type:

Generator[Connection, None, None]

pytest_databases.docker.postgres.postgres_17_connection(postgres_17_service)[source]
Return type:

Generator[Connection, None, None]

pytest_databases.docker.postgres.postgres_image()[source]
Return type:

str

pytest_databases.docker.postgres.postgres_service(docker_service, postgres_image, xdist_postgres_isolation_level)[source]
Return type:

Generator[PostgresService, None, None]

pytest_databases.docker.postgres.postgres_connection(postgres_service)[source]
Return type:

Generator[Connection, None, None]

pytest_databases.docker.postgres.pgvector_image()[source]
Return type:

str

pytest_databases.docker.postgres.pgvector_service(docker_service, pgvector_image, xdist_postgres_isolation_level)[source]
Return type:

Generator[PostgresService, None, None]

pytest_databases.docker.postgres.pgvector_connection(pgvector_service)[source]
Return type:

Generator[Connection, None, None]