How to Store user_id in PostgreSQL Chat Memory in n8n?

Hi everyone,

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.

My Current Setup:

Incoming JSON Payload (from API to n8n Webhook):

{
   "sessionId": "123e4567-e89b-12d3-a456-426614174000",
   "question": "Hello!",
   "userId": "d2aacd76-d26f-4eda-93f0-739c12da4ea2"
}

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”]}} :white_check_mark: (works)
  • Question: {{$json[“question”]}} :white_check_mark: (works)
  • User ID: {{$json[“userId”]}} :x: (not getting stored)

As you can see in the image:
afbeelding

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:

  1. How can I properly map the user_id so that it gets saved in PostgreSQL?

  2. Does the PostgreSQL Chat Memory node support storing additional fields like user_id?

  3. 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.

Extra Information:

  • n8n Version: 1.74.3
  • Database: PostgreSQL (running on Supabase)
  • n8n EXECUTIONS_PROCESS setting: own
  • Running n8n via: Docker Desktop App
  • Operating System: Windows

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hi @

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:

return {
sessionId: $json.sessionId,
question: $json.question,
metadata: {
userId: $json.userId
},
originalData: $json
};

Then, in the PostgreSQL Chat Memory node, you might be able to access this metadata when retrieving the chat history.

Can you try that?

Hi gualter,

Thanks for your suggestion! I updated my Code Node to store the userId inside the metadata field as you suggested:

return {
    sessionId: $json.sessionId,
    question: $json.question,
    metadata: {
        userId: $json.userId
    },
    originalData: $json
};

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?

Thanks again for your help!

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

Hi Joaquin,

I did it like this! I just added a PostgreSQL Update node with these settings:

Hopefully that helps!

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.