Skip to main content
Glama

MCP Server for PostgreSQL

by asadudin

MCP Server for PostgreSQL

A Model Context Protocol (MCP) server implementation for PostgreSQL, providing a simple interface to interact with PostgreSQL databases through MCP.

Features

  • Execute SQL queries with parameterized inputs
  • Run INSERT/UPDATE/DELETE operations
  • Create new databases
  • Create or update table schemas
  • Debug PostgreSQL connections
  • Containerized with Docker for easy deployment
  • Environment-based configuration

Prerequisites

  • Python 3.8+
  • PostgreSQL 10+
  • Docker (optional, for containerized deployment)
  • Docker Compose (optional, for development)

Installation

  1. Clone the repository:
    git clone https://github.com/asadudin/mcp-server-postgres.git cd mcp-server-postgres
  2. Copy the example environment file:
    cp .env.example .env
  3. Update the .env file with your PostgreSQL credentials:
    PG_HOST=postgres PG_PORT=5432 PG_USER=postgres PG_PASSWORD=your_password PG_DATABASE=your_database HOST=0.0.0.0 PORT=8056
  4. Start the service using Docker Compose:
    docker-compose up -d

Manual Installation

  1. Clone the repository:
    git clone https://github.com/asadudin/mcp-server-postgres.git cd mcp-server-postgres
  2. Create and activate a virtual environment:
    python -m venv venv source venv/bin/activate # On Windows: .\venv\Scripts\activate
  3. Install dependencies:
    pip install -r requirements.txt
  4. Copy the example environment file and update it:
    cp .env.example .env # Edit .env with your configuration
  5. Run the server:
    python mcp_server_postgres.py

Usage

The MCP server provides the following endpoints:

sql_query

Run a SELECT query and return results as JSON.

Parameters:

  • query: SQL query string
  • params: Optional JSON string of query parameters (list or dict)

Example:

{ "query": "SELECT * FROM users WHERE id = $1", "params": [1] }

sql_execute

Execute an INSERT/UPDATE/DELETE statement.

Parameters:

  • query: SQL statement
  • params: Optional JSON string of query parameters (list or dict)

Example:

{ "query": "INSERT INTO users (name, email) VALUES ($1, $2)", "params": ["John Doe", "john@example.com"] }

create_database

Create a new PostgreSQL database.

Parameters:

  • database_name: Name of the database to create

create_or_update_table

Create or update a table schema.

Parameters:

  • sql: CREATE TABLE or ALTER TABLE SQL statement

debug_postgres_connection

Debug the PostgreSQL connection.

Environment Variables

VariableDefaultDescription
PG_HOSTlocalhostPostgreSQL host
PG_PORT5432PostgreSQL port
PG_USERpostgresPostgreSQL username
PG_PASSWORDPostgreSQL password
PG_DATABASEpostgresDefault database name
HOST0.0.0.0Host to bind the MCP server to
PORT8056Port to run the MCP server on

Development

Running Tests

# Install test dependencies pip install -r requirements-dev.txt # Run tests pytest

Building the Docker Image

docker build -t mcp-server-postgres .

API Documentation

For detailed API documentation, refer to the OpenAPI specification.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • FastMCP - The MCP server framework
  • asyncpg - PostgreSQL client for Python
  • FastAPI - Modern, fast web framework for building APIs
-
security - not tested
F
license - not found
-
quality - not tested

A Model Context Protocol server implementation that provides a simple interface to interact with PostgreSQL databases, enabling SQL queries, database operations, and schema management through MCP.

  1. Features
    1. Prerequisites
      1. Installation
        1. Using Docker (Recommended)
        2. Manual Installation
      2. Usage
        1. sql_query
        2. sql_execute
        3. create_database
        4. create_or_update_table
        5. debug_postgres_connection
      3. Environment Variables
        1. Development
          1. Running Tests
          2. Building the Docker Image
        2. API Documentation
          1. Contributing
            1. License
              1. Acknowledgments

                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
                  -
                  license
                  -
                  quality
                  A Model Context Protocol server that provides tools for connecting to and interacting with various database systems (SQLite, PostgreSQL, MySQL/MariaDB, SQL Server) through a unified interface.
                  Last updated -
                  Python
                • -
                  security
                  F
                  license
                  -
                  quality
                  A Model Context Protocol server that enables performing PostgreSQL database operations (create, read, update, delete) on User and Post entities through MCP tools.
                  Last updated -
                  TypeScript

                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/asadudin/mcp-server-postgres'

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