Postgres Insert, or inserting a JSON array as one value

Hi - I’m trying to use the “Insert” Operation for Postgres. What should I put into the Columns and “Return Fields” inputs? I’m not seeing where to specify the data that is passed into this node.

I’m also happy to write the insert as a raw query…running into trouble trying to insert an array into one jsonb cell. Is there a way to force this into a working json? I’m running JSON.stringify but still getting an error "syntax error at or near “json” – but I don’t have “json” anywhere visible.

Thank you!

Hey @Byron,

You need to use the Set node to map the incoming values with your Postgres columns. You can then specify the column names in the Columns fields. The Return Fields allows you to return the values that you inserted. You can use the default value (*).

Below is a small example. In this example, I am inserting the name and age into an example table.

{
  "nodes": [
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "name",
              "value": "nathan"
            },
            {
              "name": "age",
              "value": "8"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "table": "example",
        "columns": "name,age",
        "additionalFields": {}
      },
      "name": "Postgres",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Set": {
      "main": [
        [
          {
            "node": "Postgres",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Let me know if this helps :slight_smile:

2 Likes

That works, thank you so much! I have other questions but will start a new thread.

1 Like