PgQueuer turns PostgreSQL into a fast, reliable background job processor. Jobs live in the same database as your application data. One stack, full ACID guarantees, and no separate message broker to run.
AI-assisted setup
Already using a coding agent? Copy the onboarding prompt from the
PgQueuer documentation.
The prompt directs your agent to the maintained setup route and canonical docs.
Features
💡 Minimal footprint: one pip install; bring your existing PostgreSQL connection and start enqueueing
🔁 Transactional enqueue: commit a job in the same transaction as your data; no dual-write drift
⚛️ Safe concurrency: workers claim jobs with FOR UPDATE SKIP LOCKED (never double-processed), with per-entrypoint limits and serialized dispatch when you need them
🚀 Instant dispatch: LISTEN/NOTIFY wakes workers the moment a job lands (with a polling fallback)
⏰ Scheduling & deferral: cron-style recurring tasks and execute_after, no separate scheduler process
📊 : completion tracking, Prometheus metrics, tracing (Logfire/Sentry), and a live dashboard
Observability
🧪 In-memory mode: run the whole queue without Postgres for tests and prototyping
Why PostgreSQL?
If you already run PostgreSQL, it can do double duty as your job queue. That means one fewer service to operate, and your queue and data stay consistent because they share the same database and transactions.
pip install pgqueuer
pgq install # create tables and functions in your database
The CLI reads PGHOST, PGUSER, PGDATABASE and related environment variables. Use pgq sql install to preview the SQL (or pipe it to another client: pgq sql install | psql), --prefix myapp_ to namespace tables, or pgq uninstall to remove the schema. Code reads the same names from PGQUEUER_PREFIX and related environment variables, or from settings=DBSettings(prefix="myapp_") passed to PgQueuer and Queries.
Quick Start
PgQueuer pairs consumers (workers that process jobs) with producers (code that enqueues jobs).
1. Define a consumer
Each entrypoint is a job handler. Run it with the CLI: pgq run examples.consumer:main.
The job arrives instantly via LISTEN/NOTIFY, and your consumer's process handler picks it up.
Enqueue inside a transaction
This is what a database-backed queue buys you: the job and your business data commit together, or not at all.
order_id = 42
async with connection.transaction():
await connection.execute(
"INSERT INTO orders (id, status) VALUES ($1, 'paid')", order_id
)
await queries.enqueue("send_receipt", str(order_id).encode())
# If the transaction rolls back, the job is never enqueued.
Run without a database
PgQueuer.in_memory() is a drop-in replacement that implements the same ports as the real backend, so your handlers stay identical. Good for unit tests and prototyping.
PgQueuer uses Testcontainers to spin up an ephemeral PostgreSQL instance for the test suite. Just have Docker running.
uv sync --frozen # install dependencies
uv run ruff check . && uv run ruff format . --check && uv run lint-imports && uv run mypy . && uv run pytest # full check suite
License
PgQueuer is MIT licensed. See LICENSE for details.
Alternatives
Use instead of pgqueuer: tools that do the same job, closest first.