Sling Academy
Home/PostgreSQL/Installing and Configuring TimescaleDB with PostgreSQL

Installing and Configuring TimescaleDB with PostgreSQL

Last updated: December 20, 2024

TimescaleDB is a time-series database built on top of PostgreSQL, designed to provide scalable and efficient time-series data management. It's especially useful for applications such as IoT, DevOps monitoring, and financial data analysis. This article covers the steps for installing and configuring TimescaleDB alongside PostgreSQL.

Prerequisites

Before you begin, ensure you have:

  • An operational instance of PostgreSQL. If you don't have PostgreSQL installed, you can refer to the official documentation for installation guidelines relevant to your operating system.
  • Administrative privileges to install software packages.

Installation Steps

1. Install TimescaleDB

TimescaleDB can be installed via various methods, such as using pre-built packages or compiling from source. This guide will focus on using pre-built packages, which is the most straightforward method. The following example demonstrates using Ubuntu, but detailed instructions for other operating systems can be found in the TimescaleDB documentation.

# Add TimescaleDB's third-party apt repository
sudo add-apt-repository "deb https://packagecloud.io/timescale/timescaledb/ubuntu/ $(lsb_release -c -s) main"

# Import the TimescaleDB GPG key
wget --quiet -O - https://packagecloud.io/timescale/timescaledb/gpgkey | sudo apt-key add -

# Update the package database
sudo apt-get update

# Install TimescaleDB
sudo apt-get install timescaledb-2-postgresql-13

2. Configure TimescaleDB in PostgreSQL

Once TimescaleDB is installed, we need to configure PostgreSQL to use it. This involves editing the postgresql.conf file.

# Open the PostgreSQL configuration file (exact file path may vary)
sudo nano /etc/postgresql/13/main/postgresql.conf

# Add or uncomment the following line to include TimescaleDB
shared_preload_libraries = 'timescaledb'

After adjusting the configuration file, restart PostgreSQL for changes to take effect:

# Restart PostgreSQL
sudo service postgresql restart

Verifying the Installation

1. Connect and Create a TimescaleDB

To ensure everything is set up correctly, connect to your PostgreSQL instance and create a new TimescaleDB database:

-- Connect to PostgreSQL
psql -U postgres

-- Create a new database
CREATE DATABASE exampledb;

-- Connect to the database
\c exampledb

-- Create the TimescaleDB extension
CREATE EXTENSION IF NOT EXISTS timescaledb;

2. Check installation

Verify that TimescaleDB is correctly installed and running:

-- List installed extensions
\dx

You should see TimescaleDB listed among the extensions if it was installed correctly.

Using TimescaleDB

With TimescaleDB successfully installed and configured, you can now start taking advantage of its time-series capabilities.

Creating a Hypertable

Hypertables are TimescaleDB's abstraction that automatically partitions time-series data across many tables. They appear as a single table to queries, allowing for easy data management without complex partitioning logic.

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

SELECT create_hypertable('conditions', 'time');

Your data collection and queries can now span large periods due to the seamless scalability offered by TimescaleDB with no additional overhead on your design.

Conclusion

TimescaleDB is a valuable extension to PostgreSQL that adds native time-series data management capabilities. This guide aimed to show you how easy it is to install and leverage TimescaleDB for handling time-series data efficiently.

Next Article: PostgreSQL with TimescaleDB: Best Practices for Time-Series Database Design

Previous Article: Why Use TimescaleDB with PostgreSQL for Time-Series Data

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