Skip to content
Back to skills

Pytest Databases

ASecurity

Use when working with pytest_databases (v0.20.0b3+), Docker/Podman or embedded DB fixtures, PostgreSQL/pgvector/ParadeDB/AlloyDB Omni 18, MySQL/MariaDB/Dolt/Doltgres, Oracle 26ai/ADB-Free/SQL Server/Db2/Sybase/SQL Anywhere/Teradata, CockroachDB/YugabyteDB, MongoDB, Redis/Dragonfly/KeyDB/Valkey, Elasticsearch, BigQuery/Spanner/PubSub, GizmoSQL/Hardcharger/Snowflake, MinIO/RustFS/Azure Blob, Redpanda/RabbitMQ, LocalStack, Floci, or Cloudflare Miniflare. Not for mocked DBs.

  • 19 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 6, 2026
ai-agentspythonrustgobashsqlrailsdockerawsgcpazure

Works with

  • cli

Security analysis

A100/100

Pro scans all 6 files and shows the line behind each finding

Scanned December 6, 2026

npx -y skills add litestar-org/litestar-skills --skill pytest-databases --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Pytest Databases?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Pytest Databases
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/litestar-org-pytest-databases-litestar-skills/badge)](https://www.skillsdirectory.com/skills/litestar-org-pytest-databases-litestar-skills)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: pytest-databases
description: "Use when working with pytest_databases (v0.20.0b3+), Docker/Podman or embedded DB fixtures, PostgreSQL/pgvector/ParadeDB/AlloyDB Omni 18, MySQL/MariaDB/Dolt/Doltgres, Oracle 26ai/ADB-Free/SQL Server/Db2/Sybase/SQL Anywhere/Teradata, CockroachDB/YugabyteDB, MongoDB, Redis/Dragonfly/KeyDB/Valkey, Elasticsearch, BigQuery/Spanner/PubSub, GizmoSQL/Hardcharger/Snowflake, MinIO/RustFS/Azure Blob, Redpanda/RabbitMQ, LocalStack, Floci, or Cloudflare Miniflare. Not for mocked DBs."
---

# pytest-databases

`pytest-databases` (`>=0.20.0b3`) provides session-scoped, clientless container and embedded database/broker/emulator fixtures across 32 plugin modules (31 `pytest_databases.docker.*` modules + `pytest_databases.embedded.postgres`). Load only the modules a test suite uses and connect with the project's own driver via `*_service` coordinates (`v0.19.0` `*_connection` / `*_client` fixtures remain as lazy-import `DeprecationWarning` shims).

## Code Style Rules

- Prefer `*_service` fixtures and build the project's native driver (`asyncpg`, `psycopg`, `sqlspec`, `sqlalchemy`) from service coordinates (`host`, `port`, `user`, `password`, `database`).
- Type fixtures with the service class exported from the same module (`PostgresService`, `RedisService`, `MinioService`, `FlociGCPService`).
- Declare `pytest_plugins` in root `conftest.py` or a dedicated fixture module; load only needed backends, and never pull `*_service` into unconditional `autouse=True` fixtures so unit-only runs stay Docker-free.
- Name custom wrapper fixtures distinctly (for example `custom_alloydb_omni_service`) when declaring `pytest_plugins = ("pytest_databases.docker.postgres",)` in the same module so child plugin loading never shadows the wrapper.

## Quick Reference

### Install and Enable

```bash
uv add --group test --prerelease=allow "pytest-databases>=0.20.0b3"
```

Note: on PyPI final (`0.19.0`), only 18 `docker.*` modules ship and clientless `embedded.postgres`, `provide_postgres_service`, `alloydb_omni_18_service`, `oracle_26ai_service`, and `floci_*` require `>=0.20.0b3`.

```python
pytest_plugins = ["pytest_databases.docker.postgres"]
```

Core entry point fixtures and CLI flags:

- `container_runtime` (`ContainerRuntimeConfig`), `container_client` / `docker_client` (`DockerClient`), `container_service` / `docker_service` (`ContainerService` / `DockerService`)
- `--container-runtime={auto,docker,podman}`, `--keep-containers`, `--postgres-backend={docker,embedded}`

### Fixture Selection

| Need | Use |
| --- | --- |
| Service coordinates for your own driver | `postgres_service`, `alloydb_omni_service`, `redis_service`, `oracle_26ai_service`, `floci_gcp_service`, etc. |
| Specific PostgreSQL-family version | `postgres_11_service`–`postgres_18_service`, `pgvector_13_service`–`pgvector_18_service`, `paradedb_15_service`–`paradedb_18_service`, `alloydb_omni_15_service`–`alloydb_omni_18_service` |
| Switchable Docker vs embedded Postgres | `postgres_any_service` (`--postgres-backend`, `postgres_backend` ini, or `PYTEST_DATABASES_POSTGRES_BACKEND`) |
| Host-process Postgres (no container) | `pytest_databases.embedded.postgres` (`embedded_postgres_service`, `EmbeddedPostgresService`) |
| Custom Postgres / AlloyDB Omni options | `provide_postgres_service(...)` or override `alloydb_omni_command` (`build_alloydb_omni_command(extra_gucs=...)`) |
| Parallel xdist isolation | `xdist_*_isolation_level` (`"database"`, `"server"`, or PostgreSQL `"template"`) — see [xdist.md](references/xdist.md) |

See [reference.md](references/reference.md) for the complete 32-module matrix.

<workflow>

## Workflow

1. Add `"pytest-databases>=0.20.0b3"` (with `--prerelease=allow`) to the `test` group.
2. Register only the required `pytest_databases.docker.<module>` (or `pytest_databases.embedded.postgres`) entries in `pytest_plugins`.
3. Inject the typed `*_service` fixture and pass `host`, `port`, `user`, `password`, and `database` into the app's DB config.
4. Override session-scoped config fixtures (`*_image`, `*_port`, `*_password`, `alloydb_omni_command`, `postgres_template_options`) in `conftest.py`; see [config.md](references/config.md).
5. Under `pytest-xdist`, use `"database"` (default), `"template"` for fast migrated PostgreSQL clones, or `"server"` for per-worker containers.

</workflow>

<guardrails>

## Guardrails

- **Never start manual compose/dev containers (`make start-infra`) before pytest.** Reason: `pytest-databases` manages ephemeral containers, loopback `127.0.0.1` (`resolve_service_host` / `DOCKER_HOST`) binding, and orphan cleanup automatically.
- **Use `provide_postgres_service` instead of hand-rolling `docker_service.run` + `exec_psql`.** Reason: `provide_postgres_service` handles readiness probes, xdist worker DB creation, `shm_size`, `init_sql`, and template cloning cleanly.
- **AlloyDB Omni 18 defaults**: `alloydb_omni_service`, `alloydb_omni_17_service`, and `alloydb_omni_18_service` preload `pg_textsearch`, set `shm_size="512m"`, and run `ALLOYDB_OMNI_INIT_SQL`. AlloyDB Omni 15/16 use `alloydb_omni_legacy_command`.
- **Use exact `pytest_databases.docker.*` module names**: AlloyDB Omni, pgvector, and ParadeDB live in `docker.postgres`; Dragonfly/KeyDB in `docker.redis`; Oracle 26ai/18c/ADB-Free in `docker.oracle`; Azurite in `docker.azure_blob`; Elasticsearch 7/8/9 in `docker.elastic_search`; Floci AWS/GCP/Azure/OCI in `docker.floci`.
- **Use `PYTEST_DATABASES_NAMESPACE` for multi-worktree isolation.** Reason: `ContainerService` labels containers by `rootpath` digest plus `PYTEST_DATABASES_NAMESPACE` and uses advisory OS `FileLock` leases to avoid cross-session collisions.

</guardrails>

<validation>

## Validation Checkpoint

- [ ] Installed version is `pytest-databases>=0.20.0b3` (clientless 32-module layout)
- [ ] `pytest_plugins` registers only required modules from [reference.md](references/reference.md)
- [ ] Service fixtures are typed with their matching `*Service` class and never forced via `autouse=True`
- [ ] Custom Postgres/AlloyDB fixtures use `provide_postgres_service(...)` with a distinct fixture name
- [ ] Xdist and config overrides match [xdist.md](references/xdist.md) and [config.md](references/config.md)

</validation>

<example>

## Example: Customizing AlloyDB Omni 18 with `provide_postgres_service`

```python
from collections.abc import Generator

import pytest
from pytest_databases import DockerService
from pytest_databases.docker.postgres import (
    ALLOYDB_OMNI_INIT_SQL,
    PostgresService,
    PostgresXdistIsolationLevel,
    build_alloydb_omni_command,
    provide_postgres_service,
)

pytest_plugins = ("pytest_databases.docker.postgres",)

CUSTOM_ALLOYDB_COMMAND = build_alloydb_omni_command(
    extra_gucs={
        "google_columnar_engine.enable_auto_columnarization": "off",
        "max_connections": "500",
    }
)


@pytest.fixture(scope="session")
def custom_alloydb_omni_service(
    docker_service: DockerService,
    alloydb_omni_image: str,
    postgres_host: str,
    postgres_user: str,
    postgres_password: str,
    alloydb_omni_port: int | None,
    xdist_postgres_isolate: PostgresXdistIsolationLevel,
) -> Generator[PostgresService, None, None]:
    yield from provide_postgres_service(
        docker_service=docker_service,
        image=alloydb_omni_image,
        name="alloydb-omni-custom",
        host=postgres_host,
        user=postgres_user,
        password=postgres_password,
        xdist_postgres_isolate=xdist_postgres_isolate,
        host_port=alloydb_omni_port,
        timeout=90,
        command=CUSTOM_ALLOYDB_COMMAND,
        shm_size="512m",
        init_sql=ALLOYDB_OMNI_INIT_SQL,
    )
```

</example>

---

## References Index

- [Supported database patterns](references/databases.md) — service-first, AlloyDB Omni 18, embedded Postgres, and cloud/broker emulator examples.
- [Complete fixture matrix](references/reference.md) — exact `v0.20.0b3` modules, service classes, service fixtures, and helper methods.
- [Xdist parallel testing](references/xdist.md) — `"database"`, `"server"`, and PostgreSQL `"template"` isolation levels.
- [Configuration](references/config.md) — CLI flags, environment variables, runtime resolution, and fixture overrides.
- [Troubleshooting](references/troubleshooting.md) — runtime, plugin load order, 4Kn storage, and port failures.
- [Litestar testing](../litestar-testing/SKILL.md) — integrate container-backed services with Litestar test clients.

## Official References

- <https://pypi.org/project/pytest-databases/0.20.0b3/>
- <https://github.com/litestar-org/pytest-databases>
- <https://github.com/litestar-org/pytest-databases/tree/main/src/pytest_databases/docker>
- <https://github.com/litestar-org/pytest-databases/tree/main/src/pytest_databases/embedded>

## Shared Styleguide Baseline

- [General Principles](../litestar-styleguide/references/general.md)
- [Testing](../litestar-styleguide/references/testing.md)
- [Python](../litestar-styleguide/references/python.md)

Files in this skill

  • SKILL.md7 KB
  • references/config.md2 KB
  • references/databases.md3.3 KB
  • references/reference.md4.7 KB
  • references/troubleshooting.md2 KB
  • references/xdist.md2.4 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.