badesee-preprocessor/schema/create-schema.sql

49 lines
1.4 KiB
SQL

create database monitoring;
create extension timescaledb;
CREATE TABLE device_t (
id SERIAL NOT NULL PRIMARY KEY,
device_id VARCHAR(32) NOT NULL UNIQUE,
label VARCHAR(16) NOT NULL
);
CREATE TABLE sensor_t (
id SERIAL NOT NULL PRIMARY KEY,
address NUMERIC NOT NULL UNIQUE,
label VARCHAR(5) NOT NULL,
index SMALLINT NOT NULL,
device INTEGER NOT NULL REFERENCES device_t(id),
unique (label, device),
unique (index, device)
);
CREATE TABLE measurement_t (
time TIMESTAMP WITHOUT TIME ZONE NOT NULL,
device_name VARCHAR(16) NOT NULL,
sensor_name VARCHAR(5) NOT NULL,
temperature DOUBLE PRECISION
);
SELECT create_hypertable('measurement_t', 'time');
CREATE TABLE voltage_t (
time TIMESTAMP WITHOUT TIME ZONE NOT NULL,
device_name VARCHAR(16) NOT NULL,
voltage INTEGER
);
SELECT create_hypertable('voltage_t', 'time');
insert into device_t (device_id, label) values('eui-43fa12f400006c88', 'badesee');
insert into sensor_t (address, label, index, device)
values (13258914387362694952, '0,5m ', 0, 1),
(12970366982842161448, '2,0m ', 1, 1),
(10664523975231507496, '3,0m ', 2, 1),
(15276209993662477608, '4,0m ', 3, 1);
create user preprocessor password 'geheim';
grant select on device_t, sensor_t to preprocessor;
grant insert on measurement_t to preprocessor;
grant insert on voltage_t to preprocessor;