What is PGMQ?
A lightweight message queue. Like AWS SQS and RSMQ but on Postgres.
PGMQ (Postgres Message Queue) is a message queue built on Postgres. It provides reliable, transactional message processing with the familiarity of SQL. The pgmq Python library exposes a clean, unified API for interacting with PGMQ across four different database backends.
Prerequisites
A running PostgreSQL instance with the PGMQ extension installed.
Docker (recommended)
The fastest way to get started is with the pre-built Docker image:
docker run -d --name pgmq-postgres \
-e POSTGRES_PASSWORD=postgres \
-p 5432:5432 \
ghcr.io/pgmq/pg18-pgmq:latest
Then connect and enable PGMQ:
psql postgres://postgres:postgres@localhost:5432/postgres -c "CREATE EXTENSION pgmq;"
SQL Only
You can also install PGMQ's objects directly into the pgmq schema. Use this on hosted Postgres services that do not support custom extensions.
git clone https://github.com/pgmq/pgmq.git
cd pgmq
psql -f pgmq-extension/sql/pgmq.sql postgres://postgres:postgres@localhost:5432/postgres
Or install from Python using the SQL script shipped with this package:
from pgmq import install_pgmq_from_sql
install_pgmq_from_sql(
host="localhost",
port="5432",
username="postgres",
password="postgres",
database="postgres",
)
SQL-only install does not support extension versioning or upgrades. Prefer
CREATE EXTENSION pgmq when the host allows custom extensions.
Installation
pip install pgmq
Optional backends:
| Extra | Backend |
|---|---|
pgmq[async] |
asyncpg |
pgmq[sqlalchemy] |
SQLAlchemy (sync) |
pgmq[sqlalchemy-async] |
SQLAlchemy (async) |
pgmq[fastapi] |
FastAPI HTTP API (python -m pgmq.api) |
Features
Lightweight — No background workers or external dependencies. Just Postgres SQL objects.
Exactly-once delivery — Guaranteed delivery to a single consumer within a visibility timeout.
Four identical APIs — Swap between sync (psycopg), async (asyncpg), sync SQLAlchemy, and async SQLAlchemy with minimal changes.
Queue management — Create, drop, list, purge, and partition queues.
Message operations — Send, read, archive, delete, pop. Batch operations for high throughput.
FIFO queues — Ordered processing with message group keys.
Topic routing — Pattern-based bindings for publish-subscribe and content-based routing.
Visibility timeouts — Control how long a message stays hidden after reading.
Notifications — PostgreSQL NOTIFY/LISTEN for real-time message arrival events.
Transactions — Decorators and manual connection injection for complex workflows.
Structured logging — stdlib logging with optional loguru backend.
Documentation
- Getting Started — Installation, Docker setup, and first messages
- Configuration — Environment variables and connection strings
- Clients — Choosing and initializing backends
- Transactions — Transaction decorators and manual connections
- Topic Routing — Pattern-based message routing
- Notifications — Real-time NOTIFY/LISTEN listeners
Quick Start
Sync (psycopg):
from pgmq import PGMQueue
queue = PGMQueue() # reads PG_* env vars by default
# Create a queue
queue.create_queue("my_queue")
# Send a message
msg_id = queue.send("my_queue", {"hello": "world"})
# Send a batch
batch_ids = queue.send_batch("my_queue", [{"foo": "bar"}, {"baz": "qux"}])
# Read with 30s visibility timeout
msg = queue.read("my_queue", vt=30)
print(msg.message) # {'hello': 'world'}
# Archive when done
queue.archive("my_queue", msg.msg_id)
Async (asyncpg):
from pgmq import AsyncPGMQueue
queue = AsyncPGMQueue()
await queue.init()
# Create a queue
await queue.create_queue("my_queue")
# Send a message
msg_id = await queue.send("my_queue", {"hello": "world"})
# Send a batch
batch_ids = await queue.send_batch("my_queue", [{"foo": "bar"}, {"baz": "qux"}])
# Read with 30s visibility timeout
msg = await queue.read("my_queue", vt=30)
print(msg.message) # {'hello': 'world'}
# Archive when done
await queue.archive("my_queue", msg.msg_id)
Development
# Install dependencies
uv sync --all-groups --all-extras
# Run tests (spins up Docker Postgres automatically)
make test
# Run lints
make lint
# Serve docs locally
make docs-serve
License
Apache-2.0
Metadata
Release files for pgmq 1.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pgmq-1.1.5.tar.gz | 64.1 kB | Without provenance | Details for pgmq-1.1.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pgmq-1.1.5-py3-none-any.whl | Python 3 | none | any | Details for pgmq-1.1.5-py3-none-any.whl |
Total release size: 140.7 kB
Release files / pgmq-1.1.5.tar.gz
| Download URL | pgmq-1.1.5.tar.gz |
|---|---|
| Size | 64.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dd4a64371e0147fa6ddda1cb5f024774d3df93c85bd6e4fb44cb68f6338e590e
|
|
BLAKE2b-256 checksum How to use checksums |
95d2f71764402592601d9b0c199fadcee59c480747ed908c5b17aef5669de1c4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / pgmq-1.1.5-py3-none-any.whl
| Download URL | pgmq-1.1.5-py3-none-any.whl |
|---|---|
| Size | 76.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b50ac1ac77fbac16a272e76c26085d3063b7e7dee55e562a2f89c363980fa310
|
|
BLAKE2b-256 checksum How to use checksums |
6657f5455729bc0f02f64d17262171d3f55b60d1f86e1d2127c9ba832da91458
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for pgmq 1.1.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.