Skip to main content

PGMQ Python Client

PyPI Python Versions Downloads License

The official Python client for PGMQ.

📖 Documentation  ·  💻 Source


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)

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


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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pgmq-1.1.3.tar.gz (43.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pgmq-1.1.3-py3-none-any.whl (51.9 kB view details)

Uploaded Python 3

File details

Details for the file pgmq-1.1.3.tar.gz.

File metadata

  • Download URL: pgmq-1.1.3.tar.gz
  • Upload date:
  • Size: 43.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pgmq-1.1.3.tar.gz
Algorithm Hash digest
SHA256 6d6498309f55bdf118e71d663bf178a70229a25e9d779a8e04cc3bd932720937
MD5 89efd11e0f95820560e9dc2faed39095
BLAKE2b-256 8693483f64a2f71314c57573ee8017b13e755839762fe23750310345839f1932

See more details on using hashes here.

File details

Details for the file pgmq-1.1.3-py3-none-any.whl.

File metadata

  • Download URL: pgmq-1.1.3-py3-none-any.whl
  • Upload date:
  • Size: 51.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pgmq-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d854e477a94d446af50dbc56a3bb01e8c4c38a7ec6c562bf5acae2f0802743d7
MD5 e7dc837f84cbffef01bfd96462cb5117
BLAKE2b-256 db8ed01630474d6d1b0d17ec38653564ded5ffd88878795f704cdc48a46afd00

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.1.3 This release

2 files

1.1.2

2 files

1.1.1

2 files

1.1.0

2 files

1.0.6

2 files

1.0.5

2 files

1.0.4

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

0.6.1

2 files

0.6.0

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page