Goal
Retrieve data from Cloud SQL PostgreSQL using MCP only.
Inputs to collect (ask only if missing)
project_idinstance_iddatabase_namedb_userlocal_proxy_port(default5432)
Execution workflow
- Authenticate in gcloud:
gcloud auth login - Start Cloud SQL Auth Proxy:
gcloud beta sql connect <instance_id> --project=<project_id> --user=<db_user> --database=<database_name>or run Cloud SQL Auth Proxy bound to localhost (127.0.0.1:<local_proxy_port>). - Configure MCP server in
~/.codex/config.tomlwith:command = "npx"and args["-y", "@modelcontextprotocol/server-postgres", "postgresql://<db_user>@127.0.0.1:<local_proxy_port>/<database_name>"] - Use MCP tools/resources for schema discovery and read-only SQL queries.
Guardrails
- Pass
--projectin every command. - Use read-only SQL (
SELECT,WITH,EXPLAIN) only. - Start with schema discovery and constrained queries (
LIMIT, date filters). - If auth or permissions fail, fix environment before retrying.
References
- MCP setup and usage:
references/mcp-cloudsql-postgres.md