Skip to content

AntiSol/sqlalchemy-timescaledb

 
 

Repository files navigation

SQLAlchemy TimescaleDB

This is the TimescaleDB dialect driver for SQLAlchemy. Drivers psycopg2 and asyncpg are supported.

Alembic is supported, it should correctly ignore objects (indexes, table partitions, etc) created by timescaledb.

Install

$ pip install git+https://github.com/AntiSol/sqlalchemy-timescaledb.git

Usage

Adding to table timescaledb_hypertable option allows you to configure the hypertable parameters:

import datetime
from sqlalchemy import create_engine, MetaData
from sqlalchemy import Table, Column, Integer, String, DateTime

engine = create_engine('timescaledb://user:password@host:port/database')
metadata = MetaData()
metadata.bind = engine

Metric = Table(
    'metric', metadata,
    Column('name', String),
    Column('value', Integer),
    Column('timestamp', DateTime(), default=datetime.datetime.now),
    timescaledb_hypertable={
        'time_column_name': 'timestamp'
    }
)

metadata.create_all(engine)

Or using declarative_base style:

import datetime

from sqlalchemy.orm import declarative_base
from sqlalchemy import Column, Float, String, DateTime

Base = declarative_base()

class Metric(Base):
    __table_args__ = ({
        'timescaledb_hypertable': {
            'time_column_name': 'timestamp'
        }
    })

    name = Column(String)
    value = Column(Float)
    timestamp = Column(
        DateTime(), default=datetime.datetime.now, primary_key=True
    )

Parameters

Functions

Timescaledb functions implemented:

func.first(Metric.value, Metric.timestamp)
func.last(Metric.value, Metric.timestamp)

About

TimescaleDB dialect driver for SQLAlchemy

Resources

License

Stars

Watchers

Forks

Languages

  • Python 97.9%
  • Mako 1.9%
  • Makefile 0.2%