windows 安装、使用 timescaleDB

本文介绍了TimescaleDB如何利用超表技术高效存储和管理时间序列数据,包括创建超表、调整表结构、数据保留和压缩策略。重点讲解了如何安装、创建和维护tsdb实例,以及关键概念如chunk和retention policy的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

timescaleDB(下称tsdb)主要用来存储 time-series data,对于这个概念,tsdb官方给出的定义为:Time-series data is data that collectively represents how a system, process, or behavior changes over time.
像tsdb这样的时序型数据库利用了time-series data的以下特点:

  • 以时间为中心:总有一个timestamp字段;
  • 写多改少:几乎都是insert操作
  • 最近数据比较重要:我们很少更新或回填距现在时间间隔较长的缺失数据;

time-series data和一般的data不一样的关键点在于,数据库time-series data的操作或者改变基本都是inserts 而不是overwrite;

How TSDB manages time-series data

TimescaleDB uses hypertables to store time-series data. TimescaleDB automatically partitions data in hypertables into smaller child tables called chunks. The chunks represent data for a given time period, which makes it easier to query and manage over time. For example, if you wanted to query data from 10am to 11am, instead of scanning your entire database, TimescaleDB would scan the specific chunks that contain data for just that period. All the interaction with the database still occurs on the hypertable using SQL, but TimescaleDB partitions the data to make large queries more efficient.
Many features in TimescaleDB rely on chunks, including continuous aggregates, data retention, and native compression. Native compression is particularly helpful with large time-series datasets. Time-series data can be relentless in quantity and speed, and difficult to store and query without a purpose-built time-series database. You can use TimescaleDB compression to save as much as 97% of your disk space for the same amount of data, and usually increase the speed of your queries over time.

安装Postgresql

timescaleDB要求Postgresql数据库版本为12或13,windows版本下载链接;下载完installer运行无脑往下点就行。

然后把 PostgreSQL\13\bin 这个目录添加到环境变量,cmd窗口输入pg_config验证一下,安装tsdb它也会调用这个命令。在这里插入图片描述

安装tsdb

官网教程链接:安装前的准备:
在这里插入图片描述
大意是:

  • 安装VS2015或以后版本
  • postgresql 版本要12或13
  • 环境变量配置好
  • 电脑管理员身份执行安装

tsdb下载地址
下载完解压,以管理员身份运行setup.exe,一路yes就行。

弄好之后重启服务,在安装路径\PostgreSQL\13\data\postgresql.conf文件中可以发现
在这里插入图片描述
然后启动postgresql 自带的sql shell,或者cmd登录进入数据库输入:

 create extension timescaledb;

在这里插入图片描述
然后就可以创建表,转为超表了。

基操

1.创建超表
CREATE TABLE conditions (
    time        TIMESTAMPTZ       NOT NULL,
    location    TEXT              NOT NULL,
    temperature DOUBLE PRECISION  NULL
);
SELECT create_hypertable('conditions', 'time');

create_hypertable函数两个必填:第一个是表名,第二个是时间字段。

#如果之前的普通表中有数据,在调用函数转超表时需要设置`migrate_data`参数为`true`
SELECT create_hypertable('conditions','time', migrate_data => true)

#设置一个chunk时间范围为1天
SELECT * FROM create_hypertable('conditions', 'time', chunk_time_interval => INTERVAL '1 day');

#手动删除:超过24小时的chunk删除
SELECT drop_chunks('conditions', INTERVAL '24 hours');
#自动删除:
SELECT add_retention_policy('conditions', INTERVAL '6 months');

关于create_hypertable里面的可选参数设定可以参考链接

2.修改、删除超表
#修改
ALTER TABLE conditions
  ADD COLUMN humidity DOUBLE PRECISION NULL;

#删除
DROP TABLE conditions;

To be continued…

### 如何在 Windows安装 TimescaleDB 由于官方支持主要集中在 Linux 和 macOS 平台上,对于 Windows 用户来说,在本地环境中运行 TimescaleDB 的最佳方式是通过 Docker 容器化部署。 #### 使用 Docker 部署 TimescaleDB 1. **安装 Docker** 确保已经安装了最新版本的 Docker Desktop。可以从[Docker官网](https://www.docker.com/products/docker-desktop)下载并按照说明完成安装过程[^1]。 2. **拉取 TimescaleDB 镜像** 打开命令提示符或 PowerShell 终端窗口,执行如下命令来获取最新的 TimescaleDB Docker 映像: ```bash docker pull timescale/timescaledb:latest-pg14 ``` 此操作会从 Docker Hub 下载适用于 PostgreSQL 14 版本的 TimescaleDB 扩展镜像文件。 3. **启动容器实例** 创建一个新的容器实例并将它连接到主机网络接口上。可以指定自定义参数如内存限制、CPU 资源分配以及持久化存储路径等选项。下面是一个简单的例子用于快速测试环境搭建: ```bash docker run -d \ --name=timescaledb \ -e POSTGRES_PASSWORD=mysecretpassword \ -p 5432:5432 \ timescale/timescaledb:latest-pg14 ``` 上述脚本中的 `-d` 参数表示以后台模式启动;`POSTGRES_PASSWORD` 设置数据库超级用户的密码;最后部分指定了映射外部访问端口为 `5432`【默认PostgreSQL服务监听端口号】。 4. **验证安装成功与否** 可以通过 pgAdmin 或者其他任何兼容 PostgresSQL 协议的应用程序工具连接至新建立的服务节点来进行功能检测与数据管理活动。也可以直接利用 psql 命令行客户端进入交互界面查看帮助文档或者尝试一些基本查询语句以确认一切正常工作。 ```bash psql -h localhost -U postgres ``` 输入之前设置好的 root 密码之后就可以开始探索这个时间序列优化过的 SQL 数据库特性啦!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

T.Y.Bao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值