# How to pass msg int value, to postgresql

**URL:** <https://discourse.nodered.org/t/how-to-pass-msg-int-value-to-postgresql/63389>\
**Category:** Dashboard\
**Tags:** database\
**Created:** [2 June 2022 11:56 UTC](https://discourse.nodered.org/t/how-to-pass-msg-int-value-to-postgresql/63389 "2022-06-02T11:56:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![retegi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/retegi/32/62526_2.png) [@retegi](https://discourse.nodered.org/u/retegi)\
**Post date:** [2 June 2022 11:56 UTC](https://discourse.nodered.org/t/how-to-pass-msg-int-value-to-postgresql/63389/1 "2022-06-02T11:56:42Z")

</div>

Hi! I have a int value y msg, but now i need insert this value into a postgresql.

Postgresql query incorrect because cant read "msg":  
INSERT INTO scada\_level (datetimeLevel,level) VALUES (current\_timestamp,msg);

How can i put msg value in query?

 ![124value](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/6/162f0a60ad7f3503e1de41fd357e45529b9ae441.png)  
 ![msg_into_query](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/1/e1c4d959af4e0981322bd8ba4bc352db43986797.png)

Thanks!

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [2 June 2022 12:22 UTC](https://discourse.nodered.org/t/how-to-pass-msg-int-value-to-postgresql/63389/2 "2022-06-02T12:22:06Z")

</div>

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

---

<div class="post-metadata">

**Author:** ![retegi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/retegi/32/62526_2.png) [@retegi](https://discourse.nodered.org/u/retegi)\
**Post date:** [2 June 2022 12:26 UTC](https://discourse.nodered.org/t/how-to-pass-msg-int-value-to-postgresql/63389/3 "2022-06-02T12:26:18Z")

</div>

Here:  
I only need payload to insert in query.  
 ![detail](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/2/d2646e8c2c75e86a6b5123b88e2745a353c3f15f.png)

How can i insert msg.payload into a query?

thanks!

---

<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:** [2 June 2022 12:37 UTC](https://discourse.nodered.org/t/how-to-pass-msg-int-value-to-postgresql/63389/4 "2022-06-02T12:37:02Z")

</div>

> [@retegi](#):
>
> How can i insert msg.payload into a query

Build the query using the msg parts you need, getting the path to the items of interest like @zenofmud showed you.

There are examples on the nodes [readme](https://flows.nodered.org/node/node-red-contrib-postgresql) page (assuming you are using `node-red-contrib-postgresql` - you didnt say!)

e.g...

> ### SQL query template
> 
> This node uses the [Mustache template system](https://github.com/janl/mustache.js) to generate queries based on the message:
> 
> ```auto
> -- INTEGER id column
> SELECT * FROM table WHERE id = {{{ msg.id }}};
> 
> -- TEXT id column
> SELECT * FROM table WHERE id = '{{{ msg.id }}}';
> 
> ```

This works 👆 but is prone to SQL Injection and quote errors.

This 👇is better...

> ### Parameterized query (numeric)
> 
> Parameters for parameterized queries can be passed as a parameter array `msg.params`:
> 
> ```auto
> // In a function, provide parameters for the parameterized query
> msg.params = [msg.id];
> 
> ```
> 
> ```auto
> -- In this node, use a parameterized query
> SELECT * FROM table WHERE id = $1;
> 
> ```
> 
> ### Named parameterized query
> 
> As an alternative to numeric parameters, named parameters for parameterized queries can be passed as a parameter object `msg.queryParameters`:
> 
> ```auto
> // In a function, provide parameters for the named parameterized query
> msg.queryParameters.id = msg.id;
> 
> ```
> 
> ```auto
> -- In this node, use a named parameterized query
> SELECT * FROM table WHERE id = $id;
> 
> ```

---

<div class="post-metadata">

**Author:** ![retegi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/retegi/32/62526_2.png) [@retegi](https://discourse.nodered.org/u/retegi)\
**Post date:** [2 June 2022 13:54 UTC](https://discourse.nodered.org/t/how-to-pass-msg-int-value-to-postgresql/63389/5 "2022-06-02T13:54:27Z")

</div>

> [@Steve-Mcl](#):
>
> `msg.params = [msg.id];`

Thnaks! Now is correct with msg.params = [msg.payload];

![msgparams](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/e/4e2068c56bbdef40302a09d8d0b4ec1b9568f34a.png)

 ![correctValue](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/c/cce868c6f56fbb58af9ec046c561f14b12547759.png)  
 ![postgresql](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/d/fded74e96d63da5e14b67991346238410a79353d.png)

---

<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:** [16 June 2022 13:55 UTC](https://discourse.nodered.org/t/how-to-pass-msg-int-value-to-postgresql/63389/6 "2022-06-16T13:55:20Z")

</div>

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