timescaledb
A time-series database for high-performance real-time analytics packaged as a Postgres extension
Top Related Projects
Scalable datastore for metrics, events, and real-time analytics
Apache Druid: a high performance real-time analytics database.
The Prometheus monitoring system and time series database.
QuestDB is a high performance, open-source, time-series database
ClickHouse® is a real-time analytics database management system
Apache Pinot - A realtime distributed OLAP datastore
Quick Overview
TimescaleDB is an open-source database designed to make SQL scalable for time-series data. It is implemented as an extension to PostgreSQL, providing automatic partitioning across time and space, as well as full SQL support. TimescaleDB is optimized for fast ingest and complex queries, making it ideal for IoT, financial data, and monitoring applications.
Pros
- Seamless integration with PostgreSQL, leveraging its robust ecosystem and features
- Automatic time-based partitioning for improved query performance and data management
- Supports both time-series and relational workloads in a single database
- Offers advanced features like continuous aggregates, data retention policies, and compression
Cons
- Requires PostgreSQL as a base system, which may not be suitable for all use cases
- Learning curve for users not familiar with PostgreSQL or time-series databases
- Performance may not match specialized time-series databases in certain scenarios
- Limited support for distributed deployments compared to some other time-series solutions
Code Examples
- Creating a hypertable:
CREATE TABLE sensor_data (
time TIMESTAMPTZ NOT NULL,
sensor_id INTEGER NOT NULL,
temperature DOUBLE PRECISION NULL,
humidity DOUBLE PRECISION NULL
);
SELECT create_hypertable('sensor_data', 'time');
- Inserting data into a hypertable:
INSERT INTO sensor_data (time, sensor_id, temperature, humidity)
VALUES (NOW(), 1, 25.6, 47.3),
(NOW() - INTERVAL '1 hour', 2, 23.4, 51.2);
- Querying time-series data with time bucketing:
SELECT time_bucket('1 hour', time) AS hour,
AVG(temperature) AS avg_temp,
AVG(humidity) AS avg_humidity
FROM sensor_data
WHERE time > NOW() - INTERVAL '24 hours'
GROUP BY hour
ORDER BY hour;
Getting Started
To get started with TimescaleDB:
- Install PostgreSQL and TimescaleDB extension (follow instructions on the official website).
- Create a new database and enable the extension:
CREATE DATABASE timeseries_db;
\c timeseries_db
CREATE EXTENSION IF NOT EXISTS timescaledb;
- Create a table and convert it to a hypertable:
CREATE TABLE metrics (
time TIMESTAMPTZ NOT NULL,
device_id INTEGER,
value DOUBLE PRECISION
);
SELECT create_hypertable('metrics', 'time');
- Start inserting and querying data using standard SQL commands.
Competitor Comparisons
Scalable datastore for metrics, events, and real-time analytics
Pros of InfluxDB
- Purpose-built for time-series data, offering optimized performance for time-based queries
- Flexible data model with tags and fields, allowing for efficient storage and querying of high-cardinality data
- Built-in data retention policies and continuous queries for automated data management
Cons of InfluxDB
- Limited support for complex joins and relational queries compared to TimescaleDB's SQL capabilities
- Lacks some advanced PostgreSQL features that TimescaleDB inherits, such as full ACID compliance and rich indexing options
Code Comparison
InfluxDB query example:
SELECT mean("value") FROM "cpu_usage"
WHERE "host" = 'server01' AND time >= now() - 1h
GROUP BY time(5m)
TimescaleDB query example:
SELECT time_bucket('5 minutes', time) AS five_min,
avg(value) AS mean_value
FROM cpu_usage
WHERE host = 'server01' AND time >= now() - interval '1 hour'
GROUP BY five_min
ORDER BY five_min;
Both databases offer efficient time-series querying, but TimescaleDB uses standard SQL syntax, while InfluxDB uses its own query language (InfluxQL or Flux). TimescaleDB's SQL approach may be more familiar to developers with relational database experience.
Apache Druid: a high performance real-time analytics database.
Pros of Druid
- Designed for real-time analytics on large-scale streaming data
- Highly scalable and distributed architecture
- Supports complex aggregations and fast ad-hoc queries
Cons of Druid
- Steeper learning curve and more complex setup
- Limited support for updating or deleting existing data
- Higher resource requirements for smaller datasets
Code Comparison
Druid Query Example
SELECT COUNT(*) AS count,
SUM(price) AS total_price
FROM transactions
WHERE timestamp >= CURRENT_TIMESTAMP - INTERVAL '1' DAY
GROUP BY product_id
HAVING COUNT(*) > 100
TimescaleDB Query Example
SELECT COUNT(*) AS count,
SUM(price) AS total_price
FROM transactions
WHERE time > NOW() - INTERVAL '1 day'
GROUP BY product_id
HAVING COUNT(*) > 100
Both Druid and TimescaleDB are designed for time-series data, but they have different strengths. Druid excels in real-time analytics and scalability for large datasets, while TimescaleDB offers better SQL compatibility and easier integration with existing PostgreSQL ecosystems. The code examples show similar query syntax, with minor differences in time-related functions and column naming conventions.
The Prometheus monitoring system and time series database.
Pros of Prometheus
- Built-in alerting and monitoring system with a powerful query language (PromQL)
- Native support for multi-dimensional data and time series
- Extensive ecosystem with various exporters and integrations
Cons of Prometheus
- Limited long-term storage capabilities
- Lacks built-in downsampling and data retention policies
- Scaling horizontally can be challenging
Code Comparison
TimescaleDB (SQL query):
SELECT time_bucket('1 hour', time) AS hour,
avg(temperature) AS avg_temp
FROM sensor_data
WHERE time > NOW() - INTERVAL '1 day'
GROUP BY hour
ORDER BY hour;
Prometheus (PromQL query):
avg_over_time(temperature[1h])
Additional Notes
TimescaleDB is an extension of PostgreSQL, offering advanced time-series capabilities while maintaining full SQL compatibility. It excels in handling large volumes of time-series data with efficient storage and querying.
Prometheus, on the other hand, is a complete monitoring and alerting toolkit designed for reliability and scalability in dynamic environments. It's particularly well-suited for microservices architectures and cloud-native applications.
While both handle time-series data, they serve different primary purposes: TimescaleDB as a database solution and Prometheus as a monitoring system. The choice between them depends on specific use cases and requirements.
QuestDB is a high performance, open-source, time-series database
Pros of QuestDB
- Designed for high-performance time-series data processing with zero dependencies
- Supports SQL with extensions for time-series operations
- Offers out-of-the-box web console for data exploration and visualization
Cons of QuestDB
- Smaller ecosystem and community compared to TimescaleDB
- Limited advanced features like continuous aggregates and data retention policies
- Less mature and battle-tested in production environments
Code Comparison
QuestDB SQL query:
SELECT date_trunc('hour', timestamp) AS hour,
avg(temperature) AS avg_temp
FROM sensors
WHERE location = 'NYC'
GROUP BY hour
ORDER BY hour DESC
LIMIT 24;
TimescaleDB SQL query:
SELECT time_bucket('1 hour', timestamp) AS hour,
avg(temperature) AS avg_temp
FROM sensors
WHERE location = 'NYC'
GROUP BY hour
ORDER BY hour DESC
LIMIT 24;
Both databases support SQL queries for time-series data, with slight differences in function names (e.g., date_trunc
vs time_bucket
). TimescaleDB leverages PostgreSQL's ecosystem, while QuestDB focuses on a lightweight, high-performance approach for time-series data.
ClickHouse® is a real-time analytics database management system
Pros of ClickHouse
- Exceptional performance for analytical queries on large datasets
- Highly scalable and distributed architecture
- Supports a wide range of data types and storage engines
Cons of ClickHouse
- Steeper learning curve due to its unique query language and architecture
- Limited support for real-time updates and deletes
- Less suitable for OLTP workloads compared to TimescaleDB
Code Comparison
ClickHouse query example:
SELECT date, count() AS count
FROM events
GROUP BY date
ORDER BY date
TimescaleDB query example:
SELECT time_bucket('1 day', time) AS date, count(*)
FROM events
GROUP BY date
ORDER BY date
Both examples show a simple time-based aggregation query. ClickHouse uses its native syntax, while TimescaleDB extends PostgreSQL with time-bucket functions.
ClickHouse is optimized for analytical queries on large datasets, offering superior performance for complex aggregations. TimescaleDB, built on PostgreSQL, provides a more familiar SQL interface and better support for real-time updates and OLTP workloads.
Choose ClickHouse for high-performance analytics on massive datasets, and TimescaleDB for a balance between time-series capabilities and traditional relational database features.
Apache Pinot - A realtime distributed OLAP datastore
Pros of Pinot
- Designed for real-time analytics with low latency querying on large datasets
- Supports multiple data ingestion methods, including streaming and batch
- Highly scalable and distributed architecture for handling massive data volumes
Cons of Pinot
- Steeper learning curve and more complex setup compared to TimescaleDB
- Requires more resources and infrastructure to run effectively
- Less mature ecosystem and fewer integrations with existing tools
Code Comparison
TimescaleDB (SQL-based):
CREATE TABLE metrics (
time TIMESTAMPTZ NOT NULL,
device_id INTEGER,
temperature FLOAT,
cpu_usage FLOAT
);
SELECT time_bucket('1 hour', time) AS hour,
AVG(temperature) AS avg_temp
FROM metrics
WHERE time > NOW() - INTERVAL '24 hours'
GROUP BY hour
ORDER BY hour;
Pinot (PQL-based):
SELECT DATETIMECONVERT(ts, '1:MILLISECONDS:EPOCH', '1:HOURS:EPOCH', '1:HOURS') AS hour,
AVG(temperature) AS avg_temp
FROM metrics
WHERE ts > FromDateTime(now(), 'yyyy-MM-dd HH:mm:ss') - 24 * 3600 * 1000
GROUP BY hour
ORDER BY hour
LIMIT 10000
Both TimescaleDB and Pinot offer powerful solutions for time-series data, with TimescaleDB providing a more familiar PostgreSQL-based approach and Pinot focusing on real-time analytics at scale.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
TimescaleDB is a PostgreSQL extension for high-performance real-time analytics on time-series and event data
Install TimescaleDB
Install from a Docker container:
-
Run the TimescaleDB container:
docker run -d --name timescaledb -p 5432:5432 -e POSTGRES_PASSWORD=password timescale/timescaledb:latest-pg17
-
Connect to a database:
docker exec -it timescaledb psql -d "postgres://postgres:password@localhost/postgres"
See other installation options or try Timescale Cloud for free.
Create a hypertable
You create a regular table and then convert it into a hypertable. A hypertable automatically partitions data into chunks to accelerate your queries.
-- Create timescaledb extension
CREATE EXTENSION IF NOT EXISTS timescaledb;
-- Create a regular SQL table
CREATE TABLE conditions (
time TIMESTAMPTZ NOT NULL,
location TEXT NOT NULL,
temperature DOUBLE PRECISION NULL,
humidity DOUBLE PRECISION NULL
);
-- Convert the table into a hypertable that is partitioned by time
SELECT create_hypertable('conditions', by_range('time'));
See more:
Enable columnstore
TimescaleDB's hypercore is a hybrid row-columnar store that boosts analytical query performance on your time-series and event data, while reducing data size by more than 90%. This keeps your analytics operating at lightning speed and ensures low storage costs as you scale. Data is inserted in row format in the rowstore and converted to columnar format in the columnstore based on your configuration.
-
Configure the columnstore on a hypertable:
ALTER TABLE conditions SET ( timescaledb.compress, timescaledb.compress_segmentby = 'location' );
-
Create a policy to automatically convert chunks in row format that are older than seven days to chunks in the columnar format:
SELECT add_compression_policy('conditions', INTERVAL '7 days');
See more:
Insert and query data
Insert and query data in a hypertable via regular SQL commands. For example:
-
Insert data into a hypertable named
conditions
:INSERT INTO conditions VALUES (NOW(), 'office', 70.0, 50.0), (NOW(), 'basement', 66.5, 60.0), (NOW(), 'garage', 77.0, 65.2);
-
Return the number of entries written to the table conditions in the last 12 hours:
SELECT COUNT(*) FROM conditions WHERE time > NOW() - INTERVAL '12 hours';
See more:
Create time buckets
Time buckets enable you to aggregate data in hypertables by time interval and calculate summary values.
For example, calculate the average daily temperature in a table named conditions
. The table has a time
and temperature
columns:
SELECT
time_bucket('1 day', time) AS bucket,
AVG(temperature) AS avg_temp
FROM
conditions
GROUP BY
bucket
ORDER BY
bucket ASC;
See more:
Create continuous aggregates
Continuous aggregates make real-time analytics run faster on very large datasets. They continuously and incrementally refresh a query in the background, so that when you run such query, only the data that has changed needs to be computed, not the entire dataset. This is what makes them different from regular PostgreSQL materialized views, which cannot be incrementally materialized and have to be rebuilt from scratch every time you want to refresh it.
For example, create a continuous aggregate view for daily weather data in two simple steps:
-
Create a materialized view:
CREATE MATERIALIZED VIEW conditions_summary_daily WITH (timescaledb.continuous) AS SELECT location, time_bucket(INTERVAL '1 day', time) AS bucket, AVG(temperature), MAX(temperature), MIN(temperature) FROM conditions GROUP BY location, bucket;
-
Create a policy to refresh the view every hour:
SELECT add_continuous_aggregate_policy( 'conditions_summary_daily', start_offset => INTERVAL '1 month', end_offset => INTERVAL '1 day', schedule_interval => INTERVAL '1 hour' );
See more:
Want TimescaleDB hosted and managed for you? Try Timescale Cloud
Timescale Cloud is the modern PostgreSQL data platform for all your applications. It enhances PostgreSQL to handle time series, events, real-time analytics, and vector searchâall in a single database alongside transactional workloads. You get one system that handles live data ingestion, late and out-of-order updates, and low latency queries, with the performance, reliability, and scalability your app needs. Ideal for IoT, crypto, finance, SaaS, and a myriad other domains, Timescale Cloud allows you to build data-heavy, mission-critical apps while retaining the familiarity and reliability of PostgreSQL.
A Timescale Cloud service is a single optimized 100% PostgreSQL database instance that you use as is, or extend with capabilities specific to your business needs. The available capabilities are:
- Time-series and analytics: PostgreSQL with TimescaleDB. The PostgreSQL you know and love, supercharged with functionality for storing and querying time-series data at scale for real-time analytics and other use cases. Get faster time-based queries with hypertables, continuous aggregates, and columnar storage. Save on storage with native compression, data retention policies, and bottomless data tiering to Amazon S3.
- AI and vector: PostgreSQL with vector extensions. Use PostgreSQL as a vector database with purpose built extensions for building AI applications from start to scale. Get fast and accurate similarity search with the pgvector and pgvectorscale extensions. Create vector embeddings and perform LLM reasoning on your data with the pgai extension.
- PostgreSQL: the trusted industry-standard RDBMS. Ideal for applications requiring strong data consistency, complex relationships, and advanced querying capabilities. Get ACID compliance, extensive SQL support, JSON handling, and extensibility through custom functions, data types, and extensions. All services include all the cloud tooling you'd expect for production use: automatic backups, high availability, read replicas, data forking, connection pooling, tiered storage, usage-based storage, and much more.
Check build status
Linux/macOS | Linux i386 | Windows | Coverity | Code Coverage | OpenSSF |
---|---|---|---|---|---|
Get involved
We welcome contributions to TimescaleDB! See Contributing and Code style guide for details.
Learn about Timescale
Timescale is PostgreSQL made powerful. To learn more about the company and its products, visit timescale.com.
Top Related Projects
Scalable datastore for metrics, events, and real-time analytics
Apache Druid: a high performance real-time analytics database.
The Prometheus monitoring system and time series database.
QuestDB is a high performance, open-source, time-series database
ClickHouse® is a real-time analytics database management system
Apache Pinot - A realtime distributed OLAP datastore
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot