postGreSQL 插件 timescaleDB 安装使用
基本环境
操作系统: centOS 7
postGreSQL : 10
timescaleDB : 1.0 +
postGreSQL安装
Centos7 安装Postgresql10.5和PostGIS
timescaleDB 安装
PostgreSQL 时序数据库插件 timescaleDB 部署实践 - PostGIS + timescaleDB => PG时空数据库 -- 德哥
官网安装文档
TimescaleDB Docs - Installing
timescaleDB 设置
TimescaleDB Docs - Setting up TimescaleDB
实验
1 -- 下载测试数据
2 wget https://timescaledata.blob.core.windows.net/datasets/weather_small.tar.gz
3
4 wget https://timescaledata.blob.core.windows.net/datasets/weather_big.tar.gz
5
6 -- 创建数据库
7 create database weather;
8
9 -- 解压下载文件
10 tar -zxvf weather_small.tar.gz
11
12 -- 创建表结构
13 psql -U postgres -d weather < weather.sql
14
15 -- 导入数据
16 psql -U postgres -d weather -c "\COPY conditions FROM weather_small_conditions.csv CSV"
17 psql -U postgres -d weather -c "\COPY locations FROM weather_small_locations.csv CSV"
18
19
20 -- 查询测试
21
22 SELECT * FROM conditions c ORDER BY time DESC LIMIT 10;
23
24
25 SELECT time, c.device_id, location,
26 trunc(temperature, 2) temperature, trunc(humidity, 2) humidity
27 FROM conditions c
28 INNER JOIN locations l ON c.device_id = l.device_id
29 WHERE l.environment = 'outside'
30 ORDER BY time DESC LIMIT 10;
31
32
33 SELECT date_trunc('hour', time) "hour",
34 trunc(avg(temperature), 2) avg_temp,
35 trunc(min(temperature), 2) min_temp,
36 trunc(max(temperature), 2) max_temp
37 FROM conditions c
38 WHERE c.device_id IN (
39 SELECT device_id FROM locations
40 WHERE location LIKE 'field-%'
41 ) GROUP BY "hour" ORDER BY "hour" ASC LIMIT 24;
注释
1-- 数据库启动
2systemctl start postgresql-10
3
4-- 连接数据库
5psql
6
7-- 创建数据库
8CREATE database weather_big;
9
10-- 创建时序关系
11CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;
12
13-- 导入数据
14psql -U postgres -d weather_big < weather.sql
15psql -U postgres -d weather_big -c "\COPY conditions FROM weather_big_conditions.csv CSV"
16psql -U postgres -d weather_big -c "\COPY locations FROM weather_big_locations.csv CSV"
来源 https://www.modb.pro/db/242504
相关文章