diff --git a/compose.timescaledb.yml b/compose.timescaledb.yml index 4c6a871..c8afc11 100644 --- a/compose.timescaledb.yml +++ b/compose.timescaledb.yml @@ -28,6 +28,7 @@ services: MQTT_USERNAME: ${MQTT_USERNAME} MQTT_PASSWORD: ${MQTT_PASSWORD} PG_DB: ${PG_DB} + TIMESCALEDB_ENABLE: ${TIMESCALEDB_ENABLED} LOG_LEVEL: ${LOG_LEVEL} depends_on: timescaledb: diff --git a/compose.yml b/compose.yml index 9b9c857..c431ae1 100644 --- a/compose.yml +++ b/compose.yml @@ -8,6 +8,7 @@ services: MQTT_USERNAME: ${MQTT_USERNAME} MQTT_PASSWORD: ${MQTT_PASSWORD} PG_DB: ${PG_DB} + TIMESCALEDB_ENABLED: ${TIMESCALEDB_ENABLED} LOG_LEVEL: ${LOG_LEVEL} volumes: - /etc/timezone:/etc/timezone:ro diff --git a/example.env b/example.env index 6b6aad9..3d58bc4 100644 --- a/example.env +++ b/example.env @@ -4,5 +4,7 @@ MQTT_USERNAME=your_username MQTT_PASSWORD=your_password PG_DB='host=localhost port=5432 user=postgres password=secret-replace dbname=p1 sslmode=disable' +TIMESCALEDB_ENABLED=true + # LOG_LEVEL: DEBUG, INFO, WARN or ERROR. Standard: INFO LOG_LEVEL=INFO \ No newline at end of file diff --git a/main.go b/main.go index c1775f5..750c375 100644 --- a/main.go +++ b/main.go @@ -315,14 +315,6 @@ func connectToPostgreSQL(pgConnStr string) error { time.Sleep(5 * time.Second) // Retry after 5 seconds } - // Enable TimescaleDB - _, err = db.Exec(` - CREATE EXTENSION IF NOT EXISTS timescaledb; - `) - if err != nil { - log.Fatal("Error creating TimescaleDB extension:", err) - } - // Create table if not exists _, err = db.Exec(` CREATE TABLE IF NOT EXISTS p1 ( @@ -349,11 +341,24 @@ func connectToPostgreSQL(pgConnStr string) error { returning_l2 INT, returning_l3 INT ); - SELECT create_hypertable('p1', 'timestamp', if_not_exists => TRUE); `) if err != nil { log.Fatal("Error creating table:", err) } + timescaleDBEnabled := os.Getenv("TIMESCALEDB_ENABLED") + + if timescaleDBEnabled == "true" { + // Enable TimescaleDB + _, err = db.Exec(` + CREATE EXTENSION IF NOT EXISTS timescaledb; + SELECT create_hypertable('p1', 'timestamp', if_not_exists => TRUE); + `) + if err != nil { + log.Fatal("Error creating TimescaleDB extension:", err) + } + + } + return nil }