# Function Node send short Strings to PostgreSql?

**URL:** <https://discourse.nodered.org/t/function-node-send-short-strings-to-postgresql/98261>\
**Category:** General\
**Created:** [23 July 2025 06:59 UTC](https://discourse.nodered.org/t/function-node-send-short-strings-to-postgresql/98261 "2025-07-23T06:59:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Starfoxfs](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/starfoxfs/32/69119_2.png) [@Starfoxfs](https://discourse.nodered.org/u/Starfoxfs)\
**Post date:** [23 July 2025 06:59 UTC](https://discourse.nodered.org/t/function-node-send-short-strings-to-postgresql/98261/1 "2025-07-23T06:59:00Z")

</div>

Hi,

I using a function Node to add my Strings to Postgresql:

```auto
var totalkwh = msg.payload["opendtu/yieldtotal"];
var todaykwh = msg.payload["opendtu/yieldday"];
var voltage = msg.payload["opendtu/voltage"];
var ampere = msg.payload["opendtu/current"];

msg.payload = 
    {
        
       totalkwh: totalkwh,     
        todaykwh: todaywh,
        voltage: voltage,
        ampere: ampere     
        
    }
    
return msg;

```

Now i insert it into Postgresql like this:

```auto
INSERT INTO emeter (totalkwh) VALUES ({{msg.payload.totalkwh}})

```

Is there a way to shorten msg.payload.totalkwh to e.g. msg.totalkwh or only a variable $totalkwh

I have much payloads about 20 so the INSERT QUERY is very very long.

---

<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:** [23 July 2025 08:24 UTC](https://discourse.nodered.org/t/function-node-send-short-strings-to-postgresql/98261/2 "2025-07-23T08:24:56Z")

</div>

> [@Starfoxfs](#):
>
> Is there a way to shorten msg.payload.totalkwh to e.g. msg.totalkwh or only a variable $totalkwh

Yes, don't build dynamic SQL 😉

Apart from the risk of SQL injection that dynamic SQL can expose, handling quotes and some other characters in strings can cause problems!

Instead, use the parameterised queries.

See an example here: [Using PostgreSQL with Node-RED • FlowFuse](https://flowfuse.com/node-red/database/postgresql/#inserting-product-data-into-the-database)

And the info in the README: [GitHub - alexandrainst/node-red-contrib-postgresql: Node-RED node for PostgreSQL, supporting parameters, split, back-pressure](https://github.com/alexandrainst/node-red-contrib-postgresql?tab=readme-ov-file#parameterized-query-numeric)

---

<div class="post-metadata">

**Author:** ![Starfoxfs](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/starfoxfs/32/69119_2.png) [@Starfoxfs](https://discourse.nodered.org/u/Starfoxfs)\
**Post date:** [24 July 2025 20:08 UTC](https://discourse.nodered.org/t/function-node-send-short-strings-to-postgresql/98261/3 "2025-07-24T20:08:03Z")

</div>

I have a multi table design.

So i have one value field for 5 values that came from Mqtt

How can i set msg.params to handle this ?

e.g.

```auto
var total = msg.payload.MT175.E_in;
var watt = msg.payload.MT175.P;
var device = "emeter"
var field = "totalemeter"
var time = new Date();

msg.params = [time, device, field, total];
    
return msg;

```

The Total and the Watt Values are in the same Database column.

Can i send msg.params2 ? and a second INSERT INTO ?

---

<div class="post-metadata">

**Author:** ![Starfoxfs](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/starfoxfs/32/69119_2.png) [@Starfoxfs](https://discourse.nodered.org/u/Starfoxfs)\
**Post date:** [25 July 2025 12:56 UTC](https://discourse.nodered.org/t/function-node-send-short-strings-to-postgresql/98261/4 "2025-07-25T12:56:00Z")

</div>

I have it:

```auto
msg.params = [time, device, sensor, totalemeter, wattemeter, field, field2];

```

Use all 7 Variables with msg.parms to the Postgresql Insert Node then Edit the PostgreSql Insert Node as shown here:

```auto

INSERT INTO emeter (time, device, sensor, value, field) VALUES ($1, $2, $3, $4, $6),($1, $2, $3, $5, $7);

```

$4 and $6  
$5 and $7

are inserted into value and field, that works fine.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [25 July 2025 13:37 UTC](https://discourse.nodered.org/t/function-node-send-short-strings-to-postgresql/98261/5 "2025-07-25T13:37:38Z")

</div>

If you send an object instead of an array you can use the property names, can make it a bit easier perhaps

---

<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:** [8 August 2025 13:37 UTC](https://discourse.nodered.org/t/function-node-send-short-strings-to-postgresql/98261/6 "2025-08-08T13:37:55Z")

</div>

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