postgres
MCP server from SwanHtetAungPhyo/postgres-mcp-server
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
trpc-agent-go
trpc-agent-go is a powerful Go framework for building intelligent agent systems using large language models (LLMs) and tools.
station
Station is our open-source runtime that lets teams deploy agents on their own infrastructure with full control.
tiger-cli
Tiger CLI is the command-line interface for Tiger Cloud. It includes an MCP server for helping coding agents write production-level Postgres code.
gopls
MCP server for golang projects development: Expand AI Code Agent ability boundary to have a semantic understanding and determinisic information for golang projects.
kubernetes
A Model Context Protocol (MCP) server for the Kubernetes API.
gcp-cost
💰 An MCP server that enables AI assistants to estimate Google Cloud costs, powered by Cloud Billing Catalog API and built with Genkit for Go