docker.recipes

TimescaleDB + Grafana

intermediate

TimescaleDB time-series database built on PostgreSQL with Grafana for visualization.

Overview

TimescaleDB extends PostgreSQL with specialized time-series capabilities, combining the reliability and SQL compatibility of PostgreSQL with automatic time-based partitioning, continuous aggregates, and up to 90% data compression. Originally developed by Timescale Inc. to address the limitations of traditional databases when handling massive volumes of time-stamped data, TimescaleDB has become a leading solution for IoT applications, financial markets, and infrastructure monitoring where you need both time-series performance and relational data features. Grafana complements TimescaleDB by providing rich visualization capabilities specifically designed for time-series data, creating dynamic dashboards that can display metrics, events, and trends stored in TimescaleDB hypertables. This combination leverages PostgreSQL's mature ecosystem while delivering the performance characteristics needed for high-volume time-series workloads, with Grafana's templating and alerting features providing operational insights. Organizations managing IoT sensor networks, financial trading systems, or infrastructure monitoring benefit from this stack because it offers the analytical power of SQL joins between time-series and relational data, automatic data lifecycle management through retention policies, and professional-grade visualizations that can handle millions of data points without sacrificing query performance.

Key Features

  • Automatic time-based partitioning (hypertables) for optimal query performance across large datasets
  • Full PostgreSQL compatibility allowing use of existing PostgreSQL tools, extensions, and ORMs
  • Continuous aggregates for real-time materialized views that automatically update as new data arrives
  • Data compression achieving 90%+ storage reduction using TimescaleDB's columnar compression
  • Native PostgreSQL data source integration in Grafana with optimized time-series query support
  • Advanced Grafana templating with dashboard variables for dynamic filtering across time ranges and dimensions
  • Built-in data retention policies for automatic deletion of old data based on time or storage constraints
  • Time_bucket() functions for efficient time-based aggregations and downsampling in visualizations

Common Use Cases

  • 1IoT sensor data collection and monitoring with contextual device metadata and real-time dashboards
  • 2Financial market data storage combining tick data with reference information and trading analytics
  • 3Infrastructure monitoring for long-term storage of Prometheus metrics with custom business dashboards
  • 4Application performance monitoring with database-backed alerting and historical trend analysis
  • 5Energy management systems tracking consumption patterns with billing integration and forecasting
  • 6Manufacturing process monitoring combining sensor readings with production schedules and quality metrics
  • 7Smart city applications aggregating traffic, weather, and utility data with public-facing dashboards

Prerequisites

  • Minimum 2.5GB available RAM (2GB for TimescaleDB, 512MB for Grafana) for production workloads
  • Docker and Docker Compose installed with sufficient disk space for time-series data growth
  • Ports 3000 (Grafana) and 5432 (TimescaleDB) available on the host system
  • Basic SQL knowledge for creating hypertables and time-series queries in TimescaleDB
  • Understanding of time-series data concepts like timestamps, metrics, and aggregation windows
  • Familiarity with PostgreSQL connection parameters for configuring Grafana data sources

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-pg16
4 container_name: timescaledb
5 restart: unless-stopped
6 environment:
7 POSTGRES_USER: ${POSTGRES_USER}
8 POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
9 POSTGRES_DB: ${POSTGRES_DB}
10 volumes:
11 - timescale_data:/var/lib/postgresql/data
12 ports:
13 - "5432:5432"
14 networks:
15 - timescale-network
16
17 grafana:
18 image: grafana/grafana:latest
19 container_name: grafana
20 restart: unless-stopped
21 environment:
22 GF_SECURITY_ADMIN_PASSWORD: ${GRAFANA_PASSWORD}
23 volumes:
24 - grafana_data:/var/lib/grafana
25 ports:
26 - "3000:3000"
27 depends_on:
28 - timescaledb
29 networks:
30 - timescale-network
31
32volumes:
33 timescale_data:
34 grafana_data:
35
36networks:
37 timescale-network:
38 driver: bridge

.env Template

