TimescaleDB是基于PostgreSQL数据库开发的一款时序数据库,以插件化的形式打包提供,随着PostgreSQL的版本升级而升级,不会因为另立分支带来麻烦。
TimescaleDB在许多方面的行为都类似于标准PostgreSQL数据库。如下:
(1)与PostgreSQL服务器上的其他TimescaleDB和PostgreSQL数据库共存。
(2)使用SQL作为其接口语言。包含标准数据库对象,例如表,索引和触发器。
(3)使用通用的PostgreSQL连接器连接第三方工具。
数据库实现这种同步的方式是通过将其打包为PostgreSQL扩展,从而将标准PostgreSQL数据库转换为TimescaleDB。
TimescaleDB提供的超越PostgreSQL的优势主要与处理时序数据有关。与超级表进行交互时,这些优势最为明显,超级表的行为类似于普通表,但即使将存储扩展到通常禁止的数据量,也能保持高性能。超表可以参与正常的表操作,包括与标准表的JOIN。
(1)docker安装timescaleDB
#docker search timescale
#docker pull timescale/timescaledb
#docker run -d --name timescaledb -p 5432:5432 -e POSTGRES_PASSWORD=“12345” timescale/timescaledb
(2)进入容器
#docker exec -it d6 /bin/bash进入容器
(2-1)链接数据库
bash-5.0# su - postgres切换用户
d65a580c9a80:~$ psql
或者
bash-5.0# psql -U postgres -h localhost
(2-2)创建数据库
postgres=# create database tutorial;
(2-3)在数据库中添加timescaleDB扩展
postgres=# \c tutorial先连接到数据库
You are now connected to database “tutorial” as user “postgres”.
tutorial=# create extension if not exists timescaledb cascade;用timescaledb扩展数据库
NOTICE: extension “timescaledb” already exists, skipping
CREATE EXTENSION
(2-4)创建超表
要创建一个超表,从一个常规SQL表开始,然后通过create_hypertable函数将其转换成一个超表。以下示例创建了一个超级表,用于跟踪一段时间内整个设备集合中的温度和湿度。
(2-4-1)创建常规表
CREATE TABLE conditions (
time TIMESTAMPTZ NOT NULL,
location TEXT NOT NULL,
temperature DOUBLE PRECISION NULL,
humidity DOUBLE PRECISION NULL
);
(2-4-2)转换成超表
#select create_hypertable(‘conditions’,‘time’);
#insert into conditions(time,location,temperature,humidity) values(NOW(),‘office’,70.0,50.0);插入数据
#select * from conditions order by time desc limit 100;查询数据