AIpostgresmcp serverdatabaseai agentscontext engineeringsql

Postgres MCP Server: Query Your Database in Plain English

9 min read

A Postgres MCP server lets AI agents query your database using natural language — no SQL required. Here's how it works, what to watch out for, and how Gyld adds business context on top.

Most developers have lost at least one afternoon to a query they couldn't quite remember how to write. A Postgres MCP server fixes that — it lets any MCP-compatible AI agent (Claude, ChatGPT, Cursor, Codex) read your database schema, translate a plain-English question into SQL, and return the results, all without you touching a query editor.

That's the promise. The reality is more nuanced: read-only access, permission boundaries, and how you layer in broader business context all determine whether this is genuinely useful or just a novelty.

What is a Postgres MCP server?

A Postgres MCP server is a process that implements Anthropic's Model Context Protocol — a standardized interface for connecting AI agents to external data sources — and exposes a PostgreSQL database as a set of tools an AI can call. The agent inspects the schema, formulates a SQL query from your natural-language prompt, executes it through the server, and returns structured results. No custom integration code required on the AI side; the protocol handles the handshake.

Think of MCP as a USB-C standard for AI: just as USB-C lets any device connect to any port, MCP lets any compliant AI agent connect to any MCP server — including one sitting in front of your Postgres instance.

How does a Postgres MCP server actually work?

The flow has four steps:

  1. Schema discovery. On connection, the server reads your database schema — table names, column names, types, foreign keys — and surfaces that as context to the AI.
  2. Natural language → SQL. The AI receives your prompt ("How many orders did we close last month?") alongside the schema and generates a SQL query.
  3. Execution. The MCP server runs the query against Postgres and returns the result set.
  4. Response. The AI formats the results into a human-readable answer.

Punit's walkthrough of setting this up in Cursor is a good concrete reference: connect a Postgres MCP server to your IDE, type a plain-English question, and watch the agent write and run the query for you. The setup is straightforward enough that developers who only occasionally need ad-hoc queries — not full-time database specialists — get real value from it.

On the implementation side, popular open-source options include postgres-mcp by Crystal DBA, which has accumulated over 3,000 GitHub stars and 327 forks, and the mcp-postgres-server npm package, which wires up via environment variables (PG_HOST, PG_USER, PG_PASSWORD, PG_DATABASE) and exposes a query tool for SELECT statements.

The read-only question: why it matters more than you think

The most important configuration decision for a Postgres MCP server is whether the AI can write to your database or only read from it.

For most business use cases, read-only is the right default — and enforcing it at the database user level (not just in the server config) is the safest approach. Grant the MCP server's Postgres user SELECT privileges only, on the specific schemas it needs. That way, even if the server is misconfigured or compromised, it cannot INSERT, UPDATE, or DELETE.

This matters because the attack surface is real. Datadog Security Labs published a case study showing a SQL injection vulnerability in Anthropic's own reference Postgres MCP server (@modelcontextprotocol/server-postgres) that allowed an attacker to bypass the read-only restriction and execute arbitrary SQL. Anthropic deprecated and archived that package on July 10, 2025 — but as of the Datadog report, the deprecated npm package was still seeing 21,000 weekly downloads. If you're running that package, stop.

The lesson: read-only enforcement in the MCP server layer is not sufficient on its own. Enforce it at the Postgres role level too.

Permissions and schema scoping

Beyond read vs. write, you want to scope what the AI can see:

  • Schema-level access. Grant the MCP user access only to the schemas relevant to the use case. A customer-support agent doesn't need visibility into your raw payments tables.
  • Row-level security. Postgres's native RLS policies apply even to queries run through an MCP server, as long as you connect as the right user. Use this to enforce data boundaries without rebuilding them in the MCP layer.
  • Sensitive columns. Consider views that exclude PII or financial columns, and point the MCP server at the view rather than the base table.

This is the same principle that makes Gyld's permissioned knowledge model work across other data sources: the question isn't just "can AI access this data?" but "which AI, for which users, seeing which subset?"

Natural language to SQL: what works well and what doesn't

For straightforward analytical queries — counts, aggregations, date-range filters, joins across a few tables — modern LLMs are remarkably good at generating correct SQL from plain English, especially once they have the schema. A prompt like "show me all users who signed up in the last 30 days and haven't placed an order" reliably produces the right query against a clean schema.

It breaks down when:

  • The schema is ambiguous. Column names like status or type without clear values force the AI to guess.
  • Business logic is implicit. If "active customer" means accounts with a subscription row where cancelled_at IS NULL AND trial_ends_at < NOW(), the AI won't know that unless you tell it.
  • The query needs company context. "Show me deals in the pipeline for Q3" requires knowing what your Q3 date range is, what stage names mean "pipeline," and possibly data from your CRM — not just your database schema.

