010: Securely access data through MCP PostgreSQL

Written by
Silas Grey
Updated on:July-09th-2025
Recommendation

Explore how to securely access the PostgreSQL database through MCP to achieve read-only query and data protection.

Core content:
1. Project overview: Implement read-only access to PostgreSQL
2. Core functions: Ensure data security and support read-only query for large language models
3. Configuration and usage: Including detailed descriptions of the two configuration methods of Docker and NPX

Yang Fangxian
Founder of 53AI/Most Valuable Expert of Tencent Cloud (TVP)

 

Project Introduction

Provides read-only access to a PostgreSQL database. The server allows Large Language Models (LLMs) to inspect the database schema and perform read-only queries.

Core Features

  • • Provides read-only access to a PostgreSQL database
  • • Allow LLM (Large Language Model) to view database schema and perform read-only queries
  • • Ensure data security (read operations only)

Configuration usage

Two configuration methods are provided:

The Docker way

{
  "mcpServers": {
    "postgres": {
      "command": "docker",
      "args": [
        "run", 
        "-i", 
        "--rm", 
        "mcp/postgres", 
        "postgresql://host.docker.internal:5432/mydb"
      ]
    }
  }
}

Special Notes:

  • • When running Docker on MacOS, if the server is running on the host network, use host.docker.internal
  • • You can add username and password to the PostgreSQL URL: postgresql://user:password@host:port/db-name

NPX method

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-postgres",
        "postgresql://localhost/mydb"
      ]
    }
  }
}

Build Instructions

Docker build commands are provided:

docker build -t mcp/postgres -f src/postgres/Dockerfile .