This tutorial uses a dataset that contains Bitcoin blockchain data for
the past five days, in a hypertable named transactions
.
A service in Timescale is a cloud instance which contains your database.
Each service contains a single database, named tsdb
.
You can connect to a service from your local system using the psql
command-line utility. If you've used PostgreSQL before, you might already have
psql
installed. If not, check out the installing psql section.
In the Timescale portal, click
Create service
.Click
Download the cheatsheet
to download an SQL file that contains the login details for your new service. You can also copy the details directly from this page. When you have copied your password, clickI stored my password, go to service overview
at the bottom of the page.When your service is ready to use, is shows a green
Running
label in theService Overview
. You also receive an email confirming that your service is ready to use.On your local system, at the command prompt, connect to the service using the
Service URL
from the SQL file that you downloaded. When you are prompted, enter the password:psql -x "<SERVICE_URL>"Password for user tsdbadmin:If your connection is successful, you'll see a message like this, followed by the
psql
prompt:psql (13.3, server 12.8 (Ubuntu 12.8-1.pgdg21.04+1))SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)Type "help" for help.tsdb=>
The dataset is updated daily and contains data from the last five days, typically around 1.5 million Bitcoin transactions. The data includes information about each transaction, including the value of the transaction in satoshi, the smallest denomination of Bitcoin. It also states if a transaction is the first transaction in a block, known as a coinbase transaction, which includes the reward a coin miner receives for mining the coin.
Hypertables are the core of Timescale. Hypertables enable Timescale to work efficiently with time-series data. Because Timescale is PostgreSQL, all the standard PostgreSQL tables, indexes, stored procedures and other objects can be created alongside your Timescale hypertables. This makes creating and working with Timescale tables similar to standard PostgreSQL.
Create a standard PostgreSQL table to store the Bitcoin blockchain data using
CREATE TABLE
:CREATE TABLE transactions (time TIMESTAMPTZ,block_id INT,hash TEXT,size INT,weight INT,is_coinbase BOOLEAN,output_total BIGINT,output_total_usd DOUBLE PRECISION,fee BIGINT,fee_usd DOUBLE PRECISION,details JSONB);Convert the standard table into a hypertable partitioned on the
time
column using thecreate_hypertable()
function provided by Timescale. You must provide the name of the table and the column in that table that holds the timestamp data to use for partitioning:SELECT create_hypertable('transactions', by_range('time'));Note
The
by_range
dimension builder is an addition to TimescaleDB 2.13.Create an index on the
hash
column to make queries for individual transactions faster:CREATE INDEX hash_idx ON public.transactions USING HASH (hash);Create an index on the
block_id
column to make block-level queries faster:CREATE INDEX block_idx ON public.transactions (block_id);Create a unique index on the
time
andhash
columns to make sure you don't accidentally insert duplicate records:CREATE UNIQUE INDEX time_hash_idx ON public.transactions (time, hash);
Note
When you create a hypertable, it is automatically partitioned on the time column you provide as the second parameter to create_hypertable()
. Also, Timescale automatically creates an index on the time column. However, you'll often filter your time-series data on other columns as well. Using indexes appropriately helps your queries perform better.
This tutorial uses Bitcoin transactions from the past five days.
To ingest data into the tables that you created, you need to download the dataset and copy the data to your database.
Download the
bitcoin_sample.zip
file. The file contains a.csv
file that contains Bitcoin transactions for the past five days. Download:In a new terminal window, run this command to unzip the
.csv
files:unzip bitcoin_sample.zipAt the
psql
prompt, use theCOPY
command to transfer data into your Timescale instance. If the.csv
files aren't in your current directory, specify the file paths in these commands:\COPY transactions FROM 'tutorial_bitcoin_sample.csv' CSV HEADER;Because there is over a million rows of data, the
COPY
process could take a few minutes depending on your internet connection and local client resources.
The queries in this tutorial are suitable for graphing in Grafana. If you want to visualize the results of your queries, connect your Grafana account to the Bitcoin blockchain dataset.
Grafana is and open source analytics and monitoring solution. You use Grafana to visualize queries directly from your Timescale Cloud service.
Best practice is to use an Ubuntu EC2 instance hosted in the same region as your Timescale Cloud service as a migration machine. That is, the machine you run the commands on to move your data from your source database to your target Timescale Cloud service.
Before you migrate your data:
Create a target Timescale Cloud service.
Each Timescale Cloud service has a single database that supports the most popular extensions. Timescale Cloud services do not support tablespaces, and there is no superuser associated with a service. Best practice is to create a Timescale Cloud services with at least 8 CPUs for a smoother experience. A higher-spec instance can significantly reduce the overall migration window.
To ensure that maintenance does not run during the process, adjust the maintenance window.
- Install self-managed Grafana, or sign up for Grafana Cloud
To connect the data in your Timescale Cloud service to Grafana:
Log in to Grafana
In your browser, log in to either :
- Self-hosted Grafana: at
http://localhost:3000/
. The default credentials areadmin
,admin
. - Grafana Cloud: use the URL and credentials you set when you created your account.
- Self-hosted Grafana: at
Add your Timescale Cloud service as a data source
In the Grafana dashboard, navigate to
Configuration
>Data sources
, then clickAdd data source
.In
Add data source
, selectPostgreSQL
.Configure the data source using the connection in
$TARGET
:Name
: the name to use for the datasetHost
: the host and port for your service, in this format:<HOST>:<PORT>
.For example:
example.tsdb.cloud.timescale.com:35177
.Database
:tsdb
User
:tsdbadmin
, or another privileged userPassword
: the password forUser
TLS/SSL Mode
: selectrequire
PostgreSQL details
: enableTimescaleDB
Leave the default setting for all other fields
Click
Save & test
.
Grafana checks that your details are set correctly.
Keywords
Found an issue on this page?Report an issue or Edit this page in GitHub.