A guide to installing TimescaleDB for financial data on an Ubuntu server. TimescaleDB official docs: https://docs.timescale.com/
This guide was created using Ubuntu 20.04 LTS and PostgreSQL 14
Fresh Install
If you want to install postgres by combining multiple drives into a logical volume (useful for cloud block storage, or large local database), please skip to the If using Block Storage (AWS / Digital Ocean / Etc) section. If you are installing locally, using a single drive, continue below:
Install PostgreSQL-14
Update apt, find latest version of Postgres and install
Add the TimescaleDB third party repository and install TimescaleDB. This command downloads any required dependencies from the PostgreSQL repository:
sudo sh -c "echo 'deb [signed-by=/usr/share/keyrings/timescale.keyring] https://packagecloud.io/timescale/timescaledb/ubuntu/ $(lsb_release -c -s) main' > /etc/apt/sources.list.d/timescaledb.list"
wget --quiet -O - https://packagecloud.io/timescale/timescaledb/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/timescale.keyring
sudo apt-get update
# Now install appropriate package for PG version
sudo apt install timescaledb-2-postgresql-14
There are a variety of settings that can be configured for your new database. At a minimum, you need to update your postgresql.conf file to include shared_preload_libraries = 'timescaledb'. The easiest way to get started is to run timescaledb-tune, which is installed by default when using apt:
sudo timescaledb-tune
# Restart PostgreSQL instance
sudo service postgresql restart
Creating a Hypertable
Create a new hypertable with the same index and structure of an existing Postgres table, copy over the data, and set a compression policy:
CREATE TABLE new_table (LIKE old_table INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING INDEXES);
-- Assuming 'time' is the time column for the dataset
SELECT create_hypertable('new_table', 'time');
-- If there's an error on 'create_hypertable' run the following two statements, otherwise skip this step:
-- CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;
-- SELECT create_hypertable('new_table', 'time');
-- Insert everything from old_table
INSERT INTO new_table SELECT * FROM old_table;
-- Add compression, segmented by symbol/ticker
ALTER TABLE new_table SET (
timescaledb.compress,
timescaledb.compress_segmentby = 'symbol'
);
-- Add automatic compression at a custom interval:
SELECT add_compression_policy('new_table', INTERVAL '7 days');
-- More information on compression here: https://docs.timescale.com/timescaledb/latest/how-to-guides/compression/