.env
1POSTGRES_USER=postgres
2POSTGRES_PASSWORD=changeme
3POSTGRES_DB=timeseries
4GRAFANA_PASSWORD=admin

Usage Notes

  1. 1Docs: https://docs.timescale.com/
  2. 2Grafana at http://localhost:3000 (admin/admin) | TimescaleDB on port 5432
  3. 3Create hypertable: SELECT create_hypertable('metrics', 'time');
  4. 4Full PostgreSQL compatibility - use any PostgreSQL client/ORM
  5. 5Add Grafana data source: PostgreSQL, host=timescaledb:5432
  6. 6Compression: ALTER TABLE metrics SET (timescaledb.compress);

Individual Services(2 services)

Copy individual services to mix and match with your existing compose files.

timescaledb
timescaledb:
  image: timescale/timescaledb:latest-pg16
  container_name: timescaledb
  restart: unless-stopped
  environment:
    POSTGRES_USER: ${POSTGRES_USER}
    POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    POSTGRES_DB: ${POSTGRES_DB}
  volumes:
    - timescale_data:/var/lib/postgresql/data
  ports:
    - "5432:5432"
  networks:
    - timescale-network
grafana
grafana:
  image: grafana/grafana:latest
  container_name: grafana
  restart: unless-stopped
  environment:
    GF_SECURITY_ADMIN_PASSWORD: ${GRAFANA_PASSWORD}
  volumes:
    - grafana_data:/var/lib/grafana
  ports:
    - "3000:3000"
  depends_on:
    - timescaledb
  networks:
    - timescale-network

Quick Start

terminal
1# 1. Create the compose file
2cat > docker-compose.yml << 'EOF'
3services:
4 timescaledb:
5 image: timescale/timescaledb:latest-pg16
6 container_name: timescaledb
7 restart: unless-stopped
8 environment:
9 POSTGRES_USER: ${POSTGRES_USER}
10 POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
11 POSTGRES_DB: ${POSTGRES_DB}
12 volumes:
13 - timescale_data:/var/lib/postgresql/data
14 ports:
15 - "5432:5432"
16 networks:
17 - timescale-network
18
19 grafana:
20 image: grafana/grafana:latest
21 container_name: grafana
22 restart: unless-stopped
23 environment:
24 GF_SECURITY_ADMIN_PASSWORD: ${GRAFANA_PASSWORD}
25 volumes:
26 - grafana_data:/var/lib/grafana
27 ports:
28 - "3000:3000"
29 depends_on:
30 - timescaledb
31 networks:
32 - timescale-network
33
34volumes:
35 timescale_data:
36 grafana_data:
37
38networks:
39 timescale-network:
40 driver: bridge
41EOF
42
43# 2. Create the .env file
44cat > .env << 'EOF'
45POSTGRES_USER=postgres
46POSTGRES_PASSWORD=changeme
47POSTGRES_DB=timeseries
48GRAFANA_PASSWORD=admin
49EOF
50
51# 3. Start the services
52docker compose up -d
53
54# 4. View logs
55docker compose logs -f

One-Liner

Run this command to download and set up the recipe in one step:

terminal
1curl -fsSL https://docker.recipes/api/recipes/timescaledb/run | bash

Troubleshooting

  • Grafana shows 'database connection failed': Verify POSTGRES_USER and POSTGRES_PASSWORD match in both services and TimescaleDB container is fully started
  • TimescaleDB queries are slow on large tables: Ensure tables are converted to hypertables using SELECT create_hypertable('table_name', 'time_column') before inserting data
  • Grafana dashboards timeout on large time ranges: Use time_bucket() aggregation functions instead of raw data queries and enable TimescaleDB compression
  • TimescaleDB container fails to start with 'data directory not empty': Remove the timescale_data volume or check for conflicting PostgreSQL installations on port 5432
  • Grafana shows 'plugin not found' for PostgreSQL: Restart Grafana container as the PostgreSQL data source plugin is built-in and may need reinitialization
  • High memory usage in TimescaleDB: Tune shared_buffers and effective_cache_size in postgresql.conf, or add custom configuration via volume mount

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