Skip to content
Leon van Zyl
0:21:13
1
2
1
Last update : 12/04/2025

Build a Local AI Agent Using n8n, Ollama, and PostgreSQL 🚀

Table of Contents

Discover how to create your own AI agent that runs entirely on your local machine—no coding skills required! This walkthrough outlines the steps to combine n8n’s workflow automation capabilities, Ollama’s large language models (LLMs), and PostgreSQL as a vector database to construct a private, highly functional AI assistant. The process is free, privacy-focused, and simple to replicate on various systems. If you’re an AI enthusiast, developer, or business user seeking robust local AI solutions, you’ll benefit from the tips shared here.


🌐 Why Build a Local AI Agent?

AI technology has seen massive adoption, but most solutions require expensive cloud subscriptions or external services like OpenAI. Building your own AI agent locally bypasses these concerns while providing a thoughtful alternative.

Advantages:

  • Privacy ✨: Your data remains secure on your local machine.
  • Cost-Effective 💰: No paid APIs or subscriptions required.
  • Customizable 🛠️: Tailor the agent to suit your unique needs.

Now, let’s dig into the essentials.


💻 Setting Up Your Local Environment

1. Install Ollama for Running AI Models

Ollama allows you to run LLMs directly from your machine.

Steps:

  • Visit Ollama.com and download the app for your operating system.
  • Open your terminal and verify the installation by typing ollama. A list of commands should appear.
  • Download two models using Ollama’s model manager:
    • Llama 3.2 Chat Model: A lightweight model suitable for conversations.
    • Nomic Embed Text Model: Used for vectorizing documentation.

💡 Pro Tip: Llama 3.2 is so compact that it can run even on older hardware (“potato-level”). Test the model by typing hello and verifying the setup.


2. Use Docker to Host PostgreSQL 🐳

Docker simplifies setting up your local database. PostgreSQL will be used to store memory and vectorized documents.

Steps:

  • Download Docker Desktop from Docker.com.
  • Run the following command in your terminal:

“`bash
docker run –name pgvector-container -e POSTGRESUSER=myuser -e POSTGRESPASSWORD=mypassword -e POSTGRES_DB=mydatabase -p 5432:5432 -d ankane/pgvector

**Explanation:**  
- Replace `myuser`, `mypassword`, and `mydatabase` with your preferred credentials.  
- Check Docker Desktop to ensure the database is running.  

---

### 3. Install n8n for Workflow Automation  
n8n is an intuitive tool for automating workflows and implementing your AI agent.  

**Steps:**  
- Install Node.js from [Node.js.org](https://nodejs.org).  
- Open your terminal and run:  

bash
npx n8n start
“`

💡 Pro Tip: Save this URL as it’s your go-to for configuring workflows.


🤖 Creating Your AI Agent

1. Designing Your Workflow

On the n8n dashboard, start by creating a workflow named “My AI Agent.” Add triggers and nodes:

  • Trigger Node: Determines what starts the workflow (e.g., incoming chat messages).
  • Action Node: Handles the AI logic. Add an AI agent node under advanced options.
  • System Message: Define rules like “You are a helpful assistant. Respond naturally.”

2. Adding Memory to the Agent 🧠

Memory ensures conversation continuity. While simple memory nodes work initially, they erase conversations upon server restarts. Use PostgreSQL for persistent storage:

  • Add the Postgres Chat Memory node to your agent.
  • Enter the credentials from the earlier Docker setup (e.g., mydatabase, myuser, mypassword).

💡 Fun Test: Ask the agent, “What’s my name?” after restarting the server—PostgreSQL ensures responses remain accurate.


3. Assign Tools and Functions

Enhance your AI agent’s utility by integrating tools:

  • Vector Store for Custom Knowledge Base: Use PostgreSQL’s vector extension to retrieve relevant document sections. Add the PG Vector Store node and embed documents using the Nomic Embed model.
  • Web Search: Include the SERP API to enable Google searches. Register on SERP API, obtain your API key, and link it to n8n.

Example Query: “What is the weather in New York?”


🏗️ Uploading Your Custom Documents

Your AI agent can answer questions based on uploaded PDFs, CSVs, or text files.

Steps:

  1. Create a new workflow labeled “Add Documents.”
  2. Add a form submission trigger with a file upload field.
  3. Use Postgres PG Vector Store to insert documents.
  4. Vectorize your data using Ollama’s Nomic Embed model and split it into meaningful chunks (recursive text splitter).

💡 Practical Tip: Keep table names consistent in all workflows (e.g., n8n_tutorial).


🛠️ Troubleshooting Common Issues

1. Ollama Node not Connecting?

  • Replace localhost with 127.0.0.1 in the node credentials.

2. Vector Store Error?

  • Swap Ollama node with OpenAI node, and redirect the base URL to Ollama’s endpoint (localhost/v1).

3. Lost Memory on Restart?

  • Reconfirm PostgreSQL credentials and ensure Docker is running.

🌟 Expanding Your Agent’s Capabilities

Beyond Basics 📈

Once your AI agent is functional, explore additional features like:

  • Email Integration (via Gmail): Automate responses or queries.
  • Custom APIs: Connect to CRM systems or task management tools.

Advanced Tools 💼

Consider adding vision, voice, or real-time analytics in future iterations.


🧰 Resource Toolbox

  1. Ollama Installation & Models
    Run AI models locally with Ollama’s installer.
  2. Docker Desktop
    Host PostgreSQL seamlessly via containers.
  3. Node.js
    Essential runtime for n8n setup.
  4. SERP API
    Fetch live website data for real-time queries.
  5. n8n Documentation
    Extensive resource for mastering workflow automation.

🔑 How This Transforms Your Workflow

By following these steps, you gain a powerful local AI agent capable of:

  • Searching Custom Documents 📂
  • Retrieving Web Data 🌐
  • Maintaining Context Across Chats 💬

With no reliance on external services, you’ve built a robust AI tool that respects privacy, saves costs, and scales effortlessly.

💡 Final Thought: Make your AI agent unique—add custom rules, tools, and styling. Whether for personal productivity or professional use, the possibilities are endless. 🌟

Other videos of

Play Video
Leon van Zyl
0:18:39
60
7
1
Last update : 12/04/2025
Play Video
Leon van Zyl
0:31:56
130
15
0
Last update : 02/04/2025
Play Video
Leon van Zyl
0:16:46
105
9
2
Last update : 01/04/2025
Play Video
Leon van Zyl
0:14:26
56
4
0
Last update : 31/03/2025
Play Video
Leon van Zyl
0:12:18
80
12
2
Last update : 31/03/2025
Play Video
Leon van Zyl
0:15:05
160
17
0
Last update : 29/03/2025
Play Video
Leon van Zyl
0:04:47
142
13
2
Last update : 29/03/2025
Play Video
Leon van Zyl
0:12:37
87
9
1
Last update : 27/03/2025
Play Video
Leon van Zyl
0:08:26
68
5
1
Last update : 26/03/2025