That third failure mode is where a standalone Postgres MCP server hits its ceiling.

Where Gyld fits: Postgres as one layer of company context

A Postgres MCP server gives AI agents access to your structured data. But most real business questions span multiple systems: a question about Q3 pipeline might need CRM data from HubSpot, deal notes from Slack, and financial targets from a spreadsheet in Google Drive — alongside whatever's in your database.

Gyld's approach is to treat company context as a unified layer rather than a collection of disconnected MCP servers. Gyld ingests data from the apps your team already uses — including databases alongside Slack, Notion, HubSpot, Google Drive, Gmail, and more — into a per-company knowledge base, then exposes that as MCP servers that any AI agent can plug into. The business logic and relationships live in the knowledge layer, not scattered across individual tool configs.

The difference from running your own Postgres MCP server:

Self-hosted Postgres MCPGyld company context layer
Data sourcesPostgres onlyPostgres + Slack, Notion, HubSpot, Drive, Gmail, and more
Business logicMust be in schema/viewsCaptured across sources
PermissionsDB-level rolesPer-user, per-team, company-wide
MaintenanceYou manage the serverManaged, stays current
SetupDeveloper taskNo-code
Source citationsNoYes — every answer cites its source

For a team that wants AI to answer questions about their business — not just their database — a Postgres MCP server is one input, not the full solution. You can read more about how this compares to RAG and other grounding approaches.

How to set up a Postgres MCP server: the practical steps

If you want to run a Postgres MCP server directly, here's the minimal path:

  1. Create a read-only Postgres user. CREATE USER mcp_reader WITH PASSWORD '...'; GRANT CONNECT ON DATABASE yourdb TO mcp_reader; GRANT USAGE ON SCHEMA public TO mcp_reader; GRANT SELECT ON ALL TABLES IN SCHEMA public TO mcp_reader;
  2. Choose a server implementation. postgres-mcp by Crystal DBA is actively maintained and has configurable access controls. Avoid the deprecated @modelcontextprotocol/server-postgres.
  3. Configure via environment variables. Point the server at your Postgres host, port, database, and the read-only user credentials.
  4. Connect your AI agent. Add the MCP server to your Claude, Cursor, or ChatGPT config. The agent will discover the schema on first connection.
  5. Scope what's exposed. Use views or schema grants to limit what the AI can see before you connect it to anything sensitive.

For teams who want this working across their full stack — not just one database — Gyld handles the indexing, permissioning, and MCP exposure, so you're not maintaining server configs per data source.

Key takeaways

  • A Postgres MCP server translates plain-English prompts into SQL and returns results — useful for ad-hoc queries, analytics, and AI agents that need structured data.
  • Read-only enforcement must happen at the Postgres role level, not just in the MCP server config. The SQL injection vulnerability in Anthropic's deprecated reference server is a concrete reminder of why.
  • For questions that span your database and other business systems, a unified context layer handles what a standalone MCP server cannot.

If you want your AI tools to understand your full business — database included — start building your company brain with Gyld.

Frequently asked questions

What is a Postgres MCP server?

A Postgres MCP server is a process that implements the Model Context Protocol to expose a PostgreSQL database to AI agents. It lets agents inspect your schema, generate SQL from natural-language prompts, execute queries, and return results — without the AI needing a direct database connection or custom integration code.

Is it safe to connect an AI agent to my Postgres database?

It can be, with the right controls. The most important safeguard is enforcing read-only access at the Postgres role level — not just in the MCP server configuration. Datadog Security Labs found a SQL injection vulnerability in Anthropic's reference server that bypassed its read-only restriction; that package has since been deprecated. Use a maintained fork, restrict schema access, and consider row-level security for sensitive data.

Can a Postgres MCP server understand business logic, not just schema?

Only what's encoded in the schema itself — table names, column names, types, and relationships. If "active customer" or "Q3 pipeline" means something specific to your business, the AI won't know unless that logic is captured in views, comments, or a broader context layer that includes your other business data.

Which Postgres MCP server implementation should I use?

As of mid-2025, Anthropic's reference @modelcontextprotocol/server-postgres is deprecated and has a known SQL injection vulnerability. postgres-mcp by Crystal DBA (3,000+ GitHub stars) is actively maintained and supports configurable access controls. Always use a read-only Postgres user regardless of which server you choose.

How is Gyld different from running a Postgres MCP server myself?

A self-hosted Postgres MCP server exposes one database to one AI agent. Gyld indexes your database alongside the other tools your team uses — Slack, Notion, HubSpot, Google Drive, Gmail, and more — into a single permissioned knowledge base, then exposes it as MCP servers any AI agent can plug into. You get cross-source answers with source citations, without maintaining server configs per data source.

Curtis Rosenvall

Give your AI your company's brain.

Connect your tools into one company brain your AI — and your whole team — can actually use.