New Tool Radar

AI Agents & Automation

How to Add a Postgres Query Tool to an n8n AI Agent

Give an n8n AI agent a Postgres query tool so it can answer questions from your database instead of guessing.

Schematic with three steps: Add a Postgres node as a tool; Describe the tool and its limits; Ask a question with a known answer.

An agent that can only talk will invent numbers when asked about your data. A Postgres node wired as a tool lets an n8n AI agent run a read query and answer from real rows. Keep the tool read-only: give it a database credential that can only select, and describe the tool so the agent knows which tables it may read and that it must query before answering.

What you need

  • An n8n workflow with a working AI Agent node and chat model
  • A Postgres database with a read-only credential saved in n8n
  • The table and column names the agent is allowed to read, written down

Step 1: Add a Postgres node as a tool on the agent

On the AI Agent node, open the Tool connector and add a Postgres node with a read query. Point it at the table the agent may read.

Step 2: Describe the tool and its limits

Name the tool lookup_orders or similar, and describe it as read-only: which table it reads, what question it answers, and that the agent must run it before answering any question about that data. A vague description invites the agent to answer from memory instead.

Step 3: Ask a question with a known answer and compare

Ask the agent a question whose answer you already know from the database, then compare its reply with the real rows. If it answers without calling the tool, or quotes a wrong number, tighten the description and re-test.

Worked example: synthetic order lookup

The table below is a synthetic example. It shows the expected result of querying the database; no query was run.

Check Expected Result
Agent called the Postgres tool Yes Pass
Answer matches the database row Yes Pass
No invented numbers 0 invented Pass
Only the allowed table was read Yes Pass

Expected result (UNTESTED). the agent calls the Postgres tool, and its answer matches the real row with no invented numbers. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • Use a read-only database credential for the tool, so a bad query cannot change or delete data.
  • The agent reads the tool description to decide when to query, so name the exact table and question it covers.
  • An agent with no tool will answer from memory; check the execution log to confirm the tool was actually called.

Sources

See all