https://github.com/dorosch/sqlalchemy-timescaledb
TimescaleDB dialect driver for SQLAlchemy (psycopg2 and asyncpg supported)
https://github.com/dorosch/sqlalchemy-timescaledb
python sqlalchemy timescaledb
Last synced: about 2 months ago
JSON representation
TimescaleDB dialect driver for SQLAlchemy (psycopg2 and asyncpg supported)
- Host: GitHub
- URL: https://github.com/dorosch/sqlalchemy-timescaledb
- Owner: dorosch
- License: mit
- Created: 2023-02-07T08:47:00.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-07-23T11:00:42.000Z (10 months ago)
- Last Synced: 2025-04-10T16:35:26.643Z (about 2 months ago)
- Topics: python, sqlalchemy, timescaledb
- Language: Python
- Homepage: https://pypi.org/project/sqlalchemy-timescaledb/
- Size: 39.1 KB
- Stars: 42
- Watchers: 4
- Forks: 8
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SQLAlchemy TimescaleDB
[][1]
[][2]
[][3]
[][4]This is the TimescaleDB dialect driver for SQLAlchemy. Drivers `psycopg2` and `asyncpg` are supported.
## Install
```bash
$ pip install sqlalchemy-timescaledb
```## Usage
Adding to table `timescaledb_hypertable` option allows you to configure the [hypertable parameters][5]:
```Python
import datetime
from sqlalchemy import create_engine, MetaData
from sqlalchemy import Table, Column, Integer, String, DateTimeengine = create_engine('timescaledb://user:password@host:port/database')
metadata = MetaData()
metadata.bind = engineMetric = 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:
```Python
import datetimefrom sqlalchemy.orm import declarative_base
from sqlalchemy import Column, Float, String, DateTimeBase = 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
* [chunk_time_interval][6]
## Functions
Timescaledb functions implemented:
### [first(value, time)][7]
```Python
func.first(Metric.value, Metric.timestamp)
```### [last(value, time)][8]
```Python
func.last(Metric.value, Metric.timestamp)
```[1]: https://badge.fury.io/py/sqlalchemy-timescaledb
[2]: https://github.com/dorosch/sqlalchemy-timescaledb/actions/workflows/tests.yml
[3]: https://codecov.io/gh/dorosch/sqlalchemy-timescaledb
[4]: https://pepy.tech/project/sqlalchemy-timescaledb
[5]: https://docs.timescale.com/api/latest/hypertable/create_hypertable/#optional-arguments
[6]: https://docs.timescale.com/api/latest/hypertable/set_chunk_time_interval/
[7]: https://docs.timescale.com/api/latest/hyperfunctions/first/
[8]: https://docs.timescale.com/api/latest/hyperfunctions/last/