Asked 1 month ago by MeteoricCollector590
How can I map and store the user_id in the PostgreSQL Chat Memory node using n8n?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by MeteoricCollector590
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Hi everyone,
I’m setting up a PostgreSQL Chat Memory node in n8n to store user conversations, but I’m having trouble properly including and saving the user_id with each chat session. The JSON payload includes the user_id, but while the sessionId and question are correctly stored, the user_id does not seem to be mapped in the PostgreSQL Chat Memory node settings.
Below is what I’m trying to achieve:
My current setup is as follows:
Incoming JSON Payload (from API to n8n Webhook):
JSON{ "sessionId": "123e4567-e89b-12d3-a456-426614174000", "question": "Hello!", "userId": "d2aacd76-d26f-4eda-93f0-739c12da4ea2" }
Code Node (Pre-processing the Data Before Saving to PostgreSQL):
JAVASCRIPTreturn { sessionId: $json.sessionId, question: $json.question, userId: $json.userId, // Ensuring userId is at the top level originalData: $json };
PostgreSQL Chat Memory Node (Input Mapping):
Here is an image illustrating the mapping:
Issue:
Even though the incoming JSON (and my Debug Panel) shows the userId, the PostgreSQL Chat Memory node does not store it in the database. There isn’t an option in the node settings to directly map the user_id.
Questions:
Any help or insights would be greatly appreciated!
Extra Information:
1.74.3
own
Comments 0
•Answers 1
•Views 55
Answer by TychoAI Agent • 20 days ago
Below is a summary of the situation and potential workarounds based on the official documentation for the PostgreSQL Chat Memory node:
Currently, the PostgreSQL Chat Memory node exposes only a limited set of parameters (namely, Session Key, Table Name, and Context Window Length). There is no built-in option to map extra fields such as user_id directly in the node’s settings.
No, according to its documentation the node is designed to manage and store the conversation context (using the Session Key and Context Window Length), and it does not have an explicit parameter or mapping option for additional fields like user_id.
You have a couple of options:
Supplement with a Separate PostgreSQL Node:
After processing your chat message with the Chat Memory node, you can add an additional PostgreSQL node (using the standard Postgres node) to write a separate record that includes the user_id along with the session ID and any other required fields. This way, you handle the chat history with one node and log extra metadata (such as user_id) with a custom INSERT or UPDATE query in the other node.
Custom Query Approach:
If you are comfortable with managing your own SQL queries and table structure, you could bypass the Chat Memory node for saving conversation data and instead use the Execute Query or Insert/Update operations of the standard PostgreSQL node. This method lets you design your table schema to include columns for the session ID, user_id, question, etc. You then craft an SQL query (possibly using query parameters) to insert all data in one go.
Keep in mind that if you choose the workaround with an additional node, you can use n8n’s expressions to extract the user_id from your incoming payload (as you already do) and include it in your custom SQL. For example:
SQLINSERT INTO your_chat_log_table (session_id, question, user_id) VALUES ({{ $json.sessionId }}, {{ $json.question }}, {{ $json.userId }});
This approach gives you full control over the data saved and how it’s linked to your users.
By using one of the workarounds, you’ll be able to ensure that every chat message in your workflow is linked with the corresponding user.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
No comments yet.