PostgreSQL

Let the agent inspect schemas, slow queries and database health, with a read-only role and read-only transactions.

Postgres MCP Pro gives the agent schema browsing, EXPLAIN, the slowest queries from pg_stat_statements, index advice and health checks (connections, vacuum, replication, buffer cache). In restricted mode every statement runs in a read-only transaction with a time limit.

SettingValue
RunsIn your network, next to the database
Endpoint URLhttps://<your hostname>/mcp
TransportStreamable HTTP, through Supergateway
AuthenticationBearer token (Supergateway's key)

Create a read-only role

Two layers keep it read-only: this role, and the server's restricted mode. Point it at a read replica if you have one.

CREATE ROLE sremcp_ro LOGIN PASSWORD 'choose-a-long-password';
GRANT CONNECT ON DATABASE app TO sremcp_ro;
GRANT pg_read_all_data TO sremcp_ro;   -- PostgreSQL 14+: read every table
GRANT pg_monitor TO sremcp_ro;         -- pg_stat_* views, for performance tools
ALTER ROLE sremcp_ro SET default_transaction_read_only = on;
ALTER ROLE sremcp_ro SET statement_timeout = '30s';

For the slow-query and index tools, enable the pg_stat_statements and hypopg extensions. Managed services (Amazon RDS, Cloud SQL, Azure) usually have both available.

Run the server read-only behind Supergateway

docker run -d --name mcp-postgres -p 8000:8000 \
  -e DATABASE_URI="postgresql://sremcp_ro:<password>@db.internal:5432/app" \
  -e SUPERGATEWAY_API_KEY="$MCP_API_KEY" \
  supercorp/supergateway:uvx \
  --stdio "uvx postgres-mcp --access-mode=restricted" \
  --outputTransport streamableHttp --port 8000

--access-mode=restricted is what makes execute_sql read-only. Never use unrestricted with sremcp.

Then expose it over https. See Expose a self-hosted server.

Onboard in sremcp

FieldValue
Display name / SlugPostgres (prod replica) / postgres-prod
Endpoint URLhttps://mcp-postgres.example.com/mcp
TransportStreamable HTTP
AuthenticationBearer token, Secret = MCP_API_KEY

Approve tools and grant carefully

Approve list_schemas, list_objects, get_object_details, explain_query, get_top_queries, analyze_db_health, and, if you want them, analyze_workload_indexes and analyze_query_indexes.

execute_sql runs any read query the agent writes. It's read-only in restricted mode, but it can read every table the role can. Approve it only if that's acceptable, and grant this server only to the roles that may see that data.

Try it

  • What are the 5 slowest queries by total time, and what would help them?
  • Is anything wrong with database health: connections, vacuum, replication lag?
  • Explain why SELECT … FROM orders WHERE customer_id = … is slow.

On this page