TimescaleDb入门教程

2022-03-25 00:00:00 创建 数据库 专区 订阅 将其

介绍

TimescaleDB在许多方面的行为都类似于标准PostgreSQL数据库。如下:

  1. 与PostgreSQL服务器上的其他TimescaleDB和PostgreSQL数据库共存。
  2. 使用SQL作为其接口语言。包含标准数据库对象,例如表,索引和触发器。
  3. 使用通用的PostgreSQL连接器连接第三方工具。

数据库实现这种同步的方式是通过将其打包为PostgreSQL扩展,从而将标准PostgreSQL数据库转换为TimescaleDB。

TimescaleDB提供的超越PostgreSQL的优势主要与处理时序数据有关。 与超级表进行交互时,这些优势为明显,超级表的行为类似于普通表,但即使将存储扩展到通常禁止的数据量,也能保持高性能。 超表可以参与正常的表操作,包括与标准表的JOIN。

安装

mac

  1. 编译&安装
    # Add our tap brew tap timescale/tap # To install brew install timescaledb # Post-install to move files to appropriate place /usr/local/bin/timescaledb_move.sh
    安装完会提示如下
    RECOMMENDED: Run 'timescaledb-tune' to update your config settings for TimescaleDB. timescaledb-tune --quiet --yes IF NOT, you'll need to make sure to update /usr/local/var/postgres/postgresql.conf to include the extension: shared_preload_libraries = 'timescaledb' To finish the installation, you will need to run: timescaledb_move.sh If PostgreSQL is installed via Homebrew, restart it: brew services restart postgresql
  2. 配置数据库
    修改postgresql.conf文件
    在shared_preload_libraries参数中添加timescaledb
    然后重启数据库
    # Restart PostgreSQL instance brew services restart postgresql # Add a superuser postgres: createuser postgres -s

配置

要做的件事是创建一个新的空数据库或将现有的PostgreSQL数据库转换为使用TimescaleDB。

  1. 链接数据库
    # Connect to PostgreSQL, using a superuser named 'postgres' psql -U postgres -h localhost
  2. 创建数据库,如果已经有的话就不必创建
    CREATE database tutorial;
  3. 再上一步的数据库中添加timesaleDB扩展
    -- Connect to the database \c tutorial -- Extend the database with TimescaleDB CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;

创建超表

要创建一个超表,从一个常规SQL表开始,然后通过create_hypertable函数将其转换成一个超表。

以下示例创建了一个超级表,用于跟踪一段时间内整个设备集合中的温度和湿度。

创建常规表

-- We start by creating a regular SQL table

CREATE TABLE conditions (
  time        TIMESTAMPTZ       NOT NULL,
  location    TEXT              NOT NULL,
  temperature DOUBLE PRECISION  NULL,
  humidity    DOUBLE PRECISION  NULL
);

相关文章