Skip to content

davydog187/timescale

Repository files navigation

Timescale

Build Status Hex pm Hexdocs.pm

Extends the Ecto DSL for easily working with TimescaleDB. Already using Ecto and Postgres? Great, you're all set to start working with time-series data.

Features

Adding the TimescaleDB extension

  1. Make sure your database has Timescale correctly installed
  2. Create a new Ecto migration
  3. Call the create_timescaledb_extension/0 and drop_timescaledb_extension/0 in your migration

E.g.

defmodule MyApp.Repo.Migrations.SetupTimescale do
  use Ecto.Migration

  import Timescale.Migration

  def up do
    create_timescaledb_extension()
  end

  def down do
    drop_timescaledb_extension()
  end
end

Using the Library

Here is an intermediate example querying a timescale reading using Timescale hyperfunctions with timescale's Ecto extensions.

For a more comprehensive example you can check out our guide in the docs here.

import Timescale.Hyperfunctions

Repo.all(
  from(h in "heartbeats",
    where: h.user_id == ^alex_id,
    group_by: selected_as(:minute),
    select: %{
      minute: selected_as(time_bucket(h.timestamp, "1 minute"), :minute),
      bpm: count(h)
    },
    limit: 5
  )
)

Installation

If available in Hex, the package can be installed by adding timescale to your list of dependencies in mix.exs:

def deps do
  [
    {:timescale, "~> 0.1.0"}
  ]
end

Installing Postgres / TimescaleDB on MacOS

There are many ways to install PostgreSQL locally, including Postgres.app, Docker, and building locally. Below is how to install through Homebrew

First, install Postgres

$ brew install postgresql
$ sudo chown $(whoami) /usr/local/var/postgres
$ initdb /usrl/local/var/postgres
$ createuser -s postgres
$ createdb

Make Postgres a service that is started automatically

$ brew services start postgresql

Then install TimescaleDB. For more information about installing TimescaleDB on MacOS, see the official documentation.

$ brew tap timescale/tap
$ brew install timescaledb

# Add the following to `/opt/homebrew/var/postgres/postgresql.conf`
shared_preload_libraries = 'timescaledb'