Skip to main content

Data Storage Integration

Data storage routes enable automatic persistence of MQTT messages to databases and files. Store sensor data for analytics, create historical archives, aggregate logs, and build time-series datasets—all configured through LoT syntax.
Like a filing cabinet with an assistant. You can hand your assistant a form (STORE IN with models) and they file it automatically, or you can write specific filing instructions yourself (EVENTs with queries). Either way, your data ends up organized in the right drawer.

Supported Databases

PostgreSQL

Open-source relational database for structured data storage.

MySQL

Popular relational database with wide ecosystem support.

MariaDB

MySQL-compatible database with enhanced features.

SQL Server

Microsoft SQL Server for enterprise environments.

MongoDB

Document database for flexible JSON storage.

OpenSearch

Real-time analytics, search, and visualization.

CrateDB

Distributed SQL optimized for time-series data.

Snowflake

Cloud data warehouse for large-scale analytics.

File Storage

CSV and JSON file storage for simple persistence.

Database Comparison

SQL databases use standard SQL queries. CLEAN is a simplified document-style syntax used by non-relational databases and file storage — see Query Formats below for details.

How to Build a Data Storage Route

A data storage route needs two things: a connection to your database and at least one rule for what to store. You define the connection once, then add events that listen for MQTT messages and run queries — or use a model with STORE IN to save data automatically without writing queries at all.

Quick Example

This complete route connects to a PostgreSQL database and automatically stores every sensor reading that arrives on sensors/+/reading:
That’s it — the broker handles the rest. The sections below break down each part in detail.

Step 1: Define the Route Connection

Every data storage route starts with a connection configuration. Define the route type and provide the connection details for your database:
Each database type has its own config block (SQL_CONFIG, MONGODB_CONFIG, OPENSEARCH_CONFIG, etc.). See the specific database page for exact parameters.
The examples on this page use hardcoded credentials for clarity. In production, use environment variables and encrypted secrets to keep sensitive values out of your LoT definitions:
See Environment Variables and Secrets for setup instructions across Windows, Linux, Docker, and Kubernetes.

Step 2: Store Data

There are two ways to write data into your database. Choose the approach that fits your use case:
The STORE IN approach ties a model directly to a database route. Every time an action publishes this model, the record is automatically inserted into the specified table — no EVENT or query needed.1. Define a model with STORE IN:
2. Define the route the model stores into:
3. Publish the model from an action:
Every PUBLISH MODEL call automatically inserts a row into the production_counts table with all the model fields as columns.

Step 3: Read Data (SELECT)

EVENTs also support SELECT queries to read data back from the database. Publish a message to the event’s SOURCE_TOPIC with your query parameters, and the result is published to DESTINATION_TOPIC:
To trigger this query, publish the machine ID to the source topic:
The query result is published to factory/production/latest_count_from_db, where your actions or external clients can consume it.

Common Query Placeholders

These placeholders work across all database routes:

Extracting Fields from Topics

For topic sensors/temp001/reading, use these placeholders:

Query Formats

SQL Format

For relational databases (PostgreSQL, MySQL, MariaDB, SQL Server, CrateDB, Snowflake):

CLEAN Format

For document databases and search engines (MongoDB, OpenSearch, File Storage):
The CLEAN format provides simplified JSON syntax without requiring proper escaping.

Common EVENT Parameters

SOURCE_TOPIC
string
required
MQTT topic pattern that triggers the query. For INSERT operations, incoming message data is used to populate placeholders. For SELECT operations, the payload can be used as a query parameter via {payload}.
DESTINATION_TOPIC
string
MQTT topic to publish query results. Required for SELECT queries so the result can be consumed by actions or external clients. Optional for INSERT queries (publishes operation status).
QUERY
string
required
SQL statement or CLEAN format query with placeholders. Supports both write operations (INSERT INTO ...) and read operations (SELECT ... FROM ...).

Best Practices

Always enable SSL for database connections. Most database routes support SSL configuration through the config block:
When using certificate-based authentication, store certificate passphrases as encrypted secrets rather than in plain text. See Environment Variables and Secrets for details.
Never hardcode passwords or API keys in route definitions. Use GET SECRET for sensitive values and GET ENV for non-sensitive configuration like hostnames and ports. Create dedicated database users with minimal permissions for each route.See Environment Variables and Secrets for full setup and deployment instructions.
Implement data retention policies:
  • Index lifecycle management
  • Time-based partitioning
  • Automatic cleanup jobs
When your data has a consistent schema, use STORE IN on a model to automatically insert records every time the model is published. This eliminates the need to write INSERT queries manually:
For high-throughput scenarios, configure connection pools where supported.

Next Steps

Choose the database that matches your requirements:

PostgreSQL

Start with the most popular open-source database.

MongoDB

Store flexible JSON documents.
Last modified on May 22, 2026