# How to insert variable in postgres query

**URL:** <https://community.n8n.io/t/how-to-insert-variable-in-postgres-query/16976>\
**Category:** Questions\
**Tags:** postgres\
**Created:** [August 23, 2022, 4:15pm UTC](https://community.n8n.io/t/how-to-insert-variable-in-postgres-query/16976 "2022-08-23T16:15:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tthias](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/tthias/32/16352_2.png) [@tthias](https://community.n8n.io/u/tthias)\
**Post date:** [August 23, 2022, 4:15pm UTC](https://community.n8n.io/t/how-to-insert-variable-in-postgres-query/16976/1 "2022-08-23T16:15:29Z")

</div>

## Describe the issue/error/question

Hi,

I tried 10 different things, but I can’t manage to retrieve variables in my query:  
The query : `SELECT 'hello' as col1, $1 as col2`

The variable : {{$json[“Votre email”]}}

I get the values from a google sheets.

What I’m doing wrong ?

Thanks

Matthias

## Please share the workflow

## Share the output returned by the last node

```auto
each row =
{
  "col1": "hello",
  "col2": null
}

```

## Information on your n8n setup

- **n8n version:** Version 1.6.0 (1.6.0)
- **Database you’re using (default: SQLite):**
- **Running n8n with the execution process [own(default), main]:**
- **Running n8n via [Docker, npm, n8n.cloud, desktop app]:** desktop

---

<div class="post-metadata">

**Author:** ![BramKn](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/bramkn/32/11894_2.png) [@BramKn](https://community.n8n.io/u/BramKn)\
**Post date:** [August 24, 2022, 5:58am UTC](https://community.n8n.io/t/how-to-insert-variable-in-postgres-query/16976/2 "2022-08-24T05:58:28Z")

</div>

Hi @tthias

Please share your worklow. You can simply select all nodes and paste it into a message. Please keep in mind to use the pre formatted text option.  
 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/9/9/9960d9b24ab088e4670318ed6536b3599cc98651.png)

---

<div class="post-metadata">

**Author:** ![MutedJam](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mutedjam/32/70851_2.png) [@MutedJam](https://community.n8n.io/u/MutedJam)\
**Post date:** [August 24, 2022, 6:11am UTC](https://community.n8n.io/t/how-to-insert-variable-in-postgres-query/16976/3 "2022-08-24T06:11:42Z")

</div>

Hi @tthias, in your Postgres node you’d simply specify the field name rather than the value of your parameter. So if your Google Sheet node returns data with a key (or column headers if you like) of `col1`, you would use `col1` as the value in Query Parameters. Like so:

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/4/9/49255a6d4416bbb964fae77d228a29979cdf6b01.png)

Hope this helps! Let me know if you run into any problems with this.

---

<div class="post-metadata">

**Author:** ![tthias](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/tthias/32/16352_2.png) [@tthias](https://community.n8n.io/u/tthias)\
**Post date:** [August 24, 2022, 7:58am UTC](https://community.n8n.io/t/how-to-insert-variable-in-postgres-query/16976/4 "2022-08-24T07:58:13Z")

</div>

Hey thanks for your reply !

That’s working perfectly !

I was doing the following:

 ![Screen Shot 2022-08-24 at 9.52.28 AM](https://europe1.discourse-cdn.com/n8n/original/3X/7/c/7cc7ee20d1bd22a4490f4c618875bd6747760e67.jpeg)  
 ![Screen Shot 2022-08-24 at 9.52.16 AM](https://europe1.discourse-cdn.com/n8n/original/3X/f/0/f05fa599e06e16f1d8474a3498ac23e7aeddf8be.png)

Instead of just typing the column name.

Thanks

Matthias

---

<div class="post-metadata">

**Author:** ![MutedJam](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mutedjam/32/70851_2.png) [@MutedJam](https://community.n8n.io/u/MutedJam)\
**Post date:** [August 24, 2022, 7:59am UTC](https://community.n8n.io/t/how-to-insert-variable-in-postgres-query/16976/5 "2022-08-24T07:59:25Z")

</div>

You’re most welcome, glad to hear this works for you!

---

<div class="post-metadata">

**Author:** ![tthias](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/tthias/32/16352_2.png) [@tthias](https://community.n8n.io/u/tthias)\
**Post date:** [August 24, 2022, 9:17am UTC](https://community.n8n.io/t/how-to-insert-variable-in-postgres-query/16976/6 "2022-08-24T09:17:43Z")

</div>

And what if I want to use values from different nodes as query parameters ?  
Do I need to create a node to format the input with all the required values and put it just before the postgres query node ?

---

<div class="post-metadata">

**Author:** ![MutedJam](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mutedjam/32/70851_2.png) [@MutedJam](https://community.n8n.io/u/MutedJam)\
**Post date:** [August 24, 2022, 9:43am UTC](https://community.n8n.io/t/how-to-insert-variable-in-postgres-query/16976/7 "2022-08-24T09:43:05Z")

</div>

Yes, a typical approach would be a [Set](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.set/) node in front of nodes processing incoming data “as is”. This allows you to define the exact fields you’d like to have using [expressions](https://docs.n8n.io/code-examples/expressions/) which in turn can reference data from a previous node.

The expression editor comes with a little browser so you don’t have to write simple expressions manually. Simply select the node you want to read data from and click on the respective field name:

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/3/b/3ba676279f60bc8c02f40cc5d57ee25d9fd0ff69.png)
