PostgreSQL#
Integration with PostgreSQL using the PostgreSQL Docker Image, Google’s AlloyDB Omni, pgvector Docker Image, or ParadeDB Docker Image.
Installation#
The PostgreSQL service fixtures validate readiness and create per-worker databases using the psql CLI bundled inside the container image, then confirm the published host port accepts TCP connections, so no Python client driver is required to start the service:
pip install pytest-databases
Usage Example#
import psycopg
from pytest_databases.docker.postgres import PostgresService
pytest_plugins = ["pytest_databases.docker.postgres"]
def test_postgres(postgres_service: PostgresService) -> None:
with psycopg.connect(postgres_service.dsn) as conn:
db_open = conn.execute("SELECT 1").fetchone()
assert db_open is not None and db_open[0] == 1
exit_code, output = postgres_service.exec_sql("SELECT current_database()", tuples_only=True, timeout=30.0)
assert exit_code == 0
assert output.strip().decode() == postgres_service.database
def test_template_clone(postgres_service: PostgresService) -> None:
cloned_db = postgres_service.clone_template_database("test_clone_db")
try:
assert cloned_db == "test_clone_db"
finally:
postgres_service.drop_database_force(cloned_db)
Portable Suites Across Docker and Embedded Backends#
Use postgres_any_service typed as PostgresServiceProtocol (with PostgresBackend)
to share tests seamlessly between containerized PostgreSQL and Embedded PostgreSQL:
from pytest_databases import PostgresServiceProtocol
pytest_plugins = ["pytest_databases.docker.postgres"]
def test_portable_postgres(postgres_any_service: PostgresServiceProtocol) -> None:
exit_code, output = postgres_any_service.exec_sql("SELECT 1", tuples_only=True, timeout=30.0)
assert exit_code == 0
assert output.strip() == b"1"
Pass --postgres-backend=embedded, configure postgres_backend = "embedded" in [tool.pytest.ini_options],
or set PYTEST_DATABASES_POSTGRES_BACKEND=embedded (precedence CLI -> ini -> env -> "docker") to run
against local binaries without requesting docker_service or probing Docker or Podman.
Template Database Acceleration#
Suites with heavy migrations or seed data can build the schema once and clone it for each test database. PostgreSQL
creates the clone with CREATE DATABASE ... TEMPLATE, a file-level copy of the template database, so no
migration runs again.
Set xdist_postgres_isolation_level to "template" and return the schema from postgres_template_setup. The
first process to reach the container builds the template under a file lock; every other worker waits and clones it.
The service database (pytest_databases, or pytest_databases_<worker> under pytest-xdist) is a fresh
clone, with or without pytest-xdist. Every PostgreSQL family service (versioned PostgreSQL, pgvector, ParadeDB,
AlloyDB Omni) honors these fixtures.
import pytest
from pytest_databases.docker.postgres import PostgresService
from pytest_databases.types import PostgresXdistIsolationLevel
pytest_plugins = ["pytest_databases.docker.postgres"]
@pytest.fixture(scope="session")
def xdist_postgres_isolation_level() -> PostgresXdistIsolationLevel:
return "template"
@pytest.fixture(scope="session")
def postgres_template_setup() -> list[str]:
return [
"CREATE TABLE users (id serial PRIMARY KEY, name text)",
"INSERT INTO users (name) VALUES ('alice'), ('bob')",
]
def test_worker_database(postgres_service: PostgresService) -> None:
assert postgres_service.database.startswith("pytest_databases")
postgres_template_setup accepts SQL statements, pathlib.Path objects pointing at .sql files, or a callable
that receives a PostgresService pointed at the staging database. Provider
init_sql statements (for example CREATE EXTENSION IF NOT EXISTS vector on pgvector_*_service or AlloyDB
Omni extension DDL) run on the staging template database via ensure_template(..., init_sql=...) prior to user
postgres_template_setup across both containerized and external PostgreSQL endpoints, so clones inherit enabled
extensions without re-running init_sql per worker. The SQL
text, provider init_sql, and file contents are hashed into the template name (pytest_databases_template_<digest>),
so a changed schema builds a new template even when the container is reused with --keep-containers. A callable cannot be
hashed; return its inputs (migration directories, files, or a version string) from
postgres_template_fingerprint_sources so they become part of the name.
The same helpers are available on PostgresService for custom fixtures:
from pytest_databases.docker.postgres import PostgresService
def test_custom_template(postgres_service: PostgresService) -> None:
template = postgres_service.ensure_template(
"app_template",
setup_sql="CREATE TABLE items (id serial PRIMARY KEY)",
)
worker_db = postgres_service.create_worker_database_from_template(template, "items_test")
try:
assert worker_db == "items_test"
finally:
postgres_service.drop_database_force(worker_db)
sweep_worker_databases(prefix) drops every non-template database whose name starts with prefix (_ is
matched literally, *_staging databases are kept). It also drops databases that other workers are still using, so
call it only at the end of a session.
Attaching to an External PostgreSQL Server#
When CI jobs or shared environments pre-start a PostgreSQL or AlloyDB Omni server outside pytest-databases, use
postgres_external_service or provide_external_postgres_service() to attach
without requiring a container runtime, psql, or psycopg. Readiness probes, "database" and "template"
pytest-xdist isolation, and one-shot init_sql bootstrap run directly over the stdlib PostgreSQL v3 wire
protocol (supporting trust, cleartext, MD5, and SCRAM-SHA-256 authentication).
Set POSTGRES_EXTERNAL_HOST (and optionally POSTGRES_EXTERNAL_PORT, POSTGRES_USER, and
POSTGRES_DATABASE) or call provide_external_postgres_service() directly in
a custom fixture. Custom container fixtures calling provide_postgres_service()
can also forward server command arguments (for example preloading pg_stat_statements), extra env
variables, shm_size, mem_limit, volumes, database, timeout, and init_sql.
Available Fixtures#
postgres_host: The PostgreSQL host address (defaults to"127.0.0.1", can be overridden withPOSTGRES_HOSTenvironment variable).postgres_user: The PostgreSQL superuser name (defaults to"postgres", envPOSTGRES_USER).postgres_password: The PostgreSQL superuser password (defaults to"super-secret").postgres_database: Base database name created for family services (defaults to"pytest_databases", envPOSTGRES_DATABASE;<postgres_database>_<worker>underpytest-xdist"database"or"template"isolation).postgres_image: The Docker image to use for PostgreSQL (defaults to"postgres:18").postgres_port: Optional host-side port pin (defaultNone, override viaPOSTGRES_PORTenv).postgres_startup_timeout: Startup readiness timeout in seconds for containerized PostgreSQL services (default60, envPOSTGRES_STARTUP_TIMEOUT).postgres_any_service: Cross-backend session fixture returning aPostgresServiceProtocol(PostgresService,ExternalPostgresService, or lazily importedEmbeddedPostgresServicecontrolled by--postgres-backend/postgres_backend/PYTEST_DATABASES_POSTGRES_BACKEND).postgres_external_host: Optional hostname or IP of a pre-started PostgreSQL endpoint (defaultNone, envPOSTGRES_EXTERNAL_HOST).postgres_external_port: Optional TCP port forpostgres_external_service(defaultNone, falling back to5432; envPOSTGRES_EXTERNAL_PORT).postgres_external_service: Yields anExternalPostgresServiceconnected topostgres_external_host(skips when unset).postgres_template_name: Template name prefix for"template"isolation (Nonemeans"pytest_databases_template").postgres_template_setup: SQL statements,.sqlpaths, or a callable that builds the template (defaultNone).postgres_template_fingerprint_sources: Extra strings, bytes, files, or directories hashed into the template name (default()).postgres_template_lock_dir: Directory for the template lock file (Nonemeans the system temp directory).postgres_template_options: APostgresTemplateOptionscombining the four fixtures above; every family service requests it.postgres_service: A fixture that provides aPostgresServiceinstance (withdsn,connection_config, andexec_sql()).postgres_connection: Deprecated fixture that yields apsycopg.Connection.
The following version-specific fixtures are also available. Each has its own
*_port fixture and matching env var so multiple versions can be pinned in
the same session without colliding:
postgres_11_service,postgres_11_connection,postgres_11_port(env:POSTGRES_11_PORT)postgres_12_service,postgres_12_connection,postgres_12_port(env:POSTGRES_12_PORT)postgres_13_service,postgres_13_connection,postgres_13_port(env:POSTGRES_13_PORT)postgres_14_service,postgres_14_connection,postgres_14_port(env:POSTGRES_14_PORT)postgres_15_service,postgres_15_connection,postgres_15_port(env:POSTGRES_15_PORT)postgres_16_service,postgres_16_connection,postgres_16_port(env:POSTGRES_16_PORT)postgres_17_service,postgres_17_connection,postgres_17_port(env:POSTGRES_17_PORT)postgres_18_service,postgres_18_connection,postgres_18_port(env:POSTGRES_18_PORT)
pgvector#
All pgvector_service and pgvector_13_service through pgvector_18_service fixtures automatically execute
CREATE EXTENSION IF NOT EXISTS vector; during database (or staging template) provisioning so vector types and
operators are ready immediately.
pgvector_image,pgvector_service,pgvector_connection,pgvector_port(env:PGVECTOR_PORT) — default imagepgvector/pgvector:pg18pgvector_13_service,pgvector_13_connection,pgvector_13_port(env:PGVECTOR_13_PORT)pgvector_14_service,pgvector_14_connection,pgvector_14_port(env:PGVECTOR_14_PORT)pgvector_15_service,pgvector_15_connection,pgvector_15_port(env:PGVECTOR_15_PORT)pgvector_16_service,pgvector_16_connection,pgvector_16_port(env:PGVECTOR_16_PORT)pgvector_17_service,pgvector_17_connection,pgvector_17_port(env:PGVECTOR_17_PORT)pgvector_18_service,pgvector_18_connection,pgvector_18_port(env:PGVECTOR_18_PORT)
ParadeDB#
ParadeDB extends PostgreSQL with BM25 full-text search and analytics extensions.
paradedb_image,paradedb_service,paradedb_connection,paradedb_port(env:PARADEDB_PORT) — default imageparadedb/paradedb:latest-pg18paradedb_15_service,paradedb_15_connection,paradedb_15_port(env:PARADEDB_15_PORT)paradedb_16_service,paradedb_16_connection,paradedb_16_port(env:PARADEDB_16_PORT)paradedb_17_service,paradedb_17_connection,paradedb_17_port(env:PARADEDB_17_PORT)paradedb_18_service,paradedb_18_connection,paradedb_18_port(env:PARADEDB_18_PORT)
AlloyDB Omni#
alloydb_omni_service and alloydb_omni_15_service through alloydb_omni_18_service configure shared memory
(alloydb_omni_shm_size = "512m", env ALLOYDB_OMNI_SHM_SIZE), a 90-second readiness timeout
(alloydb_omni_startup_timeout, env ALLOYDB_OMNI_STARTUP_TIMEOUT), postmaster startup GUCs
(alloydb_omni_command via build_alloydb_omni_command() for versions 17
and 18, automatically adapting when alloydb_omni_image resolves to a :15 or :16 tag, and
alloydb_omni_legacy_command for 15 and 16; override via ALLOYDB_OMNI_COMMAND or
ALLOYDB_OMNI_LEGACY_COMMAND), and default extension DDL so google_columnar_engine, pg_stat_statements,
vector, alloydb_scann, google_ml_integration, and alloydb_ai_nl are enabled out of the box across
versions 15–18, with pg_textsearch BM25 indexes enabled additionally on versions 17 and 18. Local
extension DDL and GUC inspection require no cloud credentials, while remote Vertex AI or Gemini model invocations stay
in user SQL.
from pytest_databases.docker.postgres import PostgresService
def test_alloydb_omni_stack(alloydb_omni_service: PostgresService) -> None:
exit_code, output = alloydb_omni_service.exec_sql(
"SHOW google_columnar_engine.enabled;",
tuples_only=True,
)
assert exit_code == 0
assert output.strip() == b"on"
alloydb_omni_shm_size,alloydb_omni_startup_timeout,alloydb_omni_command,alloydb_omni_legacy_commandalloydb_omni_image,alloydb_omni_service,alloydb_omni_connection,alloydb_omni_port(env:ALLOYDB_OMNI_PORT) — default imagegcr.io/alloydb-omni/alloydbomni:18alloydb_omni_15_service,alloydb_omni_15_connection,alloydb_omni_15_port(env:ALLOYDB_OMNI_15_PORT)alloydb_omni_16_service,alloydb_omni_16_connection,alloydb_omni_16_port(env:ALLOYDB_OMNI_16_PORT)alloydb_omni_17_service,alloydb_omni_17_connection,alloydb_omni_17_port(env:ALLOYDB_OMNI_17_PORT)alloydb_omni_18_service,alloydb_omni_18_connection,alloydb_omni_18_port(env:ALLOYDB_OMNI_18_PORT)
Configuration#
PostgreSQL services can be configured using environment variables:
POSTGRES_HOST: The host address for the PostgreSQL container (default:"127.0.0.1")POSTGRES_USER: Superuser name configured in PostgreSQL containers andpostgres_external_service(default:"postgres")POSTGRES_DATABASE: Base database name created for PostgreSQL family services andpostgres_external_service(default:"pytest_databases")POSTGRES_EXTERNAL_HOSTandPOSTGRES_EXTERNAL_PORT: Hostname/IP and optional TCP port (default5432) used bypostgres_external_serviceto attach to an existing serverPOSTGRES_STARTUP_TIMEOUT/ALLOYDB_OMNI_STARTUP_TIMEOUT: Readiness timeouts in seconds for PostgreSQL (60) and AlloyDB Omni (90)ALLOYDB_OMNI_SHM_SIZE/ALLOYDB_OMNI_COMMAND/ALLOYDB_OMNI_LEGACY_COMMAND: Shared memory size ("512m") and postmaster command overrides for AlloyDB OmniPYTEST_DATABASES_POSTGRES_BACKEND: Default backend selector forpostgres_any_service("docker"or"embedded")POSTGRES_PORT/POSTGRES_NN_PORT/PGVECTOR_PORT/PGVECTOR_NN_PORT/PARADEDB_PORT/PARADEDB_NN_PORT/ALLOYDB_OMNI_PORT/ALLOYDB_OMNI_NN_PORT: Pin a specific host-side port for the matching service fixture. When unset, Docker picks a random free port (the default). See GitHub issue #131.
Service API#
PostgreSQL, pgvector, ParadeDB, and AlloyDB Omni container service fixtures.
- class pytest_databases.docker.postgres.ExternalPostgresService(host: str, port: int, database: str, user: str = 'postgres', password: str = '')[source]#
Bases:
PostgresTemplateSupportConnection metadata and SQL/template helpers for a pre-running PostgreSQL endpoint.
Queries and template database management run directly over the stdlib PostgreSQL v3 wire protocol (trust, cleartext, MD5, or SCRAM-SHA-256) with zero container runtime or driver dependencies.
- property dsn: str#
Libpq keyword/value connection string for this endpoint.
- Returns:
Formatted libpq connection string.
- property connection_config: dict[str, Any]#
Driver keyword arguments (
host,port,user,database, and optionalpassword).- Returns:
Dictionary of connection keyword arguments.
- exec_sql(sql: str, *, database: str | None = None, tuples_only: bool = False, timeout: float = 30.0) tuple[int, bytes][source]#
Execute
sqlagainstdatabase(defaults toself.database) over the v3 wire protocol.- Parameters:
sql – SQL statement text to execute.
database – Optional target database override.
tuples_only – Accepted for signature parity with
PostgresService.exec_sql(); wire output is always formatted as unaligned tab-separated tuples matchingpsql -tA.timeout – Socket timeout in seconds.
- Returns:
Tuple of
(exit_code, output_bytes)where0is success,1is a SQL error, and2is a connection or authentication failure.
- class pytest_databases.docker.postgres.PostgresService(container: docker.models.containers.Container, host: str, port: int, database: str, password: str, user: str)[source]#
Bases:
ServiceContainer,PostgresTemplateSupportConnection metadata and lifecycle helpers for a PostgreSQL-compatible container service.
Template helpers (
ensure_template(),create_worker_database_from_template(),sweep_worker_databases(),clone_template_database(),drop_database_force()) runpsqlinside the container.- property dsn: str#
Libpq keyword/value connection string for this service.
- Returns:
Formatted libpq connection string.
- property connection_config: dict[str, Any]#
Driver keyword arguments (
host,port,user,database, and optionalpassword).- Returns:
Dictionary of connection keyword arguments.
- class pytest_databases.docker.postgres.PostgresServiceProtocol(*args, **kwargs)[source]#
Bases:
ProtocolRuntime-checkable structural interface shared by Docker, external, and embedded PostgreSQL services.
- property connection_config: dict[str, Any]#
Driver keyword arguments (
host,port,user,database, and optionalpassword).
- exec_sql(sql: str, *, database: str | None = None, tuples_only: bool = False, timeout: float = 30.0) tuple[int, bytes][source]#
Execute
sqlagainstdatabase(defaults toself.database).
- ensure_template(template_name: str = 'pytest_databases_template', *, setup_fn: Callable[[Self], object] | None = None, setup_sql: str | Path | Sequence[str | Path] | None = None, init_sql: str | Path | Sequence[str | Path] | None = None, fingerprint_sources: Sequence[str | bytes | Path] = (), lock_dir: Path | str | None = None, lock_timeout: float = 180.0) str[source]#
Build an immutable PostgreSQL template database once and return its name.
- create_worker_database_from_template(template_name: str, worker_db: str) str[source]#
Recreate
worker_dbcloned fromtemplate_nameand verify connectivity.
- clone_template_database(target_db: str, *, source_db: str | None = None) str[source]#
Clone
source_db(defaults toself.database) intotarget_db.
- drop_database_force(db_name: str) None[source]#
Terminate active sessions and drop
db_nameif it exists.
- sweep_worker_databases(prefix: str = 'pytest_databases_') list[str][source]#
Drop worker databases matching
prefixwhile preserving templates and active targets.
- __init__(*args, **kwargs)#
- class pytest_databases.docker.postgres.PostgresTemplateOptions(name: str | None = None, setup: Callable[[PostgresService], object] | Callable[[ExternalPostgresService], object] | str | Path | Sequence[str | Path] | None = None, lock_dir: Path | str | None = None, lock_timeout: float = 180.0, fingerprint_sources: Sequence[str | bytes | Path] = ())[source]#
Bases:
objectTemplate database settings used when the isolation level is
"template".nameis the template name prefix (None means"pytest_databases_template").setupis a callable receiving the staging PostgreSQL service, or SQL statements /.sqlfile paths whose text and contents are hashed into the template name.fingerprint_sourcesadds strings, bytes, files, or directories to that digest (use it to version a setup callable).lock_dirholds the cross-worker lock file (None means the system temp directory) andlock_timeoutis the number of seconds to wait for it.- setup: Callable[[PostgresService], object] | Callable[[ExternalPostgresService], object] | str | Path | Sequence[str | Path] | None = None#
- __init__(name: str | None = None, setup: Callable[[PostgresService], object] | Callable[[ExternalPostgresService], object] | str | Path | Sequence[str | Path] | None = None, lock_dir: Path | str | None = None, lock_timeout: float = 180.0, fingerprint_sources: Sequence[str | bytes | Path] = ()) None#
- pytest_databases.docker.postgres.alloydb_omni_15_connection(alloydb_omni_15_service)#
Provide a deprecated psycopg connection to AlloyDB Omni 15.
- pytest_databases.docker.postgres.alloydb_omni_15_port() int | None#
Return an optional pinned host port for AlloyDB Omni 15.
- pytest_databases.docker.postgres.alloydb_omni_15_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, alloydb_omni_15_port, postgres_template_options, alloydb_omni_startup_timeout, alloydb_omni_legacy_command, alloydb_omni_shm_size)#
Provide an AlloyDB Omni 15 service.
- pytest_databases.docker.postgres.alloydb_omni_16_connection(alloydb_omni_16_service)#
Provide a deprecated psycopg connection to AlloyDB Omni 16.
- pytest_databases.docker.postgres.alloydb_omni_16_port() int | None#
Return an optional pinned host port for AlloyDB Omni 16.
- pytest_databases.docker.postgres.alloydb_omni_16_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, alloydb_omni_16_port, postgres_template_options, alloydb_omni_startup_timeout, alloydb_omni_legacy_command, alloydb_omni_shm_size)#
Provide an AlloyDB Omni 16 service.
- pytest_databases.docker.postgres.alloydb_omni_17_connection(alloydb_omni_17_service)#
Provide a deprecated psycopg connection to AlloyDB Omni 17.
- pytest_databases.docker.postgres.alloydb_omni_17_port() int | None#
Return an optional pinned host port for AlloyDB Omni 17.
- pytest_databases.docker.postgres.alloydb_omni_17_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, alloydb_omni_17_port, postgres_template_options, alloydb_omni_startup_timeout, alloydb_omni_command, alloydb_omni_shm_size)#
Provide an AlloyDB Omni 17 service.
- pytest_databases.docker.postgres.alloydb_omni_18_connection(alloydb_omni_18_service)#
Provide a deprecated psycopg connection to AlloyDB Omni 18.
- pytest_databases.docker.postgres.alloydb_omni_18_port() int | None#
Return an optional pinned host port for AlloyDB Omni 18.
- pytest_databases.docker.postgres.alloydb_omni_18_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, alloydb_omni_18_port, postgres_template_options, alloydb_omni_startup_timeout, alloydb_omni_command, alloydb_omni_shm_size)#
Provide an AlloyDB Omni 18 service.
- pytest_databases.docker.postgres.alloydb_omni_command(alloydb_omni_image: str) list[str] | str | None[source]#
Return postmaster command for
alloydb_omni_service, omittingpg_textsearchfor 15/16 tags.
- pytest_databases.docker.postgres.alloydb_omni_connection(alloydb_omni_service)#
Provide a deprecated psycopg connection to the default AlloyDB Omni service.
- pytest_databases.docker.postgres.alloydb_omni_image() str[source]#
Return the default AlloyDB Omni container image tag on Google Container Registry.
- pytest_databases.docker.postgres.alloydb_omni_legacy_command() list[str] | str | None[source]#
Return postmaster command for AlloyDB Omni 15 and 16 containers without
pg_textsearch.
- pytest_databases.docker.postgres.alloydb_omni_port() int | None#
Return an optional pinned host port for the default AlloyDB Omni service.
- pytest_databases.docker.postgres.alloydb_omni_service(docker_service, alloydb_omni_image, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, alloydb_omni_port, postgres_template_options, alloydb_omni_startup_timeout, alloydb_omni_command, alloydb_omni_shm_size)#
Provide the default AlloyDB Omni service.
- pytest_databases.docker.postgres.alloydb_omni_shm_size() str | None[source]#
Return shared-memory size for AlloyDB Omni containers.
- pytest_databases.docker.postgres.alloydb_omni_startup_timeout() int[source]#
Return startup readiness timeout in seconds for AlloyDB Omni containers.
- pytest_databases.docker.postgres.build_alloydb_omni_command(*, include_textsearch: bool = True, extra_gucs: Mapping[str, str] | None = None) list[str][source]#
Return PostgreSQL postmaster args enabling verified AlloyDB Omni 15-18 columnar, BM25, ScaNN, and AI GUCs.
- pytest_databases.docker.postgres.clone_template_database(service: PostgresService, *, target_db: str, source_db: str | None = None) str[source]#
Clone
source_dbintotarget_dbusing PostgreSQLCREATE DATABASE ... TEMPLATE.- Returns:
The name of the created database.
- pytest_databases.docker.postgres.create_worker_database(container: docker.models.containers.Container, *, user: str, password: str, db_name: str) None[source]#
Create and verify a PostgreSQL database inside the container.
- Raises:
RuntimeError – If
CREATE DATABASEkeeps failing or the verification query fails.
- pytest_databases.docker.postgres.drop_database_force(service: PostgresService, *, db_name: str) None[source]#
Terminate active backends and drop
db_nameif it exists.
- pytest_databases.docker.postgres.exec_psql(container: docker.models.containers.Container, *, user: str, password: str, database: str, sql: str, tuples_only: bool = False) tuple[int, bytes][source]#
Execute a SQL statement via
psqlover the container TCP loopback interface.- Returns:
The
psqlexit code and output.
- pytest_databases.docker.postgres.make_connection_string(host: str, port: int, user: str, password: str, database: str) str[source]#
Format a libpq keyword/value connection string.
Values containing whitespace, quotes, or backslashes are single-quoted and escaped; empty values are omitted.
- Returns:
The connection string.
- pytest_databases.docker.postgres.paradedb_15_connection(paradedb_15_service)#
Provide a deprecated psycopg connection to ParadeDB PG15.
- pytest_databases.docker.postgres.paradedb_15_port() int | None#
Return an optional pinned host port for ParadeDB PG15.
- pytest_databases.docker.postgres.paradedb_15_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, paradedb_15_port, postgres_template_options, postgres_startup_timeout)#
Provide a ParadeDB PG15 service.
- pytest_databases.docker.postgres.paradedb_16_connection(paradedb_16_service)#
Provide a deprecated psycopg connection to ParadeDB PG16.
- pytest_databases.docker.postgres.paradedb_16_port() int | None#
Return an optional pinned host port for ParadeDB PG16.
- pytest_databases.docker.postgres.paradedb_16_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, paradedb_16_port, postgres_template_options, postgres_startup_timeout)#
Provide a ParadeDB PG16 service.
- pytest_databases.docker.postgres.paradedb_17_connection(paradedb_17_service)#
Provide a deprecated psycopg connection to ParadeDB PG17.
- pytest_databases.docker.postgres.paradedb_17_port() int | None#
Return an optional pinned host port for ParadeDB PG17.
- pytest_databases.docker.postgres.paradedb_17_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, paradedb_17_port, postgres_template_options, postgres_startup_timeout)#
Provide a ParadeDB PG17 service.
- pytest_databases.docker.postgres.paradedb_18_connection(paradedb_18_service)#
Provide a deprecated psycopg connection to ParadeDB PG18.
- pytest_databases.docker.postgres.paradedb_18_port() int | None#
Return an optional pinned host port for ParadeDB PG18.
- pytest_databases.docker.postgres.paradedb_18_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, paradedb_18_port, postgres_template_options, postgres_startup_timeout)#
Provide a ParadeDB PG18 service.
- pytest_databases.docker.postgres.paradedb_connection(paradedb_service)#
Provide a deprecated psycopg connection to the default ParadeDB service.
- pytest_databases.docker.postgres.paradedb_image() str[source]#
Return the default ParadeDB container image tag.
- pytest_databases.docker.postgres.paradedb_port() int | None#
Return an optional pinned host port for the default ParadeDB service.
- pytest_databases.docker.postgres.paradedb_service(docker_service, paradedb_image, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, paradedb_port, postgres_template_options, postgres_startup_timeout)#
Provide the default ParadeDB service.
- pytest_databases.docker.postgres.pgvector_13_connection(pgvector_13_service)#
Provide a deprecated psycopg connection to pgvector PG13.
- pytest_databases.docker.postgres.pgvector_13_port() int | None#
Return an optional pinned host port for pgvector PG13.
- pytest_databases.docker.postgres.pgvector_13_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, pgvector_13_port, postgres_template_options, postgres_startup_timeout)#
Provide a pgvector PG13 service.
- pytest_databases.docker.postgres.pgvector_14_connection(pgvector_14_service)#
Provide a deprecated psycopg connection to pgvector PG14.
- pytest_databases.docker.postgres.pgvector_14_port() int | None#
Return an optional pinned host port for pgvector PG14.
- pytest_databases.docker.postgres.pgvector_14_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, pgvector_14_port, postgres_template_options, postgres_startup_timeout)#
Provide a pgvector PG14 service.
- pytest_databases.docker.postgres.pgvector_15_connection(pgvector_15_service)#
Provide a deprecated psycopg connection to pgvector PG15.
- pytest_databases.docker.postgres.pgvector_15_port() int | None#
Return an optional pinned host port for pgvector PG15.
- pytest_databases.docker.postgres.pgvector_15_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, pgvector_15_port, postgres_template_options, postgres_startup_timeout)#
Provide a pgvector PG15 service.
- pytest_databases.docker.postgres.pgvector_16_connection(pgvector_16_service)#
Provide a deprecated psycopg connection to pgvector PG16.
- pytest_databases.docker.postgres.pgvector_16_port() int | None#
Return an optional pinned host port for pgvector PG16.
- pytest_databases.docker.postgres.pgvector_16_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, pgvector_16_port, postgres_template_options, postgres_startup_timeout)#
Provide a pgvector PG16 service.
- pytest_databases.docker.postgres.pgvector_17_connection(pgvector_17_service)#
Provide a deprecated psycopg connection to pgvector PG17.
- pytest_databases.docker.postgres.pgvector_17_port() int | None#
Return an optional pinned host port for pgvector PG17.
- pytest_databases.docker.postgres.pgvector_17_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, pgvector_17_port, postgres_template_options, postgres_startup_timeout)#
Provide a pgvector PG17 service.
- pytest_databases.docker.postgres.pgvector_18_connection(pgvector_18_service)#
Provide a deprecated psycopg connection to pgvector PG18.
- pytest_databases.docker.postgres.pgvector_18_port() int | None#
Return an optional pinned host port for pgvector PG18.
- pytest_databases.docker.postgres.pgvector_18_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, pgvector_18_port, postgres_template_options, postgres_startup_timeout)#
Provide a pgvector PG18 service.
- pytest_databases.docker.postgres.pgvector_connection(pgvector_service)#
Provide a deprecated psycopg connection to the default pgvector service.
- pytest_databases.docker.postgres.pgvector_image() str[source]#
Return the default pgvector container image tag.
- pytest_databases.docker.postgres.pgvector_port() int | None#
Return an optional pinned host port for the default pgvector service.
- pytest_databases.docker.postgres.pgvector_service(docker_service, pgvector_image, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, pgvector_port, postgres_template_options, postgres_startup_timeout)#
Provide the default pgvector service.
- pytest_databases.docker.postgres.postgres_11_connection(postgres_11_service)#
Provide a deprecated psycopg connection to PostgreSQL 11.
- pytest_databases.docker.postgres.postgres_11_port() int | None#
Return an optional pinned host port for PostgreSQL 11.
- pytest_databases.docker.postgres.postgres_11_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, postgres_11_port, postgres_template_options, postgres_startup_timeout)#
Provide a PostgreSQL 11 service.
- pytest_databases.docker.postgres.postgres_12_connection(postgres_12_service)#
Provide a deprecated psycopg connection to PostgreSQL 12.
- pytest_databases.docker.postgres.postgres_12_port() int | None#
Return an optional pinned host port for PostgreSQL 12.
- pytest_databases.docker.postgres.postgres_12_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, postgres_12_port, postgres_template_options, postgres_startup_timeout)#
Provide a PostgreSQL 12 service.
- pytest_databases.docker.postgres.postgres_13_connection(postgres_13_service)#
Provide a deprecated psycopg connection to PostgreSQL 13.
- pytest_databases.docker.postgres.postgres_13_port() int | None#
Return an optional pinned host port for PostgreSQL 13.
- pytest_databases.docker.postgres.postgres_13_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, postgres_13_port, postgres_template_options, postgres_startup_timeout)#
Provide a PostgreSQL 13 service.
- pytest_databases.docker.postgres.postgres_14_connection(postgres_14_service)#
Provide a deprecated psycopg connection to PostgreSQL 14.
- pytest_databases.docker.postgres.postgres_14_port() int | None#
Return an optional pinned host port for PostgreSQL 14.
- pytest_databases.docker.postgres.postgres_14_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, postgres_14_port, postgres_template_options, postgres_startup_timeout)#
Provide a PostgreSQL 14 service.
- pytest_databases.docker.postgres.postgres_15_connection(postgres_15_service)#
Provide a deprecated psycopg connection to PostgreSQL 15.
- pytest_databases.docker.postgres.postgres_15_port() int | None#
Return an optional pinned host port for PostgreSQL 15.
- pytest_databases.docker.postgres.postgres_15_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, postgres_15_port, postgres_template_options, postgres_startup_timeout)#
Provide a PostgreSQL 15 service.
- pytest_databases.docker.postgres.postgres_16_connection(postgres_16_service)#
Provide a deprecated psycopg connection to PostgreSQL 16.
- pytest_databases.docker.postgres.postgres_16_port() int | None#
Return an optional pinned host port for PostgreSQL 16.
- pytest_databases.docker.postgres.postgres_16_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, postgres_16_port, postgres_template_options, postgres_startup_timeout)#
Provide a PostgreSQL 16 service.
- pytest_databases.docker.postgres.postgres_17_connection(postgres_17_service)#
Provide a deprecated psycopg connection to PostgreSQL 17.
- pytest_databases.docker.postgres.postgres_17_port() int | None#
Return an optional pinned host port for PostgreSQL 17.
- pytest_databases.docker.postgres.postgres_17_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, postgres_17_port, postgres_template_options, postgres_startup_timeout)#
Provide a PostgreSQL 17 service.
- pytest_databases.docker.postgres.postgres_18_connection(postgres_18_service)#
Provide a deprecated psycopg connection to PostgreSQL 18.
- pytest_databases.docker.postgres.postgres_18_port() int | None#
Return an optional pinned host port for PostgreSQL 18.
- pytest_databases.docker.postgres.postgres_18_service(docker_service, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, postgres_18_port, postgres_template_options, postgres_startup_timeout)#
Provide a PostgreSQL 18 service.
- pytest_databases.docker.postgres.postgres_any_service(request: pytest.FixtureRequest, tmp_path_factory: pytest.TempPathFactory) Generator[PostgresServiceProtocol, None, None][source]#
Provide a session-scoped Docker or embedded PostgreSQL service selected via
--postgres-backend.- Yields:
Active PostgreSQL service conforming to
PostgresServiceProtocol.
- pytest_databases.docker.postgres.postgres_connection(postgres_service)#
Provide a deprecated psycopg connection to the default PostgreSQL service.
- pytest_databases.docker.postgres.postgres_database() str[source]#
Return the default database name for PostgreSQL services.
- pytest_databases.docker.postgres.postgres_external_host() str | None[source]#
Return an optional pre-started external PostgreSQL hostname or IP from
POSTGRES_EXTERNAL_HOST.
- pytest_databases.docker.postgres.postgres_external_port() int | None#
Return an optional port for a pre-started external PostgreSQL service (defaults to 5432).
- pytest_databases.docker.postgres.postgres_external_service(postgres_external_host: str | None, postgres_external_port: int | None, postgres_user: str, postgres_password: str, postgres_database: str, xdist_postgres_isolation_level: PostgresXdistIsolationLevel, postgres_template_options: PostgresTemplateOptions, postgres_startup_timeout: int) Generator[ExternalPostgresService, None, None][source]#
Attach to a pre-started PostgreSQL service configured via
POSTGRES_EXTERNAL_HOST.- Yields:
Configured
ExternalPostgresServicetargeting the isolated database.
- pytest_databases.docker.postgres.postgres_host() str[source]#
Return the host address for PostgreSQL containers.
- pytest_databases.docker.postgres.postgres_image() str[source]#
Return the default PostgreSQL container image tag.
- pytest_databases.docker.postgres.postgres_password() str[source]#
Return the default superuser password for PostgreSQL containers.
- pytest_databases.docker.postgres.postgres_port() int | None#
Return an optional pinned host port for the default PostgreSQL container.
- pytest_databases.docker.postgres.postgres_service(docker_service, postgres_image, xdist_postgres_isolation_level, postgres_host, postgres_user, postgres_password, postgres_database, postgres_port, postgres_template_options, postgres_startup_timeout)#
Provide the default PostgreSQL service.
- pytest_databases.docker.postgres.postgres_startup_timeout() int[source]#
Return startup readiness timeout in seconds for PostgreSQL containers.
- pytest_databases.docker.postgres.postgres_template_fingerprint_sources() Sequence[str | bytes | Path][source]#
Return extra strings, bytes, files, or directories folded into the template name digest.
- pytest_databases.docker.postgres.postgres_template_lock_dir() Path | str | None[source]#
Return the directory for template lock files (None means the system temp directory).
- pytest_databases.docker.postgres.postgres_template_name() str | None[source]#
Return the template name prefix for
"template"isolation (None means"pytest_databases_template").
- pytest_databases.docker.postgres.postgres_template_options(postgres_template_name: str | None, postgres_template_setup: Callable[[PostgresService], object] | str | Path | Sequence[str | Path] | None, postgres_template_lock_dir: Path | str | None, postgres_template_fingerprint_sources: Sequence[str | bytes | Path]) PostgresTemplateOptions[source]#
Combine the
postgres_template_*fixtures; every PostgreSQL family service uses it.
- pytest_databases.docker.postgres.postgres_template_setup() Callable[[PostgresService], object] | str | Path | Sequence[str | Path] | None[source]#
Return the template setup: a callable receiving the staging service, or SQL statements /
.sqlpaths.
- pytest_databases.docker.postgres.postgres_user() str[source]#
Return the default superuser name for PostgreSQL containers.
- pytest_databases.docker.postgres.provide_default_postgres_session(request: pytest.FixtureRequest) Generator[PostgresService | ExternalPostgresService, None, None][source]#
Yield
postgres_serviceif registered, or provision a Docker/external PostgreSQL session fallback.- Yields:
Active
PostgresServiceorExternalPostgresService.
- pytest_databases.docker.postgres.provide_external_postgres_service(host: str, port: int = 5432, *, user: str = 'postgres', password: str = '', database: str = 'pytest_databases', maintenance_database: str = 'postgres', xdist_postgres_isolate: PostgresXdistIsolationLevel = 'database', template: PostgresTemplateOptions | None = None, init_sql: str | Path | Sequence[str | Path] | None = None, lock_dir: Path | str | None = None, timeout: float = 60.0, pause: float = 0.5) Generator[ExternalPostgresService, None, None][source]#
Attach to a pre-started external PostgreSQL endpoint with clientless wire readiness and isolation.
Uses the stdlib PostgreSQL v3 wire client (trust, cleartext, MD5, and SCRAM-SHA-256) to wait for readiness, create or clone per-worker databases, and execute optional
init_sqlscripts without requiring a container runtime,psql, orpsycopg.- Parameters:
host – External PostgreSQL server hostname or IP address.
port – External PostgreSQL server TCP port.
user – Role name used for authentication and database setup.
password – Optional role password (empty string for
trustauthentication).database – Base database name (suffixed
_<worker>underpytest-xdist).maintenance_database – Existing database probed for readiness and
CREATE DATABASEstatements.xdist_postgres_isolate –
"database"or"template"("server"is rejected on fixed endpoints).template – Optional template database configuration when
xdist_postgres_isolate="template".init_sql – Optional SQL statement,
.sqlfile path, or sequence of statements/paths applied once.lock_dir – Optional directory for cross-worker coordination lock and marker files.
timeout – Seconds to wait for the external server to accept queries.
pause – Seconds between readiness probe attempts.
- Yields:
Configured
ExternalPostgresServicetargeting the isolated database.- Raises:
ValueError – If
useris empty,xdist_postgres_isolate="server", or timing values are non-positive.TimeoutError – If the external server does not answer
SELECT 1withintimeoutseconds.RuntimeError – If database creation, template setup, or
init_sqlfails.
- pytest_databases.docker.postgres.provide_postgres_connection(service: PostgresService | ExternalPostgresService, *, fixture_name: str) Generator[Connection[Any], None, None][source]#
Emit a deprecation warning and yield a
psycopg.Connectionforservice.- Yields:
An open
psycopg.Connectionconnected toservice.
- pytest_databases.docker.postgres.provide_postgres_service(docker_service: DockerService, image: str, name: str, host: str, user: str, password: str, xdist_postgres_isolate: PostgresXdistIsolationLevel, host_port: int | None = None, timeout: int = 60, pause: float = 0.5, template: PostgresTemplateOptions | None = None, *, database: str = 'pytest_databases', command: str | list[str] | None = None, env: Mapping[str, str] | None = None, shm_size: int | str | None = None, mem_limit: str | None = None, volumes: dict[str, dict[str, str]] | list[str] | None = None, init_sql: str | Path | Sequence[str | Path] | None = None) Generator[PostgresService, None, None][source]#
Start and validate a PostgreSQL-compatible container service.
Readiness runs
SELECT 1throughpsqlinside the container againstpostgresand then opens a TCP connection to the published host port.POSTGRES_USERandPOSTGRES_DBare forwarded into the container environment so custom superuser names and database names initialize cleanly across officialpostgres,pgvector,paradedb, andalloydbomniimages.With
xdist_postgres_isolate="template"the template described bytemplateis built once per container and the service database (<database>or<database>_<worker>) is cloned from it. Anyinit_sqlstatements or.sqlfiles run deterministically once against the provisioned database under a cross-worker file lock.- Yields:
A configured
PostgresServiceinstance.