TimescaleDB IoT Analytics
TimescaleDB for time-series with MQTT and Grafana.
Overview
TimescaleDB is a PostgreSQL extension specifically designed for time-series data that combines the reliability and SQL capabilities of PostgreSQL with specialized time-series functionality including automatic partitioning, continuous aggregates, and data compression. Originally developed to handle the massive scale of time-series data in IoT and monitoring applications, TimescaleDB provides up to 90% data compression while maintaining full PostgreSQL compatibility. This IoT analytics stack combines TimescaleDB with Eclipse Mosquitto MQTT broker and Grafana to create a complete pipeline for collecting, storing, and visualizing IoT sensor data in real-time. The architecture allows MQTT devices to publish sensor readings to Mosquitto, which can then be processed and stored in TimescaleDB's hypertables for efficient time-series queries, while Grafana provides rich dashboards and alerting capabilities. This combination is particularly valuable for IoT deployments because it handles the entire data lifecycle from ingestion through visualization while providing enterprise-grade reliability and SQL query capabilities that make it easy to correlate time-series data with relational business data.
Key Features
- Automatic time-based partitioning through TimescaleDB hypertables for efficient IoT data storage
- Up to 90% data compression with TimescaleDB's native compression algorithms
- Full PostgreSQL compatibility allowing complex SQL joins between time-series and relational data
- MQTT broker with Eclipse Mosquitto supporting IoT device connectivity and pub/sub messaging
- Continuous aggregates for real-time computation of averages, sums, and other metrics
- Native PostgreSQL extensions work with TimescaleDB including PostGIS for location data
- Rich time-series visualizations in Grafana with built-in TimescaleDB data source support
- Data retention policies for automatic cleanup of old IoT sensor data
Common Use Cases
- 1Smart building management systems collecting temperature, humidity, and energy consumption data
- 2Industrial IoT monitoring for equipment performance, vibration analysis, and predictive maintenance
- 3Agricultural sensor networks tracking soil moisture, weather conditions, and crop health metrics
- 4Vehicle fleet tracking with GPS coordinates, fuel consumption, and engine diagnostics
- 5Environmental monitoring stations collecting air quality, noise levels, and weather data
- 6Smart city infrastructure monitoring traffic patterns, parking availability, and utility usage
- 7Home automation systems integrating multiple IoT devices with long-term trend analysis
Prerequisites
- Minimum 3GB RAM for all services (TimescaleDB 2GB, Grafana 512MB, Mosquitto 256MB)
- Docker and Docker Compose installed with support for multi-container networking
- Available ports 5432 (TimescaleDB), 1883 (MQTT), and 3000 (Grafana) or custom port configuration
- Basic understanding of SQL and PostgreSQL for creating hypertables and queries
- MQTT protocol knowledge for configuring IoT device connections
- Understanding of time-series data concepts like retention policies and continuous aggregates
For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms
docker-compose.yml
docker-compose.yml
1services: 2 timescaledb: 3 image: timescale/timescaledb:latest-pg154 container_name: timescaledb5 restart: unless-stopped6 ports: 7 - "${TSDB_PORT:-5432}:5432"8 environment: 9 - POSTGRES_USER=${TSDB_USER}10 - POSTGRES_PASSWORD=${TSDB_PASSWORD}11 - POSTGRES_DB=iot_data12 volumes: 13 - timescale_data:/var/lib/postgresql/data1415 mosquitto: 16 image: eclipse-mosquitto:217 container_name: iot-mqtt18 restart: unless-stopped19 ports: 20 - "${MQTT_PORT:-1883}:1883"21 volumes: 22 - ./mosquitto.conf:/mosquitto/config/mosquitto.conf:ro2324 grafana: 25 image: grafana/grafana:latest26 container_name: iot-grafana27 restart: unless-stopped28 ports: 29 - "${GRAFANA_PORT:-3000}:3000"30 environment: 31 - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_PASSWORD}32 volumes: 33 - grafana_data:/var/lib/grafana3435volumes: 36 timescale_data: 37 grafana_data: .env Template
.env
1# TimescaleDB IoT2TSDB_PORT=54323TSDB_USER=iot4TSDB_PASSWORD=iot_password5MQTT_PORT=18836GRAFANA_PORT=30007GRAFANA_PASSWORD=adminUsage Notes
- 1TimescaleDB at localhost:5432
- 2MQTT broker at localhost:1883
- 3Grafana at http://localhost:3000
- 4Create hypertables for time-series
Individual Services(3 services)
Copy individual services to mix and match with your existing compose files.
timescaledb
timescaledb:
image: timescale/timescaledb:latest-pg15
container_name: timescaledb
restart: unless-stopped
ports:
- ${TSDB_PORT:-5432}:5432
environment:
- POSTGRES_USER=${TSDB_USER}
- POSTGRES_PASSWORD=${TSDB_PASSWORD}
- POSTGRES_DB=iot_data
volumes:
- timescale_data:/var/lib/postgresql/data
mosquitto
mosquitto:
image: eclipse-mosquitto:2
container_name: iot-mqtt
restart: unless-stopped
ports:
- ${MQTT_PORT:-1883}:1883
volumes:
- ./mosquitto.conf:/mosquitto/config/mosquitto.conf:ro
grafana
grafana:
image: grafana/grafana:latest
container_name: iot-grafana
restart: unless-stopped
ports:
- ${GRAFANA_PORT:-3000}:3000
environment:
- GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_PASSWORD}
volumes:
- grafana_data:/var/lib/grafana
Quick Start
terminal
1# 1. Create the compose file2cat > docker-compose.yml << 'EOF'3services:4 timescaledb:5 image: timescale/timescaledb:latest-pg156 container_name: timescaledb7 restart: unless-stopped8 ports:9 - "${TSDB_PORT:-5432}:5432"10 environment:11 - POSTGRES_USER=${TSDB_USER}12 - POSTGRES_PASSWORD=${TSDB_PASSWORD}13 - POSTGRES_DB=iot_data14 volumes:15 - timescale_data:/var/lib/postgresql/data1617 mosquitto:18 image: eclipse-mosquitto:219 container_name: iot-mqtt20 restart: unless-stopped21 ports:22 - "${MQTT_PORT:-1883}:1883"23 volumes:24 - ./mosquitto.conf:/mosquitto/config/mosquitto.conf:ro2526 grafana:27 image: grafana/grafana:latest28 container_name: iot-grafana29 restart: unless-stopped30 ports:31 - "${GRAFANA_PORT:-3000}:3000"32 environment:33 - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_PASSWORD}34 volumes:35 - grafana_data:/var/lib/grafana3637volumes:38 timescale_data:39 grafana_data:40EOF4142# 2. Create the .env file43cat > .env << 'EOF'44# TimescaleDB IoT45TSDB_PORT=543246TSDB_USER=iot47TSDB_PASSWORD=iot_password48MQTT_PORT=188349GRAFANA_PORT=300050GRAFANA_PASSWORD=admin51EOF5253# 3. Start the services54docker compose up -d5556# 4. View logs57docker compose logs -fOne-Liner
Run this command to download and set up the recipe in one step:
terminal
1curl -fsSL https://docker.recipes/api/recipes/timescaledb-iot-stack/run | bashTroubleshooting
- TimescaleDB connection refused: Ensure POSTGRES_USER and POSTGRES_PASSWORD environment variables are set and container has fully initialized
- Mosquitto authentication failures: Check mosquitto.conf file permissions and verify it's mounted read-only to /mosquitto/config/mosquitto.conf
- Grafana TimescaleDB datasource connection errors: Use service name 'timescaledb' as hostname, not localhost, due to Docker networking
- Hypertable creation fails: Connect to TimescaleDB first and run 'CREATE EXTENSION IF NOT EXISTS timescaledb;' before creating hypertables
- MQTT messages not persisting: Verify database connection from your MQTT consumer and check that hypertables are created with proper time column
- High memory usage in TimescaleDB: Enable compression on older data chunks and configure appropriate retention policies for IoT data lifecycle
Community Notes
Loading...
Loading notes...
Download Recipe Kit
Get all files in a ready-to-deploy package
Includes docker-compose.yml, .env template, README, and license
Ad Space
Shortcuts: C CopyF FavoriteD Download