# How to insert data to Postgres

**URL:** <https://community.n8n.io/t/how-to-insert-data-to-postgres/17530>\
**Category:** Questions\
**Tags:** postgres\
**Created:** [September 9, 2022, 1:55pm UTC](https://community.n8n.io/t/how-to-insert-data-to-postgres/17530 "2022-09-09T13:55:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![kidrulit](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/kidrulit/32/17002_2.png) [@kidrulit](https://community.n8n.io/u/kidrulit)\
**Post date:** [September 9, 2022, 1:55pm UTC](https://community.n8n.io/t/how-to-insert-data-to-postgres/17530/1 "2022-09-09T13:55:23Z")

</div>

## Describe the issue/error/question

Hey folks, I am trying to understand if there’s a way to execute the INSERT operation in Postgres without writing the query.  
I tried to follow the [documentation](https://docs.n8n.io/integrations/builtin/app-nodes/n8n-nodes-base.postgres/?utm_source=n8n_app&utm_medium=node_settings_modal-credential_link&utm_campaign=n8n-nodes-base.postgres#2-postgres-node-execute-query), and I noticed there’s a [way](https://docs.n8n.io/_images/integrations/builtin/app-nodes/postgres/set_node.png) to map the query parameters to the table values. However, I don’t see that in the interface in my setup.

 ![2022-09-09_16-49-31](https://europe1.discourse-cdn.com/n8n/original/3X/c/9/c949c81ff9393df25baad5e3f4dcb0874f05f72b.png)

In the example above I tried inserting the JSON representation.

```auto
{
  "id": 777654140,
  "Country": {{ $json["query"]["ip_country"] }}
}

```

What should I type into the `Columns` field?  
I’m expecting to see the data table schema here to insert the values. For instance, Integromat follows the same approaches, which I find pretty nice.

Executing the query is not a problem, but I expect the no-code experience when I pick up INSERT mode.

## What is the error message (if any)?

## Please share the workflow

```auto
(Select the nodes and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow, respectively)

```

## Share the output returned by the last node

## Information on your n8n setup

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

---

<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:** [September 9, 2022, 2:07pm UTC](https://community.n8n.io/t/how-to-insert-data-to-postgres/17530/2 "2022-09-09T14:07:10Z")

</div>

Hi @kidrulit  
Check out this example:

> **[Generate and insert data into a Postgres database | n8n workflow template](https://n8n.io/workflows/356-generate-and-insert-data-into-a-postgres-database/)**
>
> This is Workflow 1 in the blog tutorial \[Database activity monitoring and alerting\](https://blog.n8n.io/database-monitoring-and-alerting-with-n8n/).
> 
> \## Prerequ

---

<div class="post-metadata">

**Author:** ![kidrulit](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/kidrulit/32/17002_2.png) [@kidrulit](https://community.n8n.io/u/kidrulit)\
**Post date:** [September 9, 2022, 3:32pm UTC](https://community.n8n.io/t/how-to-insert-data-to-postgres/17530/4 "2022-09-09T15:32:55Z")

</div>

Is there a way to pass variables to Postgres without the Function module?  
It seems there is no option of using the variables from previous sources in Function

I struggle passing data from wbhook to Postgres

---

<div class="post-metadata">

**Author:** ![Jon](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jon/32/40174_2.png) [@Jon](https://community.n8n.io/u/Jon)\
**Post date:** [September 9, 2022, 5:04pm UTC](https://community.n8n.io/t/how-to-insert-data-to-postgres/17530/5 "2022-09-09T17:04:10Z")

</div>

Hey @kidrulit,

You could try using the Execute Query option and writing out your insert query that way. As part of our current node overhaul project though the way data is defined will be changed and will be much easier but that is going to be a few months away yet so for now either a function / set node if you want to use the insert option or building the query manually is the best bet.

---

<div class="post-metadata">

**Author:** ![kidrulit](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/kidrulit/32/17002_2.png) [@kidrulit](https://community.n8n.io/u/kidrulit)\
**Post date:** [September 10, 2022, 7:16pm UTC](https://community.n8n.io/t/how-to-insert-data-to-postgres/17530/6 "2022-09-10T19:16:55Z")

</div>

Thanks!  
Is it possible to list more than one property in the Query parameter field?

My following INSERT query fails when I pass all 14 properties in the `Query Parameters` field.

```auto
INSERT INTO public."page-events" (id, created_at, isp_provider, country, path, hostname, http_referrer, language, hash, utm_medium, utm_source, utm_campaign, utm_content, utm_term)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)

```

It seems that the parameters are simply not loaded in the original query as I receive the error message `ERROR: null value in column "id" of relation "page-events" violates not-null constraint`

That’s how the parameters are listed:

 ![2022-09-10_22-17-26](https://europe1.discourse-cdn.com/n8n/original/3X/2/b/2b8c93978f96a665fb74ca71d0a3df7e9636c9e9.png)

Am I missing something? Can I pass parameters directly to the query?

---

<div class="post-metadata">

**Author:** ![Jon](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jon/32/40174_2.png) [@Jon](https://community.n8n.io/u/Jon)\
**Post date:** [September 11, 2022, 5:33am UTC](https://community.n8n.io/t/how-to-insert-data-to-postgres/17530/7 "2022-09-11T05:33:16Z")

</div>

Hey @kidrulit,

That should be fine, the error seems to be Postgres saying the id is empty. The query you put looks to be very different from what you have in the expression field though.

Once you select execute query you can just pop in the first query and use expressions for the values.

---

<div class="post-metadata">

**Author:** ![kidrulit](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/kidrulit/32/17002_2.png) [@kidrulit](https://community.n8n.io/u/kidrulit)\
**Post date:** [September 11, 2022, 12:51pm UTC](https://community.n8n.io/t/how-to-insert-data-to-postgres/17530/8 "2022-09-11T12:51:55Z")

</div>

Hey @Jon

Thanks for the reply.  
Can you assist me with how does it differ from the Expression field? I didn’t quite get it.

The Insertion works fine when I execute in `pg_admin`  
I simply copied the result of the Expression field and passed it instead of values.

```auto
INSERT INTO public."page-events" (id, created_at, isp_provider, country, path, hostname, http_referrer, language, uid_hash, utm_medium, utm_source, utm_campaign, utm_content, utm_term)
VALUES (10000100, '2022-9-11 12:42', 'Datacamp Limited', 'Japan', '/pricing-page', 'www.spatial.chat', 'www.spatial.chat/pricing', 'jp', 'jsjdjsjdsjdjsj', 'seo', 'google', 'mark', 'none', 'oooo')

```

```auto
INSERT 0 1

Query returned successfully in 894 msec.

```

---

<div class="post-metadata">

**Author:** ![kidrulit](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/kidrulit/32/17002_2.png) [@kidrulit](https://community.n8n.io/u/kidrulit)\
**Post date:** [September 11, 2022, 1:19pm UTC](https://community.n8n.io/t/how-to-insert-data-to-postgres/17530/9 "2022-09-11T13:19:13Z")

</div>

I guess I fundamentally missing how the query parameter mapping works in n8n.  
Let’s test on a more straightforward case.

The input in a Query field:  
`SELECT * FROM public."page-events" WHERE id = $1; `

The input in the Query Parameters field  
`{{ $json["id"] }},` , resulting `10000100,`

 ![2022-09-11_16-17-28](https://europe1.discourse-cdn.com/n8n/original/3X/d/d/dd89883afe323bb005c8ca567a569e641bc78449.png)

The n8n’s output is void.

When I run the `SELECT * FROM public."landing-page-events" WHERE id = 10000100; ` in Query field I get the result of the string matching the id. I assume the Query ignores the Query Parameters field.

---

<div class="post-metadata">

**Author:** ![kidrulit](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/kidrulit/32/17002_2.png) [@kidrulit](https://community.n8n.io/u/kidrulit)\
**Post date:** [September 11, 2022, 1:36pm UTC](https://community.n8n.io/t/how-to-insert-data-to-postgres/17530/10 "2022-09-11T13:36:11Z")

</div>

Ok, I think I got it. Thanks, I found a similar [question](http://community.n8n.io/t/query-parameters-not-working-with-postgres/17203) on the forum.  
Darg-and-dropping value from the previous step into the Query Parameters field doesn’t work; I need to specify the name.  
When I put `id` but not the value to the Query Parameters, I get the result.

It’s super inconvenient, guys I would be happy to see more detailed instructions in the official doc. Is your doc open-sourced, can I contribute there?

---

<div class="post-metadata">

**Author:** ![Jon](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jon/32/40174_2.png) [@Jon](https://community.n8n.io/u/Jon)\
**Post date:** [September 11, 2022, 3:08pm UTC](https://community.n8n.io/t/how-to-insert-data-to-postgres/17530/11 "2022-09-11T15:08:59Z")

</div>

Hey @kidrulit,

The docs can be contributed to by anyone, I think we make some assumptions as most input fields work the same way.

When you use execute query you would use the same query that you would when using the database normally but for your values you can use variables which will be swapped out when running.
