TimescaleDB Integration
Deploy a TimescaleDB ingestion container on ALPON X5 AI or ALPON X4 through ALPON Cloud: write a Python script that inserts sensor readings, build the arm64 image, push it to the Sixfab Container Registry, and deploy it with the database connection settings as environment variables.
Deploy TimescaleDB ingestion on ALPON
Connect your ALPON X5 AI or ALPON X4 to TimescaleDB and store real-time sensor data in a time-series database. This guide builds a small Python ingestion container, deploys it through ALPON Cloud with the database connection as environment variables, and verifies the readings with a SQL query.
Write a Python script that connects to your TimescaleDB instance with psycopg2 and inserts
readings, package it in a python:3.11-slim image built for linux/arm64, and push
it to your Sixfab Container Registry. Then use the Applications → Deploy
panel on ALPON Cloud to launch the container on your ALPON X5 AI or
ALPON X4 with DB_USER, DB_PASSWORD, DB_HOST,
DB_PORT and DB_NAME as environment variables, and query the
sensor_data table to confirm the rows arrive.
Overview
TimescaleDB is a time-series database built on PostgreSQL, designed for efficiently storing and querying sensor and event data. With an ALPON X5 AI or ALPON X4 it can be used from a fully containerized environment managed through ALPON Cloud, letting you continuously ingest and manage time-series data from your device fleet.
This guide walks through creating a simple TimescaleDB data ingestion container and deploying it on the ALPON using ALPON Cloud. The steps are identical on ALPON X4 and ALPON X5 AI. For more on TimescaleDB itself, see the Timescale documentation.
- ALPON device: an ALPON X5 AI or ALPON X4, powered on, connected to ALPON Cloud and operational.
- ALPON Cloud account: access to ALPON Cloud for container management.
- Docker installed: on your development machine, for building the container image.
- TimescaleDB service: a running TimescaleDB instance, hosted on Timescale or another PostgreSQL-compatible service.
New to container deployment? Start with Containerize Apps for ALPON.
-
1
Prepare the Dockerfile
On your development machine, create a file named
Dockerfilethat defines a Python-based ingestion environment:Dockerfile · TimescaleDB ingestorFROM python:3.11-slim RUN apt-get update && apt-get install -y \\ gcc \\ libpq-dev \\ && rm -rf /var/lib/apt/lists/* WORKDIR /app RUN pip install --no-cache-dir psycopg2-binary==2.9.9 COPY main.py . CMD ["python", "main.py"]This container runs a Python script that sends data into your TimescaleDB instance.
-
2
Write the data ingestion script
Create a file named
main.pyin the same directory as your Dockerfile. The script:- connects to your TimescaleDB instance,
- creates a
sensor_datatable if it doesn’t exist, and - inserts simulated temperature and humidity readings every 10 seconds.
main.py · data ingestion scriptimport psycopg2 import os import random import time from datetime import datetime class PostgresClient: def __init__(self): self.conn = psycopg2.connect( f"postgres://{os.getenv('DB_USER')}:{os.getenv('DB_PASSWORD')}@" f"{os.getenv('DB_HOST')}:{os.getenv('DB_PORT')}/{os.getenv('DB_NAME')}?sslmode=require" ) print("Database connected!") def setup_table(self): with self.conn.cursor() as cur: cur.execute(""" CREATE TABLE IF NOT EXISTS sensor_data ( id SERIAL PRIMARY KEY, sensor_name VARCHAR(50), temperature DECIMAL(5,2), humidity DECIMAL(5,2), timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) """) self.conn.commit() print("Table created!") def insert_data(self): sensors = ['TEMP_001', 'TEMP_002', 'HUMID_001'] with self.conn.cursor() as cur: cur.execute(""" INSERT INTO sensor_data (sensor_name, temperature, humidity) VALUES (%s, %s, %s) """, ( random.choice(sensors), round(random.uniform(15.0, 35.0), 2), round(random.uniform(30.0, 80.0), 2) )) self.conn.commit() print(f"{datetime.now().strftime('%H:%M:%S')} - Data inserted") def show_data(self): with self.conn.cursor() as cur: cur.execute("SELECT * FROM sensor_data ORDER BY timestamp DESC LIMIT 5") for row in cur.fetchall(): print(f"ID: {row[0]}, Sensor: {row[1]}, Temp: {row[2]}°C, Humidity: {row[3]}%") def run_continuous(self): print("Starting continuous data generation... (Ctrl+C to stop)") try: while True: self.insert_data() time.sleep(10) except KeyboardInterrupt: print("\ Stopped by user") finally: self.conn.close() def main(): db = PostgresClient() db.setup_table() db.show_data() db.run_continuous() if __name__ == "__main__": main() -
3
Build and push the image
From the directory containing your
Dockerfileandmain.py, build the image for the ALPON’sarm64architecture:bash · build the imagedocker buildx build --platform linux/arm64 --load -t timescale-ingestor .
Then log in to Sixfab Registry, click + Add Container, and follow the prompts to push the
timescale-ingestorimage.Pushing images to the Sixfab Container RegistryFor the full walkthrough of tagging and pushing an image, see Update Containers from the Sixfab Container Registry.
-
4
Deploy the container on ALPON
Open your device in ALPON Cloud, go to the Applications section, and click + Deploy. In the Deploy Container window, use these settings:
Container NametimescaleImageThetimescale-ingestorimage and tag you pushed to the Sixfab Container Registry.EnvironmentClick + Add More in the Environment section and add the five variables in the table below.Key Value DB_USERyour_timescale_usernameDB_PASSWORDyour_timescale_passwordDB_HOSTyour_timescale_hostDB_PORT5432DB_NAMEyour_database_nameClick + Deploy to launch the ingestion container on the device.
-
5
Verify the data in TimescaleDB
Once the container is running, data is continuously sent to your TimescaleDB instance. To check, run this query against the database:
sql · latest readingsSELECT * FROM sensor_data ORDER BY timestamp DESC LIMIT 5;
You should see the latest simulated readings from your ALPON. If the table is empty, confirm the container is running in the Applications section and that the connection variables in step 4 point at a reachable database.
- The
timescalecontainer shows as running in the Applications section. - The
sensor_datatable exists in your TimescaleDB instance. - New rows appear every 10 seconds when you re-run the query above.
With TimescaleDB integrated, your ALPON can store and query large volumes of time-series data for
analytics, monitoring, and visualization. Replace the simulated readings in main.py with your
real sensor inputs.
Production image policy: Replace floating
:latestreferences with a reviewed immutable tag or digest, then record the selected version for rollback.
Updated 15 days ago
