PGMQ (PostgreSQL Message Queue)#
A lightweight message queue. Like AWS SQS and RSMQ but on Postgres.
https://github.com/tembo-io/pgmq
PGMQ is a lightweight message queue based on PostgreSQL.
It is implemented using a PostgreSQL Extension.
Since it is based solely on PostgreSQL, it is very suitable for lightweight business needs that require an MQ.
Currently, the usage scenario is using the existing API Server + PGMQ + consumer to handle some asynchronous tasks and retry mechanisms with third-party services.
Installation#
Tembo Docker Image#
First, ensure your system has the following installed:
- Docker
- Docker Compose
The simplest way is to use the official Tembo Docker Image:
docker run -d --name postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 quay.io/tembo/pg16-pgmq:latest
Installing from Extension#
If you want to install using the original postgres image as an extension:
- Download the latest PGMQ SQL and Control files:
curl -o pgmq.sql https://raw.githubusercontent.com/tembo-io/pgmq/main/pgmq-extension/sql/pgmq.sql
curl -o pgmq.control https://raw.githubusercontent.com/tembo-io/pgmq/main/pgmq-extension/pgmq.control
- Update
docker-compose.ymlto add the volume settings:
db.sqlcontains some initialization SQL files (depending on the project’s needs)pgmq.sqlandpgmq.controlare the PGMQ SQL and Control files- The version number in
pgmq--1.3.3.sqlshould match the version number inpgmq.control
- The version number in
stateful_volumes/postgresqlis the directory mounted forpostgrescontainer persistence
version: '3.8'
services:
postgres:
image: postgres:15.1
container_name: postgres
ports:
- 5432:5432
env_file:
- postgres.env
restart: always
volumes:
- ./db.sql:/docker-entrypoint-initdb.d/db.sql
- ./pgmq.control:/usr/share/postgresql/15/extension/pgmq.control
- ./pgmq.sql:/usr/share/postgresql/15/extension/pgmq--1.3.3.sql
- ./stateful_volumes/postgresql:/var/lib/postgresql/data/
healthcheck:
test: ["CMD", "pg_isready", "-U", "your_postgres_user", "-d", "your_postgres_db"]
interval: 2s
timeout: 5s
retries: 3
- Start the
postgrescontainer:
If you have previously run a
postgrescontainer, remember to delete the volume first!
docker compose up postgres -d
PGMQ Python Client#
- Install the
pgmqPython Client:
pip install tembo-pgmq-python
Since
tembo-pgmq-pythondirectly usespsycopg(which ispsycopg3, notpsycopg2)
it may have issues when installed withpoetryon Mac.
It is recommended to install it directly withpip.
- Using the
pgmqPython Client:
The tembo-pgmq-python README is quite clear.
The main components are the PGMQueue and Message data classes.
The vt stands for visibility timeout, which is the time duration during which a message will not be re-read after being read.
Here is a simple example:
from tembo_pgmq_python import PGMQueue, Message
queue = PGMQueue(
host="0.0.0.0",
port="5432",
username="postgres",
password="postgres",
database="postgres"
)
msg_id: int = queue.send("my_queue", {"hello": "world"})
read_message: Message = queue.read("my_queue", vt=30)
print(read_message)
deleted: bool = queue.delete("my_queue", read_message.msg_id)
dropped: bool = queue.drop_queue("my_queue")
For more examples, refer to the tembo-pgmq-python README.
