I’m currently setting up a PostgreSQL Chat Memory node in n8n to store user conversations, but I’m struggling to properly include and save the user_id alongside each chat session. What I’m trying to achieve:
Each chat message should be stored in PostgreSQL with a session ID and user ID.
The user_id is included in the JSON payload sent to n8n.
I need to ensure that the PostgreSQL Chat Memory node correctly maps and stores the user_id in my database.
Code Node (Pre-processing the Data Before Saving to PostgreSQL):
return {
sessionId: $json.sessionId,
question: $json.question,
userId: $json.userId, // Ensuring userId is at the top level
originalData: $json
};
PostgreSQL Chat Memory Node (Input Mapping):
Session ID: {{$json[“sessionId”]}} (works)
Question: {{$json[“question”]}} (works)
User ID: {{$json[“userId”]}} (not getting stored)
As you can see in the image:
Issue:
Even though I can see userId in the incoming JSON and in my Debug Panel, the PostgreSQL Chat Memory node does not seem to store it in my database. I don’t see an option to directly map user_id in the node settings.
My Questions:
How can I properly map the user_id so that it gets saved in PostgreSQL?
Does the PostgreSQL Chat Memory node support storing additional fields like user_id?
If not, is there a recommended workaround to ensure that every chat message is linked to the correct user?
Any help or insights would be greatly appreciated! Thanks in advance.
Storing the userId like that is probably not valid via the postgres chat memory node. According to the Postgres Chat Memory node documentation:
… the node is primarily designed to store chat history, and it doesn’t mention capabilities for storing additional user-specific data.
A potential workaround you can try is to use the metadata field:
You can include additional information in the metadata. This example is for the Chat Trigger node but a similar approach might work for the PostgreSQL Chat Memory node.
You could try modifying your Code node to include the user_id in the metadata:
However as you can see in the image, after updating the PostgreSQL Chat Memory node, I can’t find an option to map or store the metadata field in the database. There doesn’t seem to be built-in support for metadata in this node.
I also checked my database (n8n_chat_histories), but the metadata field is not present or automatically stored. I even tried manually adding a metadata column in my table, but the PostgreSQL Chat Memory node doesn’t seem to recognize or save it.
Could you clarify how exactly the metadata should be stored and retrieved in the PostgreSQL Chat Memory node? Or is there another workaround to ensure the userId is properly saved with each chat message?
Hi Mark,
Did you manage to find a solution for this? I’m running into the same issue when trying to insert new rows into my chat_histories table in Supabase using the Postgres chat memory node. I’m including the user_id in the request body, but I haven’t found a way to send it as metadata yet.
yeah, N8N has to start making it possible for us to create production ready products and that would mean implementing user id specefic chat sessions in the chat node or a new node for that. That would solve this problem along with unlocking a huge potential for n8n to become production ready
right now the only solution is to create a metadata for storying vector database for each chat (not the best way for having chat history) or writing the code manually and existing n8n. Also, I have to mention that even if you implement this via n8n, unfortunately you cannot use it really for serios production bc every chat message counts as a execution and not each session, that means 5 messages in at the same 2 minute interval from your user will be 5 executions, meaning you are gonna have to sell a kidney if you start having serious number of users and want to stay with n8n