πŸ”¬ Export AI agent conversation logs from Postgres to Google Sheets

⚑ 1,071 views Β· πŸ”¬ Document Extraction & Analysis

Description

Overview

This n8n workflow retrieves AI agent chat memory logs stored in Postgres and pushes them to Google Sheets, creating one sheet per session. It’s useful for teams building chat-based products or agents and needing to review or analyze session logs in a collaborative format.

Who is it for

Prerequisites

Google Sheets Template

This workflow expects a Google Sheets file where each session will be stored in its own tab. A basic tab layout is duplicated and renamed with the session ID. πŸ‘‰ Use this template as a starting point Note: You can hide the template after the first tabs have been created

How it works

  1. Trigger The workflow can be launched manually or on a schedule (e.g. daily at noon).

  2. Retrieve sessions Runs a SQL query to get distinct session_id values from the n8n_chat_histories table.

  3. Loop over sessions For each session:

    • Clears the corresponding sheet (if it exists).
    • Duplicates the template tab.
    • Renames it with the current session_id.
  4. Fetch messages Selects all messages linked to the session from Postgres.

  5. Append to sheet Adds each message to the Google Sheet with columns:

    • Who: speaker role (user, assistant, etc.)
    • Message: text content
    • Date: timestamp from created_at, formatted yyyy-MM-dd hh:mm:ss

Notes

πŸ‘‰ If you’re looking for a solution to better visualize and analyse conversations, reach out to us!

πŸ”— Nodes Used

Google Sheets, HTTP Request, Postgres, Schedule Trigger

πŸ“₯ Import

Download workflow.json and import into n8n: Workflow menu β†’ Import from File

πŸ“– Importing guide Β· πŸ”‘ Credential setup