Install and configure TimescaleDB with PostgreSQL for high-performance time-series data

Intermediate 45 min Apr 01, 2026 29 views
Ubuntu 24.04 Ubuntu 22.04 Debian 12 AlmaLinux 9 Rocky Linux 9 Fedora 41

Learn how to install TimescaleDB extension on PostgreSQL for handling time-series data at scale. This tutorial covers hypertables setup, compression policies, performance tuning, and security hardening with SSL.

Prerequisites

  • Root or sudo access
  • At least 2GB RAM
  • 50GB free disk space
  • Network access for package downloads

What this solves

TimescaleDB transforms PostgreSQL into a high-performance time-series database, ideal for IoT data, metrics storage, and analytics workloads. This tutorial shows you how to install TimescaleDB, create hypertables for automatic data partitioning, set up compression and retention policies, and secure your deployment with SSL.

Step-by-step installation

Update system packages

Start by updating your package manager to ensure you get the latest versions of all components.

sudo apt update && sudo apt upgrade -y
sudo dnf update -y

Install PostgreSQL

Install PostgreSQL server and client tools. We'll use PostgreSQL 16 for optimal TimescaleDB compatibility.

sudo apt install -y postgresql-16 postgresql-client-16 postgresql-contrib-16
sudo dnf install -y postgresql-server postgresql-contrib
sudo postgresql-setup --initdb

Start and enable PostgreSQL

Enable PostgreSQL to start automatically on boot and start the service.

sudo systemctl enable --now postgresql
sudo systemctl status postgresql

Add TimescaleDB repository

Add the official TimescaleDB repository to get the latest version of the extension.

echo "deb https://packagecloud.io/timescale/timescaledb/ubuntu/ $(lsb_release -c -s) main" | sudo tee /etc/apt/sources.list.d/timescaledb.list
wget --quiet -O - https://packagecloud.io/timescale/timescaledb/gpgkey | sudo apt-key add -
sudo apt update
sudo tee /etc/yum.repos.d/timescale_timescaledb.repo <

Install TimescaleDB extension

Install the TimescaleDB extension package that matches your PostgreSQL version.

sudo apt install -y timescaledb-2-postgresql-16
sudo dnf install -y timescaledb-postgresql

Configure PostgreSQL for TimescaleDB

Run the TimescaleDB tune utility to optimize PostgreSQL settings for time-series workloads.

sudo timescaledb-tune --quiet --yes

Restart PostgreSQL

Restart PostgreSQL to apply the tuned configuration settings.

sudo systemctl restart postgresql

Create TimescaleDB database

Create a dedicated database for your time-series data and enable the TimescaleDB extension.

sudo -u postgres createdb tsdb
sudo -u postgres psql -d tsdb -c "CREATE EXTENSION IF NOT EXISTS timescaledb;"

Create database user

Create a dedicated user for your TimescaleDB applications with appropriate permissions.

sudo -u postgres psql -d tsdb -c "CREATE USER tsdb_user WITH PASSWORD 'secure_password_123!';"
sudo -u postgres psql -d tsdb -c "GRANT ALL PRIVILEGES ON DATABASE tsdb TO tsdb_user;"
sudo -u postgres psql -d tsdb -c "GRANT ALL ON SCHEMA public TO tsdb_user;"

Configure hypertables and compression

Create a sample hypertable

Create a table for sensor data and convert it to a hypertable for automatic partitioning by time.

sudo -u postgres psql -d tsdb
CREATE TABLE sensor_data (
    time TIMESTAMPTZ NOT NULL,
    device_id INTEGER NOT NULL,
    temperature DOUBLE PRECISION,
    humidity DOUBLE PRECISION,
    location TEXT
);

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

Enable compression

Enable compression on the hypertable to reduce storage requirements for older data.

ALTER TABLE sensor_data SET (timescaledb.compress,
    timescaledb.compress_segmentby = 'device_id',
    timescaledb.compress_orderby = 'time DESC');

Set up compression policy

Create a policy to automatically compress data older than 7 days.

SELECT add_compression_policy('sensor_data', INTERVAL '7 days');

Set up retention policy

Create a retention policy to automatically delete data older than 1 year.

SELECT add_retention_policy('sensor_data', INTERVAL '1 year');
\q

Configure SSL security

Generate SSL certificates

Create self-signed SSL certificates for secure connections. For production, use certificates from a trusted CA.

sudo -u postgres openssl req -new -x509 -days 365 -nodes -text \
    -out /var/lib/postgresql/server.crt \
    -keyout /var/lib/postgresql/server.key \
    -subj "/CN=example.com"
sudo -u postgres chmod 600 /var/lib/postgresql/server.key

Configure PostgreSQL for SSL

Enable SSL in the PostgreSQL configuration and require encrypted connections.

ssl = on
ssl_cert_file = '/var/lib/postgresql/server.crt'
ssl_key_file = '/var/lib/postgresql/server.key'
ssl_prefer_server_ciphers = on
ssl_ciphers = 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256'

Performance tuning

