Deploying TimescaleDB
Introduction
TimescaleDB extends PostgreSQL with time-series superpowers and a familiar SQL interface. This guide shows you how to package TimescaleDB with a Dockerfile, configure secure credentials, attach persistent storage, and deploy it to Klutch.sh for low-latency access from your applications.
Prerequisites
- GitHub repository containing your Dockerfile and optional init scripts.
- Klutch.sh account with a project ready in the dashboard at klutch.sh/app.
- TCP access expectations for database traffic.
Project structure
.├── Dockerfile└── init └── 001_bootstrap.sqlSample Dockerfile
# Use the official TimescaleDB image built on PostgreSQL 15FROM timescale/timescaledb:latest-pg15
# Optional: place your bootstrap SQL in /docker-entrypoint-initdb.d/COPY init/*.sql /docker-entrypoint-initdb.d/
# Rely on Klutch.sh environment variables for secretsENV POSTGRES_DB=app_db \ POSTGRES_USER=app_user
# Do not set POSTGRES_PASSWORD here—configure it as a secret in the dashboardEXPOSE 5432Recommended environment variables
POSTGRES_PASSWORD(required) – strong password for the primary role.POSTGRES_USER(optional) – defaults topostgresif not set.POSTGRES_DB(optional) – defaults to the user name.TIMESCALEDB_TELEMETRY(optional) – set tooffif you want to disable telemetry.
Persistence
TimescaleDB needs durable storage. In Klutch.sh, add a persistent volume with:
- Mount path:
/var/lib/postgresql/data - Size: choose based on dataset growth and retention.
Networking
- Protocol: TCP
- Internal container port:
5432(TimescaleDB default) - Clients connect to your app’s TCP endpoint (e.g.,
example-app.klutch.sh:8000) while Klutch.sh routes traffic to port5432inside the container.
Optional bootstrap SQL
init/001_bootstrap.sql
CREATE TABLE IF NOT EXISTS metrics ( ts timestamptz NOT NULL, value double precision NOT NULL);
SELECT create_hypertable('metrics', 'ts', if_not_exists => TRUE);Health check (recommended)
Add a simple readiness command in your deployment checks:
pg_isready -U "$POSTGRES_USER" -d "$POSTGRES_DB" -h 127.0.0.1 -p 5432Deployment on Klutch.sh
- Push your Dockerfile and any init scripts to a GitHub repository.
- In klutch.sh/app, create a new app and select GitHub as the source.
- Klutch.sh automatically detects the Dockerfile in the repository root.
- Set traffic type to TCP and specify internal port
5432. - Add environment variables (e.g.,
POSTGRES_USER,POSTGRES_DB,POSTGRES_PASSWORD) in the dashboard. Mark sensitive values as secrets. - Attach a persistent volume at
/var/lib/postgresql/datawith an appropriate size. - Deploy. Once live, connect from your app using the TCP endpoint (e.g.,
example-app.klutch.sh:8000) and the credentials you configured.
Verification
-
From a client or jump box, run:
Terminal window PGPASSWORD="$POSTGRES_PASSWORD" psql "host=example-app.klutch.sh port=8000 dbname=$POSTGRES_DB user=$POSTGRES_USER sslmode=require" -c "SELECT NOW();" -
Confirm hypertables exist:
Terminal window psql "...connection string..." -c "\dt"
Next steps
- Tune TimescaleDB config (e.g., chunk interval, retention policies) via SQL once deployed.
- Enable regular backups from the mounted data path.
- Add application-side connection pooling if your workload is bursty.