8000 GitHub - Pingviinituutti/mqtt-postgres: A simple rust program to write mqtt topics to a postgresql database · GitHub
[go: up one dir, main page]

Skip to content

About

A simple rust program to write mqtt topics to a postgresql database

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

Repository files navigation

mqtt-postgres

Rust MQTT ingestion worker that batches writes into PostgreSQL.

What it does

  • Subscribes to configurable MQTT topic filters.
  • Parses payload into JSON (payload column).
  • Extracts simple values into value_numeric or value_text when possible.
  • Batches inserts into mqtt_messages.
  • Exposes /healthz, /readyz, and /metrics for operations.

Architecture

  • MQTT broker -> Rust worker -> Postgres
  • Internal bounded queue for backpressure.
  • Timer-based and size-based batch flush.
  • Retry with exponential backoff for transient SQL errors.

Environment variables

Copy and edit .env.example.

Required:

  • MQTT_URL
  • MQTT_TOPICS
  • MQTT_NAME_PROPERTY (defaults to name)
  • MQTT_SENSOR_VALUE_PROPERTY (defaults to sensor_value)
  • DATABASE_URL (or PG_HOST, PG_PORT, PG_DB, PG_USER, PG_PASSWORD)

Optional:

  • MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID
  • PG_SCHEMA (default public)
  • PG_TABLE (default mqtt_messages)
  • BATCH_SIZE (default 100)
  • BATCH_INTERVAL_MS (default 500)
  • QUEUE_CAPACITY (default 10000)
  • MAX_INSERT_RETRIES (default 5)
  • METRICS_ADDR (default 0.0.0.0:8080)

Table schema

Create the target table first:

psql "$DATABASE_URL" -f schema.sql

Run locally

cargo run --release

Health and metrics

  • GET /healthz -> process alive
  • GET /readyz -> MQTT connected and Postgres reachable
  • GET /metrics -> Prometheus text format

The ingestor looks for a sensor name field and a sensor value field in each topic payload. Messages that do not contain the configured sensor value field are skipped. On first startup, retained messages are used to build an allowlist of sensor IDs, where the sensor ID is the last path segment of the topic.

Docker

docker build -t mqtt-postgres:latest .
docker run --rm -p 8080:8080 --env-file .env mqtt-postgres:latest

GitHub Container Registry

Push a tag like v1.2.3 to publish a new image to GHCR at:

ghcr.io/<owner>/mqtt-postgres

The workflow also publishes latest for tagged releases.

Kubernetes

Manifest is in k8s/deployment.yaml.

kubectl apply -f k8s/deployment.yaml

Replace the image and secret values for your environment.

Note

Heavy use of copilot was used to write this code.

About

A simple rust program to write mqtt topics to a postgresql database

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0