Skip to main content

PostgreSQL Overview

The POSTGRESQL route stores MQTT messages in PostgreSQL databases. It supports SSL connections, parameterized queries, and automatic reconnection for reliable data persistence. Because the route speaks the PostgreSQL wire protocol, it also works with any service built on PostgreSQL — you use the same POSTGRESQL route type and point SQL_CONFIG at that provider’s host, port, database, and credentials.
PostgreSQL is ideal for structured data that requires complex queries, joins, and ACID compliance. Use it when you need relational integrity and powerful SQL capabilities.
For TimescaleDB or Tiger Data (Tiger Cloud), use standard PostgreSQL connection settings — host, port 5432, database, user, and password from your service dashboard. Create hypertables and time-series schemas in SQL as you would in any Postgres client; the route executes your QUERY strings against that database.

Basic Syntax


Connection Configuration

SQL_CONFIG Parameters

SERVER
string
required
PostgreSQL server hostname or IP address.
DATABASE
string
required
Target database name.
USERNAME
string
required
Database username.
PASSWORD
string
required
Database password.
PORT
integer
PostgreSQL port. Default: 5432.
USE_SSL
boolean
Enable SSL connection. Default: false.
TRUST_SERVER_CERTIFICATE
boolean
Trust server certificate without validation. Default: false.
CONNECTION_TIMEOUT
integer
Connection timeout in seconds. Default: 30.
COMMAND_TIMEOUT
integer
Command timeout in seconds. Default: 30.
Never hardcode credentials in production. Use environment variables and encrypted secrets to keep sensitive values out of your route definitions:
See Environment Variables & Secrets for setup and usage.

Event Configuration

EVENT Parameters

SOURCE_TOPIC
string
required
MQTT topic pattern that triggers the INSERT. Supports wildcards.
QUERY
string
required
SQL INSERT statement with placeholders for dynamic values.
DESTINATION_TOPIC
string
MQTT topic to publish query result status.

Query Placeholders


Writing Data

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

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.

Timed Polling

EVENTs can also run SELECT queries on a schedule instead of waiting for an MQTT trigger. Use WITH EVERY to publish fresh data at a fixed interval:
The query result is published to dashboard/devices/active every 10 seconds — no trigger message needed.

Table Schema Examples

Create tables to match your data:

Troubleshooting

  • Verify SERVER and PORT are correct
  • Check PostgreSQL is running and accepting connections
  • Verify firewall allows connections on port 5432
  • Check pg_hba.conf allows your IP address
  • Verify USERNAME and PASSWORD are correct
  • Check user has permissions on the DATABASE
  • Verify authentication method in pg_hba.conf
  • Ensure USE_SSL matches server requirements
  • For self-signed certs, set TRUST_SERVER_CERTIFICATE “true”
  • Verify SSL is enabled on PostgreSQL server
  • Check placeholder names match topic structure
  • Verify table and column names exist
  • Test query manually in psql first
  • Ensure proper escaping for special characters
  • Increase CONNECTION_TIMEOUT for slow networks
  • Increase COMMAND_TIMEOUT for complex queries
  • Check server load and query performance

Next Steps

Data Storage Routes Overview

Compare all storage options.

MySQL Route

Configure MySQL database storage.
Last modified on May 22, 2026