shared_preload_libraries = 'timescaledb' max_connections = 200 shared_buffers = 256MB effective_cache_size = 1GB maintenance_work_mem = 64MB checkpoint_completion_target = 0.9 wal_buffers = 16MB default_statistics_target = 100 random_page_cost = 1.1 effective_io_concurrency = 200

Configure client authentication

Update pg_hba.conf to require SSL connections and use strong authentication.

# TYPE  DATABASE        USER            ADDRESS                 METHOD

Require SSL for all connections

hostssl all all 0.0.0.0/0 md5 hostssl all all ::/0 md5

Local connections

local all postgres peer local all all peer

Configure firewall

Open PostgreSQL port 5432 for secure connections.

sudo ufw allow 5432/tcp
sudo ufw reload
sudo firewall-cmd --permanent --add-port=5432/tcp
sudo firewall-cmd --reload

Restart PostgreSQL

Restart PostgreSQL to apply SSL and configuration changes.

sudo systemctl restart postgresql

Set up monitoring and backups

Create monitoring script

Create a script to monitor TimescaleDB performance and hypertable statistics.

#!/bin/bash

echo "=== TimescaleDB Status ==="
psql -d tsdb -U tsdb_user -h localhost -c "SELECT * FROM timescaledb_information.hypertables;"

echo "=== Compression Stats ==="
psql -d tsdb -U tsdb_user -h localhost -c "SELECT * FROM timescaledb_information.compressed_chunk_stats;"

echo "=== Database Size ==="
psql -d tsdb -U tsdb_user -h localhost -c "SELECT pg_size_pretty(pg_database_size('tsdb'));"

echo "=== Active Connections ==="
psql -d tsdb -U tsdb_user -h localhost -c "SELECT count(*) FROM pg_stat_activity WHERE datname = 'tsdb';"
sudo chmod +x /usr/local/bin/tsdb-monitor.sh

Set up backup script

Create an automated backup script for your TimescaleDB database.

#!/bin/bash

BACKUP_DIR="/var/backups/timescaledb"
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="$BACKUP_DIR/tsdb_backup_$DATE.sql"

mkdir -p $BACKUP_DIR

echo "Starting TimescaleDB backup..."
pg_dump -h localhost -U tsdb_user -d tsdb > $BACKUP_FILE

if [ $? -eq 0 ]; then
    echo "Backup completed: $BACKUP_FILE"
    gzip $BACKUP_FILE
    # Keep only last 7 days of backups
    find $BACKUP_DIR -name "*.gz" -mtime +7 -delete
else
    echo "Backup failed!"
    exit 1
fi
sudo chmod +x /usr/local/bin/tsdb-backup.sh
sudo mkdir -p /var/backups/timescaledb
sudo chown postgres:postgres /var/backups/timescaledb

Schedule automated backups

Add a cron job to run daily backups at 2 AM.

sudo -u postgres crontab -e
0 2   * /usr/local/bin/tsdb-backup.sh

Verify your setup

# Check TimescaleDB version
psql -d tsdb -U tsdb_user -h localhost -c "SELECT * FROM timescaledb_information.license;"

Test SSL connection

psql "sslmode=require host=localhost dbname=tsdb user=tsdb_user"

Insert sample data

psql -d tsdb -U tsdb_user -h localhost -c " INSERT INTO sensor_data (time, device_id, temperature, humidity, location) VALUES (NOW(), 1, 23.5, 60.2, 'Building A'), (NOW() - INTERVAL '1 hour', 1, 24.1, 58.7, 'Building A'), (NOW() - INTERVAL '2 hours', 2, 22.8, 62.1, 'Building B');"

Query the data

psql -d tsdb -U tsdb_user -h localhost -c "SELECT * FROM sensor_data ORDER BY time DESC;"

Check hypertable chunks

psql -d tsdb -U tsdb_user -h localhost -c "SELECT * FROM timescaledb_information.chunks WHERE hypertable_name = 'sensor_data';"
Note: Your TimescaleDB installation integrates well with monitoring solutions like Grafana and Prometheus for visualization and alerting.

Common issues

SymptomCauseFix
Extension not found errorTimescaleDB not installed correctlyReinstall with sudo apt install timescaledb-2-postgresql-16
SSL connection refusedSSL not configured properlyCheck certificate permissions and postgresql.conf SSL settings
Permission denied on backupWrong directory ownershipRun sudo chown postgres:postgres /var/backups/timescaledb
Hypertable creation failsExtension not enabledRun CREATE EXTENSION timescaledb; in target database
Poor query performanceMissing indexes on commonly queried columnsCreate indexes on device_id and other filter columns
Compression policy not workingBackground worker not runningCheck timescaledb.max_background_workers setting
Never use chmod 777. It gives every user on the system full access to your files. Instead, use specific permissions like 600 for private keys and proper ownership with chown.

Next steps

Automated install script

Run this to automate the entire setup

#timescaledb #time-series-database #postgresql-extension #hypertables #timescaledb-compression #iot-database #metrics-storage #timescaledb-ssl

Need help?

Don't want to manage this yourself?

We handle infrastructure for businesses that depend on uptime. From initial setup to ongoing operations.

Talk to an engineer