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.

ALPON X5 AI ALPON X4 TimescaleDB Time-series data
ALPON · Tutorial · Containers · Databases
How do I send data from ALPON to TimescaleDB?

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.

Before you start
  • 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. 1

    Prepare the Dockerfile

    On your development machine, create a file named Dockerfile that defines a Python-based ingestion environment:

    Dockerfile · TimescaleDB ingestor
    FROM 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. 2

    Write the data ingestion script

    Create a file named main.py in the same directory as your Dockerfile. The script:

    • connects to your TimescaleDB instance,
    • creates a sensor_data table if it doesn’t exist, and
    • inserts simulated temperature and humidity readings every 10 seconds.
    main.py · data ingestion script
    import 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. 3

    Build and push the image

    From the directory containing your Dockerfile and main.py, build the image for the ALPON’s arm64 architecture:

    bash · build the image
    docker 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-ingestor image.

    Pushing images to the Sixfab Container Registry

    For the full walkthrough of tagging and pushing an image, see Update Containers from the Sixfab Container Registry.

  4. 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 Name timescale
    Image The timescale-ingestor image and tag you pushed to the Sixfab Container Registry.
    Environment Click + Add More in the Environment section and add the five variables in the table below.
    KeyValue
    DB_USERyour_timescale_username
    DB_PASSWORDyour_timescale_password
    DB_HOSTyour_timescale_host
    DB_PORT5432
    DB_NAMEyour_database_name

    Click + Deploy to launch the ingestion container on the device.

  5. 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 readings
    SELECT * 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.

Ready when…
  • The timescale container shows as running in the Applications section.
  • The sensor_data table 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 :latest references with a reviewed immutable tag or digest, then record the selected version for rollback.


Did this page help you?