Sling Academy
Home/PostgreSQL/TimescaleDB: Combining Relational and Time-Series Data in PostgreSQL

TimescaleDB: Combining Relational and Time-Series Data in PostgreSQL

Last updated: December 21, 2024

In the modern data-driven world, the need to efficiently store and analyze time-series data alongside traditional relational data has led to the rise of hybrid databases like TimescaleDB. This robust extension to PostgreSQL provides the best of both worlds: the power of relational databases with the unique capabilities required to manage time-series data. This article explores how you can tap into the strengths of TimescaleDB to handle time-series workloads within PostgreSQL's familiar environment.

Understanding TimescaleDB

TimescaleDB extends PostgreSQL by integrating time-series data capabilities with minimal overhead. It leverages the existing PostgreSQL ecosystem, offering seamless integration and enhanced performance for time-series workloads. Typical use cases for TimescaleDB include monitoring, metrics collection, financial data analysis, and Internet of Things (IoT) applications, which generate large volumes of time-stamped data.

Getting Started with TimescaleDB

To start using TimescaleDB, you first need a PostgreSQL server installed. If you haven’t already set it up, download and install PostgreSQL on your system. With PostgreSQL in place, the next step is to install the TimescaleDB extension.

# Add TimescaleDB repository
echo "deb https://packagecloud.io/timescale/timescaledb/debian/ stretch main" | sudo tee /etc/apt/sources.list.d/timescale_timescaledb.list
wget -qO - https://packagecloud.io/timescale/timescaledb/gpgkey | sudo apt-key add -
sudo apt-get update

# Install TimescaleDB
dpkg --get-selections | grep postgresql
sudo apt install timescaledb-postgresql-12

After installation, you must configure PostgreSQL to enable the TimescaleDB extension:

-- Connect to PostgreSQL database
psql -U your_username -d your_database

-- Enable TimescaleDB extension
CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;

Creating a Hybrid Time-Series Table

With TimescaleDB enabled, the next step is to define your time-series data. A typical approach involves creating a "hypertable," a special kind of table optimized for time-series workloads. Let's create a basic hypertable to store temperature sensor data:

CREATE TABLE temperature_data (
    time        TIMESTAMPTZ       NOT NULL,
    location    TEXT              NOT NULL,
    temperature DOUBLE PRECISION  NOT NULL
);

-- Convert the regular table into a hypertable
timescaledb_information.table_sizes
SELECT create_hypertable('temperature_data', 'time');

This setup not only supports large writes and queries but also leverages PostgreSQL’s indexing and query optimizations for relational data.

Inserting and Querying Data

Inserting data into a TimescaleDB hypertable works just like any other PostgreSQL table, ensuring compatibility and ease of use:

INSERT INTO temperature_data (time, location, temperature)
VALUES
'timestamp'(
    ('2023-10-08 10:34:23+00','New York', 22.5),
    ('2023-10-08 10:37:01+00','Los Angeles', 27.3)
);

Similarly, you can perform complex queries that combine both relational and time-series data considerations:

SELECT 
    location, 
    AVG(temperature) as avg_temp
FROM 
    temperature_data
WHERE 
    time >= NOW() - INTERVAL '24 hours'
GROUP BY location;

Advanced Features

TimescaleDB offers several advanced features, such as automated data retention policies and continuous aggregates, enhancing its ability to manage time-series data effectively. Continuous aggregates, for example, can automatically refresh summary views of data, improving query performance for frequent calculations.

To create a continuous aggregate, use the following:

CREATE MATERIALIZED VIEW daily_temperature AS
SELECT 
    time_bucket('1 day', time) as day, 
    location,
    AVG(temperature) as avg_temp
FROM 
    temperature_data
GROUP BY day, location;

SELECT refresh_continuous_aggregate('daily_temperature', NOW() - INTERVAL '1 day', NOW());

Conclusion

TimescaleDB offers a comprehensive solution for integrating time-series data compression and analysis within PostgreSQL's robust ecosystem. By following the steps outlined, you can combine relational and time-series data management into a single, efficient system. This compatibility and performance make TimescaleDB a powerful tool for a wide range of applications requiring real-time analytics and historical data analysis. Exploring TimescaleDB's functionalities can significantly enhance your data handling capabilities.

Next Article: PostgreSQL with TimescaleDB: How to Visualize Time-Series Data with Grafana

Previous Article: PostgreSQL with TimescaleDB: Using `pg_prometheus` for Metrics Storage

Series: PostgreSQL Tutorials: From Basic to Advanced

PostgreSQL

You May Also Like

  • PostgreSQL with TimescaleDB: Querying Time-Series Data with SQL
  • PostgreSQL Full-Text Search with Boolean Operators
  • Filtering Stop Words in PostgreSQL Full-Text Search
  • PostgreSQL command-line cheat sheet
  • How to Perform Efficient Rolling Aggregations with TimescaleDB
  • PostgreSQL with TimescaleDB: Migrating from Traditional Relational Models
  • Best Practices for Maintaining PostgreSQL and TimescaleDB Databases
  • PostgreSQL with TimescaleDB: Building a High-Performance Analytics Engine
  • Integrating PostgreSQL and TimescaleDB with Machine Learning Models
  • PostgreSQL with TimescaleDB: Implementing Temporal Data Analysis
  • Combining PostgreSQL, TimescaleDB, and Airflow for Data Workflows
  • PostgreSQL with TimescaleDB: Visualizing Real-Time Data with Superset
  • Using PostgreSQL with TimescaleDB for Energy Consumption Analysis
  • PostgreSQL with TimescaleDB: How to Query Massive Datasets Efficiently
  • Best Practices for Writing Time-Series Queries in PostgreSQL with TimescaleDB
  • PostgreSQL with TimescaleDB: Implementing Batch Data Processing
  • Using PostgreSQL with TimescaleDB for Network Traffic Analysis
  • PostgreSQL with TimescaleDB: Troubleshooting Common Performance Issues
  • Building an IoT Data Pipeline with PostgreSQL and TimescaleDB