How to Add PostgreSQL Chat Memory to Your n8n AI Agent

How to Add PostgreSQL Chat Memory to Your n8n AI Agent

If you’re building an AI agent in n8n and want to maintain conversation context using a robust database, integrating PostgreSQL chat memory is a great choice. This lets your AI remember previous interactions across sessions, making conversations more natural and effective.

In this guide, you’ll learn how to quickly add PostgreSQL chat memory to your n8n AI agent.


Step 1: Add Memory to Your AI Agent

  1. Open your AI agent in n8n.
  2. Click on Add Memory.
  3. From the options, select PostgreSQL Chat Memory.
  4. Create and configure a new PostgreSQL connection by entering your database details.
  5. Save the connection.

Step 2: Start Your Chat

Once connected, start your chat as usual. The PostgreSQL Chat Memory integration will automatically create the necessary database table to store chat history if it doesn’t already exist.

Here is the table schema that gets created:

sqlCopyEditCREATE TABLE IF NOT EXISTS public.n8n_chat_histories
(
    id integer NOT NULL DEFAULT nextval('n8n_chat_histories_id_seq'::regclass),
    session_id character varying(255) NOT NULL,
    message jsonb NOT NULL,
    CONSTRAINT n8n_chat_histories_pkey PRIMARY KEY (id)
);
  • id: Auto-incrementing primary key.
  • session_id: Unique identifier to group messages by conversation session.
  • message: JSONB column storing individual messages with role and content.

Why Use PostgreSQL Chat Memory?

  • Persistent conversation history across restarts.
  • Scalable and reliable storage solution.
  • Works seamlessly with n8n’s AI integrations like OpenAI and Google Gemini.
  • Allows for complex querying or extensions like vector search if needed.

Final Thoughts

Adding PostgreSQL chat memory in n8n is straightforward and powerful. It lets your AI agent maintain rich conversation context over time, improving the user experience and enabling more advanced workflows.

Ready to try it out? Head to your n8n AI agent and add PostgreSQL Chat Memory today!