# How to send MQTT node data (integers & string) to a table in postgres database

**URL:** <https://discourse.nodered.org/t/how-to-send-mqtt-node-data-integers-string-to-a-table-in-postgres-database/77570>\
**Category:** General\
**Tags:** database, function-node, mqtt\
**Created:** [14 April 2023 16:10 UTC](https://discourse.nodered.org/t/how-to-send-mqtt-node-data-integers-string-to-a-table-in-postgres-database/77570 "2023-04-14T16:10:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![daaven](https://avatars.discourse-cdn.com/v4/letter/d/7ab992/32.png) [@daaven](https://discourse.nodered.org/u/daaven)\
**Post date:** [14 April 2023 16:10 UTC](https://discourse.nodered.org/t/how-to-send-mqtt-node-data-integers-string-to-a-table-in-postgres-database/77570/1 "2023-04-14T16:10:55Z")

</div>

![Screenshot (22)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/1/41ca533aa309a1ade3b7492c4233655dab2c8e14.jpeg)  
 ![Screenshot (23)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/2/d2d2667bbb041409070da4df7b0462dc50d3f26c.png)  
Node-RED version: v3.0.2  
Node.js version: v18.12.1  
I uploaded my flow chart, on the far left shows MQTT nodes. Customer Name and Description are both strings. Analog, Test Time, Test Pressure, Job Number, and Serial Number are all integer values. I can't seem to figure out what to insert in the function node to send to the database to place all the values in the same row when it uploads. I also uploaded a picture of the table I have in postgres. My current function node only uploads one integer value at a time and leaves of the rest of the columns blank.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [14 April 2023 16:19 UTC](https://discourse.nodered.org/t/how-to-send-mqtt-node-data-integers-string-to-a-table-in-postgres-database/77570/2 "2023-04-14T16:19:13Z")

</div>

Hi Daniel, welcome to the forum.

Can you show us your function node code please?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [14 April 2023 16:50 UTC](https://discourse.nodered.org/t/how-to-send-mqtt-node-data-integers-string-to-a-table-in-postgres-database/77570/3 "2023-04-14T16:50:57Z")

</div>

> [@daaven](#):
>
> I can't seem to figure out what to insert in the function node to send to the database to place all the values in the same row when it uploads

Basic understanding of node red should help.

Every node (I.e. all of your MQTT nodes) will output data at different times. Even if the MQTT messages are transmitted at the exact same time, messages travelling down the wires into the function node will never reach the function node at the same time.

In other words, you will only get one piece of information at a time. This can be clearly seen if you add a debug node before the function node.

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

Once you have all of the values in a single message payload, you can easily build an SQL insert query with all of the values.

---

<div class="post-metadata">

**Author:** ![daaven](https://avatars.discourse-cdn.com/v4/letter/d/7ab992/32.png) [@daaven](https://discourse.nodered.org/u/daaven)\
**Post date:** [14 April 2023 18:20 UTC](https://discourse.nodered.org/t/how-to-send-mqtt-node-data-integers-string-to-a-table-in-postgres-database/77570/4 "2023-04-14T18:20:25Z")

</div>

msg.topic = "INSERT INTO pressure\_data (tech\_name, pressure\_value, job\_num, serial\_num, notes) VALUES ($1, $2, $3, $4, $5)";

msg.payload = [msg.payload[0], msg.payload[1], msg.payload[2], msg.payload[3], msg.payload[4]];

return msg;

That is the code currently in the function node. I have modified it, but still get no results

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [14 April 2023 21:01 UTC](https://discourse.nodered.org/t/how-to-send-mqtt-node-data-integers-string-to-a-table-in-postgres-database/77570/5 "2023-04-14T21:01:36Z")

</div>

The help for the node-red-contrib-postgresql says to put values as an array in msg.params not msg.payload:

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/2/72515785951a601b7098f7f4f73a31a6c13845ee.jpeg)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [13 June 2023 21:01 UTC](https://discourse.nodered.org/t/how-to-send-mqtt-node-data-integers-string-to-a-table-in-postgres-database/77570/6 "2023-06-13T21:01:50Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
