Esta página no está disponible en español. Se muestra la versión en inglés.

Scheduled Postgres → Gmail report

Every week, query Postgres, have an LLM write a short summary, and email it with Gmail.

En esta página

In this guide you build a weekly report that runs by itself. It reads numbers from your Postgres database, asks an LLM to turn them into a short written summary, and emails it to your team from your Gmail account.

It is for ops, finance, and product teams who want a regular report without anyone building it by hand. It takes about 30 minutes.

Outcome: every Monday morning, the people you choose receive an email with your key numbers and a plain-language summary.

Prerequisites

Connectors to install

AdapterPurpose
postgresRun the report query
gmailSend the email from your Google account

Install each from MCP → Add connectors (click the connector, then Add to workspace) and connect it on Instance config.

Example report query

Change this to fit your tables. This one totals the last seven days of orders:

SELECT
  DATE_TRUNC('week', created_at) AS week,
  COUNT(*) AS order_count,
  SUM(total)::numeric(12,2) AS revenue
FROM orders
WHERE created_at >= NOW() - INTERVAL '7 days'
GROUP BY 1
ORDER BY 1 DESC;

Build the workflow

  1. Create the workflow

    Name it weekly-ops-report in Workflow Studio.

  2. Query Postgres

    Add an mcp step with postgres_query and your SQL. postgres_query only allows read queries.

  3. Write the summary

    Add an LLM step that turns the rows into a short summary with highlights and anything unusual.

  4. Send the email

    Add an mcp step with gmail_send_email to your distribution list.

  5. Test it

    Start a manual run. What you should see: the run succeeds and the email arrives.

  6. Schedule it

    Open Workflow setup → Cron, choose the Weekly Mon 09:00 preset (or type a Cron (5-field) expression such as 0 8 * * 1), choose a Timezone, choose an Automation API key, and click Add schedule.

  7. Optional: add approval

    For reports that need a sign-off, add a human step between the LLM and Gmail steps. See Approve then send.

What you should see after step 6: the schedule under Active schedules, with the cron expression, timezone, and time of the next run. See External triggers.

Query step

{
  "id": "weekly-metrics",
  "type": "mcp",
  "name": "Fetch weekly KPIs",
  "instance_id": "REPLACE_POSTGRES_INSTANCE",
  "tool_name": "postgres_query",
  "tool_args": {
    "sql": "SELECT DATE_TRUNC('week', created_at) AS week, COUNT(*) AS order_count, SUM(total)::numeric(12,2) AS revenue FROM orders WHERE created_at >= NOW() - INTERVAL '7 days' GROUP BY 1 ORDER BY 1 DESC"
  },
  "timeout_s": 60
}

LLM step

{
  "id": "write-summary",
  "type": "llm",
  "name": "Executive summary",
  "model_ref": "REPLACE_MODEL_REF",
  "prompt": "You are a business analyst. Given this weekly metrics JSON from our database, write a concise email body (plain text, 3-5 short paragraphs) for leadership. Call out week-over-week changes if visible. Data: {{steps.weekly-metrics.result.rows}}",
  "depends_on": ["weekly-metrics"],
  "timeout_s": 120
}

Gmail step

{
  "id": "send-report",
  "type": "mcp",
  "name": "Email weekly report",
  "instance_id": "REPLACE_GMAIL_INSTANCE",
  "tool_name": "gmail_send_email",
  "tool_args": {
    "to": ["leadership@example.com"],
    "subject": "Weekly ops report",
    "body": "{{steps.write-summary.result.text}}"
  },
  "depends_on": ["write-summary"],
  "timeout_s": 30
}

to is a list of addresses. Add "body_type": "html" if the body is HTML. The default is plain text.

Full workflow

Paste this into the JSON editor, then replace REPLACE_POSTGRES_INSTANCE and REPLACE_GMAIL_INSTANCE with the ids of your instances, and REPLACE_MODEL_REF with a model from your Providers catalog. Change the SQL and the recipients first.

{
  "steps": [
    {
      "id": "weekly-metrics",
      "type": "mcp",
      "name": "Fetch weekly KPIs",
      "instance_id": "REPLACE_POSTGRES_INSTANCE",
      "tool_name": "postgres_query",
      "tool_args": {
        "sql": "SELECT DATE_TRUNC('week', created_at) AS week, COUNT(*) AS order_count, SUM(total)::numeric(12,2) AS revenue FROM orders WHERE created_at >= NOW() - INTERVAL '7 days' GROUP BY 1 ORDER BY 1 DESC"
      },
      "timeout_s": 60
    },
    {
      "id": "write-summary",
      "type": "llm",
      "name": "Executive summary",
      "model_ref": "REPLACE_MODEL_REF",
      "prompt": "You are a business analyst. Given this weekly metrics JSON from our database, write a concise email body (plain text, 3-5 short paragraphs) for leadership. Call out week-over-week changes if visible. Data: {{steps.weekly-metrics.result.rows}}",
      "depends_on": ["weekly-metrics"],
      "timeout_s": 120
    },
    {
      "id": "send-report",
      "type": "mcp",
      "name": "Email weekly report",
      "instance_id": "REPLACE_GMAIL_INSTANCE",
      "tool_name": "gmail_send_email",
      "tool_args": {
        "to": ["leadership@example.com"],
        "subject": "Weekly ops report",
        "body": "{{steps.write-summary.result.text}}"
      },
      "depends_on": ["write-summary"],
      "timeout_s": 30
    }
  ]
}

Common problems

ProblemWhat to check
The query step failsThe SQL has an error, the table does not exist, or the connection is not wired on the instance
postgres_query only allows SELECT/CTE queriesThe report must be a SELECT or WITH query
The email is not sentGmail is not turned on for the Google connection, or to is not a list
The schedule never runsCheck Active schedules for the next run time and timezone, and that the schedule has an Automation API key
The summary invents numbersTell the LLM in the prompt to use only the data given

Variations

  • Write the rows to Google Sheets with google_sheets_insert_multiple_rows.
  • Send with Resend (resend_send_email) from a verified domain.
  • Run several queries (revenue, support tickets, churn) before one LLM step.

Next steps

Editar esta páginaagentruntime-io/agentruntime-docs