Postgres update/add

Hi

I have one table in posgres and i have a number of items. I would then check if that item exists in the database if it is not existing not it should be added. How could this be done? By query and then an if formula?

= lookup item, if existing = stop
= lookup item, if no existing = add

I have tried a few different ones but not really got it to work.

Kind regards
Mattias

I guess the problem you have is that the Postgres-Node does not output anything in case it does not find the item. So you have to set on the Postgres-Node under Node (not under Parameters!) the option “Always Output Data” to “true”. If you do not do that it will not output anything and the workflow will stop. Only with that option will it at least output an empty item and you can then check for it.

I created the following example workflow which assumes you have a table called “product” which has the properties: id, timestamp

Apparently I am still struggling, now the set node is only taking the first item from my array, I get really confused, but probably it is on my side:)

Not sure what you want to do exactly but looks generally correct to me. Without knowing what the node “convertSubscribers” does actually output and the same what the input of the “Set” nodes does output it is impossible to say for sure what is going wrong. Additionally would also have to know what you expect the “Set” node to do.

In the convertSubribers do i make an array become repeating items. Then i use the postgres to check if the item already exists. If yes then it should update (true on if) and if not exist it should add. The set node i used to prepare the json that should to into postgres. I just copied the example you gave me. The problem i have is is that it only add the first item of the array and to not repeat for all of them.

Honestly still do not understand what exactly you are doing. I really need an example workflow I can run to be able to help you.

Sorry did not see that all nodes was not added, that explain alot:) Sorry for that, in chrome on mac it have been abit tricky to copy nodes lately.

Because of the way the Postgres node currently operates there is right now sadly no simple way to build such a workflow. At least not with my very limited postgres query building skills. For that reason did I have to use SplitInBatches Node.

Here an example workflow which creates n8n workflows directly in the database and so does something similar like you want to do:

Thanks! I will try, then we know at leat it was not only me. Thanks for the help

Mattias, hi!
In the end, how did you manage to implement updating existing lines and adding new ones via N8N?

Hey @11110,

Welcome to the community :sparkling_heart:

Did you check the workflow Jan shared? It is a good example of workflow to get this working. If you’ve any questions, or have run into issues, please feel free to let us know :slight_smile:

We are using that one but it have some limitations Both performance and use case wise