- Published on
Setting Up TimescaleDB with TypeORM and DockerCompose
- Authors
- Name
- Yair Mark
- @yairmark
TimescaleDB is an excellent tool if you need to work with time series data for example for trading data or IOT data. It sits on top of Postgres as an extension to Postgres. Setting it up with docker-compose is fairly easy. The steps are:
- Create a docker-compose file with the timescaleDB container and configuration
- Create an initial migration script for TypeORM to setup TimescaleDB
- Run the migration script
Docker-compose file
The compose file is fairly straightforward. You need to pass in environment variables to configure the Postgres DB. If you leave out these variables the default values will be used.
You also need to configure the volume for the DB to use.
The other possible environment variables you can use for Postgres based containers can be found here.
version: "3"
services:
timescaledb:
image: timescale/timescaledb:latest-pg11
environment:
POSTGRES_PASSWORD: "your super secure password"
volumes:
- db-data:/var/lib/postgresql/data
ports:
- "5432:5432"
volumes:
db-data:
driver: local
Running this is as easy as cd
ing into the folder from the command line and running docker-compose up -d
If you want to shutdown and start over:
docker-compose down
docker volume rm nameOfYourDBVolume
Initial TypeORM Migration Script
Install the TypeORM
cli by running npm install -g typeorm
. Then from the root of your project (where you already have typeorm setup) run: typeorm migration:create -n SetupTimescaleDB
.
This will create a file with a timestamp prepended to it and the name you specified. You then need to modify the script to look as below:
import { MigrationInterface, QueryRunner } from 'typeorm'
export class SetupTimescaleDB1562056165793 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<any> {
await queryRunner.query(`CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE`)
}
// @ts-ignore
public async down(queryRunner: QueryRunner): Promise<any> {}
}
Run the migration Script
For this you need to make sure that TimescaleDB is running (you have to have run docker-compose). Then from the root for your project simply run:
typeorm migration:run