root27/pg-mcp
If you are the rightful owner of pg-mcp and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.
pg-mcp is a PostgreSQL Model Context Protocol server built in Go, providing a safe interface for querying PostgreSQL databases with read-only access.
pg-mcp
A PostgreSQL MCP (Model Context Protocol) server built in Go.
This server provides a safe interface for querying PostgreSQL databases with read-only access.
It exposes MCP tools for:
- Listing tables
- Describing tables
- Executing safe
SELECTorWITHqueries
Write operations (INSERT, UPDATE, DELETE, DROP, etc.) are blocked by default.
Features
- Safe query execution (only
SELECTandWITHqueries allowed) - Protection against destructive SQL (DROP, DELETE, TRUNCATE, ALTER, etc.)
- Schema discovery when queries fail
- Two transport modes:
- stdio (default) for CLI/agent integration
- http for HTTP-based usage
- Docker-ready
Installation
Clone and Build
git clone https://github.com/root27/pg-mcp.git
cd pg-mcp
go build -o pg-mcp
Configuration
Database connection is configured using environment variables:
| Variable | Default | Description |
|---|---|---|
DB_HOST | localhost | Database host |
DB_PORT | 5432 | Database port |
DB_USER | postgres | Database user |
DB_PASSWORD | password | Database password |
DB_NAME | mydb | Database name |
DB_SSLMODE | disable | SSL mode (e.g. require) |
Example:
export DB_HOST=localhost
export DB_PORT=5432
export DB_USER=postgres
export DB_PASSWORD=secret
export DB_NAME=mydb
export DB_SSLMODE=disable
Running the server
Run (stdio transport)
This is the default mode. It runs over stdio, which is useful for direct MCP integrations (e.g., when an agent launches the server as a subprocess).
./pg-mcp
Run (http transport)
You can also run the server over HTTP, which is useful if you want to expose it as a service.
./pg-mcp -t http
# or
./pg-mcp --transport http
The server will listen on
http://localhost:8080/mcp
Docker
You can pull the images for arm64 and amd64
Arm64
docker pull ghcr.io/root27/pg-mcp:arm64
Run Container
docker run -it \
-e DB_HOST=host.docker.internal \
-e DB_PORT=5432 \
-e DB_USER=postgres \
-e DB_PASSWORD=secret \
-e DB_NAME=mydb \
-e DB_SSLMODE=disable \
-p 8080:8080 \
ghcr.io/root27/pg-mcp:arm64 --transport http
Amd64
docker pull ghcr.io/root27/pg-mcp:amd64
Run Container
docker run -it \
-e DB_HOST=host.docker.internal \
-e DB_PORT=5432 \
-e DB_USER=postgres \
-e DB_PASSWORD=secret \
-e DB_NAME=mydb \
-e DB_SSLMODE=disable \
-p 8080:8080 \
ghcr.io/root27/pg-mcp:amd64 --transport http
The server is accessible at http://localhost:8080/mcp