# How do I write dynamic data to Postgres DB

**URL:** <https://discourse.nodered.org/t/how-do-i-write-dynamic-data-to-postgres-db/67682>\
**Category:** General\
**Tags:** database\
**Created:** [14 September 2022 07:35 UTC](https://discourse.nodered.org/t/how-do-i-write-dynamic-data-to-postgres-db/67682 "2022-09-14T07:35:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eldho1416](https://avatars.discourse-cdn.com/v4/letter/e/977dab/32.png) [@eldho1416](https://discourse.nodered.org/u/eldho1416)\
**Post date:** [14 September 2022 07:35 UTC](https://discourse.nodered.org/t/how-do-i-write-dynamic-data-to-postgres-db/67682/1 "2022-09-14T07:35:53Z")

</div>

Hello Everyone,

I'm doing a project in which I want to send data to my local MQTT broker and I will get the data in nodered by subscribing to the MQTT broker. Now I want to Insert the data coming out from "MQTT in" node to postgres DB. How do I Achieve this?

I have attached the flow for better understanding

 ![Capture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b2f713c5e15f9e11c1051b3a590c8491ec9d028b.png)

---

<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 September 2022 09:08 UTC](https://discourse.nodered.org/t/how-do-i-write-dynamic-data-to-postgres-db/67682/2 "2022-09-14T09:08:12Z")

</div>

Do you know the name and structure of the database table?  
Are you familiar with SQL?

---

<div class="post-metadata">

**Author:** ![eldho1416](https://avatars.discourse-cdn.com/v4/letter/e/977dab/32.png) [@eldho1416](https://discourse.nodered.org/u/eldho1416)\
**Post date:** [14 September 2022 09:51 UTC](https://discourse.nodered.org/t/how-do-i-write-dynamic-data-to-postgres-db/67682/3 "2022-09-14T09:51:16Z")

</div>

I'm not very familiar with SQL, yes i do know name and structure of DB table.

---

<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 September 2022 10:09 UTC](https://discourse.nodered.org/t/how-do-i-write-dynamic-data-to-postgres-db/67682/4 "2022-09-14T10:09:26Z")

</div>

You need to convert your MQTT message into SQL

The SQL will be something like this.  
Because I don't have a Postgre database I'm not sure if it needs to be in msg.payload or msg.topic.

```auto
INSERT INTO table_name (temp, humidity) VALUES ( {{{msg.Temp}}}, {{{msg.Humidity}}} )

```

A Change node like this might construct the SQL statement.

```auto
[
    {
        "id": "fc1c0b042a761aa4",
        "type": "change",
        "z": "ac631a6e.90e038",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "Temp",
                "pt": "msg",
                "to": "payload.Temp",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "Humidity",
                "pt": "msg",
                "to": "payload.Humidity",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "INSERT INTO table_name(temp, humidity) VALUES({{{ msg.Temp }}}, {{{ msg.Humidity }}})",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 380,
        "y": 280,
        "wires": [
            []
        ]
    }
]

```

---

<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 November 2022 10:10 UTC](https://discourse.nodered.org/t/how-do-i-write-dynamic-data-to-postgres-db/67682/5 "2022-11-13T10:10:01Z")

</div>

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