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.xpostgres_12_image
,postgres_12_service
,postgres_12_connection
: PostgreSQL 12.xpostgres_13_image
,postgres_13_service
,postgres_13_connection
: PostgreSQL 13.xpostgres_14_image
,postgres_14_service
,postgres_14_connection
: PostgreSQL 14.xpostgres_15_image
,postgres_15_service
,postgres_15_connection
: PostgreSQL 15.xpostgres_16_image
,postgres_16_service
,postgres_16_connection
: PostgreSQL 16.xpostgres_17_image
,postgres_17_service
,postgres_17_connection
: PostgreSQL 17.xpgvector_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
- __init__(host, port, database, password, user)¶
- pytest_databases.docker.postgres.postgres_11_service(docker_service, xdist_postgres_isolation_level)[source]¶
- Return type:
- pytest_databases.docker.postgres.postgres_12_service(docker_service, xdist_postgres_isolation_level)[source]¶
- Return type:
- pytest_databases.docker.postgres.postgres_13_service(docker_service, xdist_postgres_isolation_level)[source]¶
- Return type:
- pytest_databases.docker.postgres.postgres_14_service(docker_service, xdist_postgres_isolation_level)[source]¶
- Return type:
- pytest_databases.docker.postgres.postgres_15_service(docker_service, xdist_postgres_isolation_level)[source]¶
- Return type:
- pytest_databases.docker.postgres.postgres_16_service(docker_service, xdist_postgres_isolation_level)[source]¶
- Return type:
- pytest_databases.docker.postgres.postgres_17_service(docker_service, xdist_postgres_isolation_level)[source]¶
- Return type:
- pytest_databases.docker.postgres.postgres_service(docker_service, postgres_image, xdist_postgres_isolation_level)[source]¶
- Return type: