Get the FREE Ultimate OpenClaw Setup Guide →

postgres

MCP server from SwanHtetAungPhyo/postgres-mcp-server

Installation
Run this command in your terminal to add the MCP server to Claude Code.
Run in terminal:
Command
claude mcp add --transport stdio swanhtetaungphyo-postgres-mcp-server ./postgresql-mcp-server

How to use

This MCP server provides a PostgreSQL-specific interface for AI assistants via the MCP (Model Context Protocol). It exposes three core tools that map to common database operations: execute_query for reading data, ddl_query for managing database structures (DDL), and modify_query for inserting, updating, or deleting data. Each tool is designed with safety in mind: query validation blocks dangerous operations, and administrative actions like DROP DATABASE are restricted. Connectors between the MCP client and the PostgreSQL database are secured using standard PostgreSQL connection settings, and input sanitization is applied through the underlying ORM, reducing the risk of SQL injection.

To use the server, start the binary produced for your platform and point MCP clients at it. You can run read-only SELECT queries through execute_query to fetch results, use ddl_query when you need to create or alter tables, and use modify_query to update or remove data. The server enforces validation rules per tool to ensure only allowed SQL statements are executed in the given context, helping keep your data safe while enabling AI-driven database interactions.

How to install

Prerequisites:

  • Go 1.19 or later
  • PostgreSQL 13+ (recommended)
  • A working development environment (bash shell, Git)

Install and run locally:

# Create project directory and initialize module
mkdir postgresql-mcp-server
cd postgresql-mcp-server
go mod init postgresql-mcp-server

Install dependencies:

go get github.com/metoro-io/mcp-golang
go get github.com/metoro-io/mcp-golang/transport/stdio
go get gorm.io/driver/postgres
go get gorm.io/gorm

Prepare and run the server:

# Place your server code in main.go (or as appropriate for your project)
# Update the database connection string in the code:
const dbConn = "host=localhost user=postgres password=yourpassword dbname=yourdb port=5432 sslmode=disable"

# Build the binary
go build -o postgresql-mcp-server

# Run the server
./postgresql-mcp-server

Your MCP server should now be running and ready to accept connections from MCP clients.

Additional notes

Tips and notes:

  • Ensure your PostgreSQL instance allows connections from the host where you run the MCP server and that the user has appropriate permissions for the operations you intend to perform.
  • Consider using SSL connections in production by adjusting sslmode and providing SSL certificates as needed.
  • For security, avoid hardcoding credentials in code; consider using environment variables or a secrets manager and the provided environment-variable patterns shown in the README.
  • If you encounter connection issues, verify the DB connection string, port, and that PostgreSQL is listening on the expected interface.
  • The server's validation prevents dangerous commands (e.g., DROP DATABASE) through the query tools, but you should still design your MCP client flows with a principle of least privilege.

Related MCP Servers

Sponsor this space

Reach thousands of developers