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:

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!