TimescaleDb入门教程

介绍

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
);

转换成超表

-- This creates a hypertable that is partitioned by time
--   using the values in the `time` column.

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;
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值