Timescale DB 未创建超表

Tea*_*orn 2 go hypertable timescaledb

我正在关注 timescaleDB 的教程.. https://docs.timescale.com/timescaledb/latest/how-to-guides/hypertables/create/#create-a-hypertable

但我无法创建超表..我已经成功安装了 postgres 和 timescale。为什么我不能制作超表?

package main

import (
    "context"
    "encoding/json"
    "fmt"
    "log"
    "os"
    "runtime"
    "strconv"
    "sync"

    "github.com/jackc/pgx/v4/pgxpool"
)




    func main(){
    timeScaleDB()
    }
      

      
    func timeScaleDB(){
    
        psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
        "password=%s dbname=%s sslmode=disable",
        host, port, user, password, dbname)
    
        ctx := context.Background()
        dbpool, err := pgxpool.Connect(ctx, psqlInfo)
        if err != nil {
            fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
            os.Exit(1)
        }
        defer dbpool.Close()

       
       queryCreatetable := `CREATE TABLE sample_table (
        time TIMESTAMPTZ NOT NULL,
        id INTEGER PRIMARY KEY,
        name VARCHAR NOT NULL
        );
        SELECT create_hypertable('sample_table', 'time');`
        
    //execute statement
    _, err = dbpool.Exec(ctx, queryCreatetable)
    if err != nil {
    fmt.Fprintf(os.Stderr, "Unable to create sample_table hypertable: %v\n", err)
    os.Exit(1)
    }
    
    fmt.Println("Successfully created hypertable sample_table")
    }
Run Code Online (Sandbox Code Playgroud)

错误:无法创建sample_table超表:错误:函数create_hypertable(未知,未知)不存在(SQLSTATE 42883)

jon*_*sdp 6

您能否确认您是否启用了该扩展程序?只需将服务器加入 psql 会话并使用命令列出扩展即可\dx

tsdb=> \dx
                                          List of installed extensions
        Name         | Version |   Schema   |                            Description
---------------------+---------+------------+-------------------------------------------------------------------
 plpgsql             | 1.0     | pg_catalog | PL/pgSQL procedural language
 timescale_analytics | 0.2     | public     | timescale_analytics
 timescaledb         | 2.2.0   | public     | Enables scalable inserts and complex queries for time-series data
...

Run Code Online (Sandbox Code Playgroud)

或者干脆尝试提前启用扩展:

CREATE EXTENSION IF NOT EXISTS timescaledb;
Run Code Online (Sandbox Code Playgroud)

  • 所以表上没有安装扩展,但数据库安装了扩展,这就是我出错的地方。 (3认同)
  • 你检查过 pgx 先决条件了吗?看起来它正在删除,因为您没有使用它。无论如何,请逐步尝试这个小步骤:​​https://github.com/jackc/pgx/wiki/Getting-started-with-pgx 我在这里尝试了你的示例,并得到了几个不同的错误,但不是你所遇到的错误:``` go run sample.go 09:30:37 # 命令行参数 ./sample.go:5:3: 已导入且未使用: "encoding/json" ./sample.go:7:3: 已导入且未使用:“log”./sample.go:11:3:已导入但未使用:“sync”``` (2认同)
  • 我能够很好地连接到数据库,并很好地创建表。我只是无法创建超级表。今晚睡前将致力于创建一个超级表。感谢您的指点! (2认同)