Skip to main content
Glama

Postgres MCP Server

by wongjingping

MCP Postgres Server

A Model Context Protocol server for querying PostgreSQL databases with tools for data retrieval and schema inspection.

Setup

Prerequisites: Python 3.11+ and PostgreSQL

# Install dependencies pip install -r requirements.txt # Configure database connection (defaults shown) export POSTGRES_HOST=localhost export POSTGRES_PORT=5432 export POSTGRES_USER=postgres export POSTGRES_PASSWORD=postgres export POSTGRES_DB=housing

Usage

Development

Run the following command to start the server in development mode and launch the MCP Inspector:

mcp dev main.py

MCP Client Integration

You can also add the following to your MCP client configuration to use the server:

{ "mcpServers": { "postgres": { "command": "python", "args": ["main.py"], "env": { "POSTGRES_HOST": "localhost", "POSTGRES_PORT": "5432", "POSTGRES_USER": "postgres", "POSTGRES_PASSWORD": "postgres", "POSTGRES_DB": "housing" } } } }

See mcp.json for an example of how to add the server in Cursor IDE.

Available Tools

  • query_database - Execute SELECT queries (with SQL injection protection)
  • get_table_schema - Get table column information and constraints
  • list_tables - List all database tables
  • postgres://schema - Access complete database schema (resource)

Comparison with Official MCP Postgres Server

This implementation differs from the official @modelcontextprotocol/server-postgres in several key ways:

FeatureThis Server (Python)Official Server (TypeScript)
SafetyGuards against SQL injectionNo protection
Connection Pooling✅ Built-in (asyncpg)❌ Single connection

Security

  • Only SELECT statements allowed
  • Parameterized queries supported
  • Environment-based configuration (no hardcoded credentials)

Next Steps

  • Implement auth for postgres - currently I'm still relying on env variables which doesn't seem very secure
  • Extend this to run on multiple databases and potentially multiple postgres instances as well
  • Implement table / column pruning
-
security - not tested
F
license - not found
-
quality - not tested

hybrid server

The server is able to function both locally and remotely, depending on the configuration or use case.

A Model Context Protocol server that enables querying PostgreSQL databases with tools for executing SELECT queries, inspecting database schemas, and listing tables through natural language.

  1. Setup
    1. Usage
      1. Available Tools
        1. Comparison with Official MCP Postgres Server
          1. Security
            1. Next Steps

              Related MCP Servers

              • -
                security
                F
                license
                -
                quality
                A Model Context Protocol server providing both read and write access to PostgreSQL databases, enabling LLMs to query data, modify records, and manage database schemas.
                Last updated -
                4
                JavaScript
              • -
                security
                A
                license
                -
                quality
                A Model Context Protocol server that enables powerful PostgreSQL database management capabilities including analysis, schema management, data migration, and monitoring through natural language interactions.
                Last updated -
                355
                46
                TypeScript
                AGPL 3.0
                • Linux
                • Apple
              • -
                security
                F
                license
                -
                quality
                A Model Context Protocol server that enables interaction with PostgreSQL databases to list tables, retrieve schemas, and execute read-only SQL queries.
                Last updated -
                16,948
                JavaScript
                • Linux
                • Apple
              • -
                security
                -
                license
                -
                quality
                A Model Context Protocol server that provides read-only access to PostgreSQL databases, enabling LLMs to inspect database schemas and execute read-only queries.
                Last updated -
                JavaScript

              View all related MCP servers

              MCP directory API

              We provide all the information about MCP servers via our MCP API.

              curl -X GET 'https://glama.ai/api/mcp/v1/servers/wongjingping/postgres-mcp'

              If you have feedback or need assistance with the MCP directory API, please join our Discord server