Get the FREE Ultimate OpenClaw Setup Guide →

ollama -db

An interactive chat interface that combines Ollama's LLM capabilities with PostgreSQL database access through the Model Context Protocol (MCP).

Installation
Run this command in your terminal to add the MCP server to Claude Code.
Run in terminal:
Command
claude mcp add --transport stdio robdodson-ollama-mcp-db node server.js \
  --env DATABASE_URL="PostgreSQL connection string, e.g. postgresql://user:password@host:5432/dbname" \
  --env OLLAMA_MODEL="qwen2.5-coder:7b-instruct"

How to use

This MCP server provides a natural language interface to a PostgreSQL database using the Model Context Protocol (MCP). It leverages Ollama to translate user questions into SQL queries that run against your database, then returns AI-assisted explanations and the raw results in a readable format. The system is designed to be read-only: SQL queries are restricted to SELECT statements, and credentials are kept secure via your .env file. Start by ensuring your PostgreSQL database is reachable and that Ollama is running with the qwen2.5-coder:7b-instruct model loaded. Once running, you can ask questions like: “Which products generated the most revenue last month?” or “Show the top customers by order value for the last quarter.” The MCP flow handles schema loading, query generation, execution, and natural-language explanation of results.

How to install

Prerequisites:

  • Node.js 16 or higher
  • Access to a running PostgreSQL database
  • Ollama installed and running locally with the model qwen2.5-coder:7b-instruct

Installation steps:

  1. Clone the repository git clone <your-repo-url> cd <your-repo-name>

  2. Install dependencies npm install

  3. Pull the required Ollama model ollama pull qwen2.5-coder:7b-instruct

  4. Create a .env file at the project root with your database connection DATABASE_URL=postgresql://user:password@localhost:5432/dbname OLLAMA_MODEL=qwen2.5-coder:7b-instruct # optional, defaults to this value

  5. Run the server npm start

Notes:

  • Ensure network access from the server to the PostgreSQL instance.
  • The environment variables can be adjusted in the .env file or via your hosting environment.

Additional notes

Tips and common issues:

  • If you see “Failed to connect to database,” verify the DATABASE_URL is correct and that PostgreSQL accepts connections from your host.
  • If Ollama cannot be reached, ensure Ollama is running locally (ollama serve) and the model is installed (ollama list).
  • Since the server operates in read-only mode with SELECT-only queries, ensure your database user has minimal required permissions.
  • You can customize the Ollama model via the OLLAMA_MODEL environment variable to switch to a different model if needed.
  • For production deployments, consider securing the environment with proper secrets management and restricting network access to the MCP endpoint.

Related MCP Servers

Sponsor this space

Reach thousands of developers