Skip to main content

CrateDB Overview

The CRATEDB connector stores MQTT messages in CrateDB, a distributed SQL database optimized for time-series and machine data. It combines the familiarity of SQL with the scalability needed for IoT workloads.
CrateDB excels at high-volume time-series data with fast ingestion and SQL query capabilities. Use it when you need real-time analytics on sensor data at scale.

Basic Syntax


Connection Configuration

CRATEDB_CONFIG Parameters

string
required
CrateDB server hostname or IP address.
string
required
Database username.
string
required
Database password.
integer
CrateDB PostgreSQL protocol port. Default: 5432.
string
Schema for unqualified table names and for STORE IN. Default: doc. Kept exactly as written, so it is case-sensitive.
boolean
Connect over TLS. Default: false. Without it the connection is plain text. See TLS.
boolean
Require TLS and accept a certificate the broker cannot verify, such as a self-signed one. Default: false. Applies only when USE_SSL is true.
integer
Seconds to wait for a connection to open. A whole number, 1 or more. Default: 15.
integer
Seconds to wait for an event query or a STORE IN insert. A whole number, 0 or more; 0 waits forever. Default: 30.
Deploy refuses a missing HOST, a PORT outside 1–65535, a CONNECTION_TIMEOUT below 1, a COMMAND_TIMEOUT that is not a whole number, and a USE_SSL or TRUST_SERVER_CERTIFICATE that is not true or false. A password, user, or database that contains ;, =, or a quote is sent as typed.
Never hardcode credentials in production. Use environment variables and encrypted secrets to keep sensitive values out of your connector definitions:
See Environment Variables & Secrets for setup and usage.

TLS

Use USE_SSL "true" with TRUST_SERVER_CERTIFICATE "true" for a CrateDB node with a self-signed certificate:
Action: A CrateDB node that only accepts TLS now needs USE_SSL "true". Without it the broker no longer tries TLS on its own.

Agent queries

An agent can read this database when the connector opts in and the agent grants connector_query. The agent runs one read statement. It cannot insert, update, or delete. See Database queries. The clauses below belong in CRATEDB_CONFIG. CrateDB is refused unless you set AGENT_USERNAME and AGENT_PASSWORD, or AGENT_CONNECTION_STRING. It does not fall back to a read-only transaction on the connector login. The person who asks and the agent’s DEFAULT_USER both need PublishSys on $SYS/Coreflux/Routes/<connector>/Query.

How placeholders are quoted

The broker places each placeholder in an event QUERY by the quotes around it. A publisher on the source topic can no longer change the SQL by putting SQL in the payload.
Action: Put every placeholder that carries text in single quotes (WHERE status = '{payload}'). Put a placeholder that names a table or column in double quotes (FROM "{topic.2}"). An unquoted placeholder must be a number, a comma-separated list of numbers, true, false, or null. Anything else, including a bare name such as line_1, refuses the event. The message names the placeholder.
{env.NAME} and {secret.NAME} are placed as written. A value that itself contains {topic} is stored as text and is not expanded again. The same rules on the other SQL engines are in How placeholders are quoted. MongoDB and OpenSearch JSON queries place each value as one JSON value instead (see OpenSearch placeholders).

Writing Data

Store sensor readings with timestamps:
Alternative: STORE IN with Models — Instead of writing EVENT queries, you can bind a model directly to this connector. Every PUBLISH MODEL call automatically inserts a row — no query needed:
See Data Storage Overview for the full STORE IN workflow.

Storing models with STORE IN

STORE IN writes to the schema set by DATABASE, or to doc when DATABASE is omitted or blank. That is the same schema an event uses for a table name without a schema, so an event can read back what a model stored. The table name is folded to lower case, and column names come from the model fields in lower case. The first store creates a missing table. A table that already exists keeps its columns. If the table is dropped later, the next store creates it again. A model INT is a 32-bit whole number. A topic value outside -2147483648 to 2147483647 is logged and the model does not publish, so nothing is stored. The value is no longer stored as 2147483647.
Upgrading: Rows that earlier versions stored are in the doc schema. When DATABASE names another schema, new rows go to that schema, and the old rows stay in doc. Copy them over, or query doc.<table>, if you still need them.

Reading Data

EVENTs also support SELECT queries to read data back from the database. Publish a message to the event’s SOURCE_TOPIC, and the query result is published to DESTINATION_TOPIC:
To trigger this query, publish the sensor ID to the source topic:
The query result is published to db/result/latest, where your actions or external clients can consume it.
CrateDB makes new rows visible to reads about once a second. A SELECT event that runs right after an insert may not see that row yet. Run REFRESH TABLE <table> first when you need it.

Timed Polling

EVENTs can also run SELECT queries on a fixed schedule using WITH EVERY. This is especially powerful with CrateDB’s fast aggregations:

While CrateDB is unreachable

After a connection failure, the next event or STORE IN checks CrateDB at once. If CrateDB answers, the work runs. If not, the work is skipped and later checks run at most every 5 seconds. Work that arrives between checks is skipped instead of waiting out CONNECTION_TIMEOUT. Each check logs how many events and STORE IN rows were skipped: a warning while CrateDB is still down, and an information line with the total when the connector reconnects.

Checking the connection

-checkConnectorConnection <Name> connects to CrateDB and runs SELECT 1. A wrong host, port, or login fails the check, and the reply names the host and port it tried. The password is not included. The check waits at most 10 seconds.

Table Schema Examples

Create tables optimized for time-series:

Troubleshooting

  • Run -checkConnectorConnection and read the host and port in the reply
  • Verify HOST and PORT are correct
  • Check CrateDB is running and accepting connections
  • Verify firewall allows connections on port 5432
  • If the node only accepts TLS, set USE_SSL "true". See TLS
  • Verify USERNAME and PASSWORD are correct
  • Check user has permissions on the DATABASE
  • Verify table exists and has correct schema
  • Put text placeholders in single quotes, and table or column placeholders in double quotes. An unquoted text value refuses the event. See How placeholders are quoted
  • Check column types match inserted data
  • Look in the schema set by DATABASE, or doc when it is omitted. The name is case-sensitive
  • An INT value outside the 32-bit range is logged and not stored

Next Steps

Data Storage Connectors Overview

Compare all storage options.

Snowflake Connector

Configure Snowflake cloud data warehouse.
Last modified on December 4, 2026