Insert Temperature and Humidity Data into PostgreSQL Using Node-RED

In this post, I’ll walk you through how i used Node-RED together with the node-red-contrib-postgresql plugin to insert temperature and humidity data into a PostgreSQL database. This is a simple but useful workflow that could be helpful for anyone working with sensors or data logging.

Tech Stack

  • Node-RED
  • PostgreSQL
  • Plugin: node-red-contrib-postgresql
  • Docker(used for running PostgreSQL)

PostgreSQL Table Setup

CREATE TABLE IF NOT EXISTS public.sensor_data (
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
temperature BIGINT,
humidity BIGINT
);

Node-RED Flow Design

The Node-RED flow consists of four key nodes:

Inject -> Function -> Postgresql -> Debug

1. Inject Node: Sample Data

{
   "temperature": 45.6,
   "humidity": 85.2
}

This is injects sample temperature and humidity values in JSON format.

2. Function Node: SQL and Parameter Formatting

msg.params = [
    msg.payload.temperature,
    msg.payload.humidity
];
return msg;

This node builds the paramaters and formats the payload as an array of values.

3. Postgresql Node Configuration

  • Plugin: node-red-contrib-postgresql
  • Query: configure insert query
  • Parameters are passed via msg.params
INSERT INTO sensor_data (temperature, humidity) VALUES ($1, $2)

Postgresql Config node

4. Debug Node

This node helps verify whether the insertion was successful. You can also use a catch node to handle any errors that might occur during the database operation.

Flow Screenshot

Conclusion

This project started with a simple idea but led to some helpful learning moments — especially around formatting and debugging SQL operation in Node-RED. I hope this help anyone else looking to integrate Node-RED with PostgreSQL!

Feel free to leave a comment if you have questions or want to share your own setup.

Leave a Comment

Your email address will not be published. Required fields are marked *