Postgres MCP Server

PostgreSQL MCP server - query, schema introspection, explain, and health checks for AI assistants

Local serverstdioGo

What is the Postgres MCP server?

Postgres MCP server is a locally run integration for AI assistants that speak the Model Context Protocol. PostgreSQL MCP server - query, schema introspection, explain, and health checks for AI assistants.

What you get

One click adds this to your local Yaw MCP config so it's available in every Yaw Terminal session. Or install manually below.

  • Role-based access as the primary control — - the recommended posture is to use a least-privileged postgres role in DATABASE_URL (e.g. one with GRANT pg_read_all_data); postgres itself then enforces the boundary, no env var needed. See Configuring access
  • Parameterized queries — - pg_query takes a params array for $1, $2, etc. No string-interpolated SQL in our code path
  • Written from scratch, actively maintained — - not a fork of the deprecated code. Unit + integration tests (npm test, npm run test:integration) run against a real Postgres; releases cut via release.sh
  • Schema introspection built in — - pg_list_schemas, pg_list_tables, pg_describe_table return columns, primary keys, foreign keys, and indexes without the agent having to remember pg_catalog joins
  • EXPLAIN as a first-class tool — - text or JSON format, with optional ANALYZE. ANALYZE for non-SELECT statements requires ALLOW_WRITES=1 and always rolls back, so the plan is real but the write doesn't persist
  • Perf diagnostics the deprecated server never had — - pg_top_queries (from pg_stat_statements), pg_seq_scan_tables, pg_unused_indexes, pg_table_bloat, pg_inspect_locks, pg_replication_status. Answer "why is this slow?" in one tool call

What the assistant can call

Once Postgres is connected, these are the calls the assistant has available:

  • pg_readonly — Run a SQL statement guaranteed read-only - always inside BEGIN READ ONLY, regardless of ALLOW_WRITES. The recommended tool for read access; safe for
  • pg_query — Run a SQL query. Writes gated by the role in DATABASE_URL first, ALLOW_WRITES second. Supports parameterized queries via params. Result fields
  • pg_list_schemas — List non-system schemas
  • pg_list_tables — List tables (and optionally views) in a schema with estimated row counts. Paginated via limit/offset
  • pg_describe_table — Kind, columns, PK, outgoing FKs, incoming FKs (referenced_by), CHECK / UNIQUE / EXCLUDE constraints, indexes, and partition parent/children for a
  • pg_list_views — List views and materialized views in a schema, including their SQL definitions
  • pg_list_functions — List functions, procedures, and aggregates in a schema with signatures and return types
  • pg_list_extensions — List installed extensions (pgvector, postgis, pg_stat_statements, etc.) with versions
  • pg_search_columns — Find columns by name pattern across all user schemas. Case-insensitive, supports SQL LIKE wildcards
  • pg_explain — EXPLAIN or EXPLAIN ANALYZE for a SQL statement. Text or JSON output. Optional hypothetical_indexes (requires the
  • pg_health — Server version, database size, connection count, active queries, table count
  • pg_top_queries — Top N queries by total/mean execution time. Requires the pg_stat_statements extension

Setting it up

Setup follows the usual MCP pattern — install or clone the server, register it in your client's configuration file, restart the client. The configuration blocks on this page cover the common clients.

Configuration and credentials

You will need 3 environment variables: DATABASE_URL, ALLOW_WRITES, POSTGRES_SSL_REJECT_UNAUTHORIZED. The server will not start without them, which is usually why the tools fail to appear on a first run. Keep credentials in your client's env block or a secrets manager rather than in a file you might commit.

Choosing this one

Plenty of database access servers cover similar ground. The differences that matter in practice are scope of access and how much setup stands between you and a working tool call. Postgres's toolset — pg_readonly, pg_query, pg_list_schemas and 11 more — is a fair guide to whether it matches your workflow. It is maintained by jeffyaw; worth a glance at recent repository activity before you build anything load-bearing on it.

We check each listing at SyncDev against the project's documentation before it goes live — if something here drifts out of date, it is a bug worth reporting.

Before you rely on it

  • It runs with your machine's permissions. That is convenient and also the reason to think about what you point it at before you approve a tool call.
  • With 14 tools registered it takes up a noticeable share of the context window; turn it off in projects that never touch Postgres.
  • Missing credentials fail quietly in some clients — if no tools show up, check the environment block first.
  • MCP clients confirm each tool call by default. Leave that on until you have watched what the postgres mcp server does with a few real requests.

Available tools

ToolWhat it does
pg_readonlyRun a SQL statement guaranteed read-only - always inside BEGIN READ ONLY, regardless of ALLOW_WRITES. The recommended tool for read access; safe for hosts to auto-allow.
pg_queryRun a SQL query. Writes gated by the role in DATABASE_URL first, ALLOW_WRITES second. Supports parameterized queries via params. Result fields include dataTypeName (e.g. int4, jsonb) alongside dataTypeID.
pg_list_schemasList non-system schemas.
pg_list_tablesList tables (and optionally views) in a schema with estimated row counts. Paginated via limit/offset.
pg_describe_tableKind, columns, PK, outgoing FKs, incoming FKs (referenced_by), CHECK / UNIQUE / EXCLUDE constraints, indexes, and partition parent/children for a relation.
pg_list_viewsList views and materialized views in a schema, including their SQL definitions.
pg_list_functionsList functions, procedures, and aggregates in a schema with signatures and return types.
pg_list_extensionsList installed extensions (pgvector, postgis, pg_stat_statements, etc.) with versions.
pg_search_columnsFind columns by name pattern across all user schemas. Case-insensitive, supports SQL LIKE wildcards.
pg_explainEXPLAIN or EXPLAIN ANALYZE for a SQL statement. Text or JSON output. Optional hypothetical_indexes (requires the [HypoPG](https://github.com/HypoPG/hypopg) extension) lets you ask "what would the plan be with these index
pg_healthServer version, database size, connection count, active queries, table count.
pg_top_queriesTop N queries by total/mean execution time. Requires the pg_stat_statements extension.
pg_seq_scan_tablesTables with heavy sequential scans - missing-index candidates.
pg_unused_indexesNon-unique, non-primary indexes with low scan counts - drop candidates.

How to install the Postgres MCP server

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": ["-y", "@yawlabs/postgres-mcp@latest"],
      "env": {
        "DATABASE_URL": "postgres://user:password@host:5432/dbname"
      }
    }
  }
}

Configuration as documented by the project. Restart the client after saving.

Configuration

VariableDescriptionRequired
DATABASE_URLEndpoint or connection string the server talks to.Yes
ALLOW_WRITESConfiguration value read at startup.Optional
POSTGRES_SSL_REJECT_UNAUTHORIZEDConfiguration value read at startup.Optional

Example prompts to try

  • Use Postgres to pg readonly.
  • Use Postgres to pg query.
  • Use Postgres to pg list schemas.

Frequently asked questions

It connects Postgres to MCP-compatible AI assistants such as Claude and Cursor, exposing 14 tools (pg_readonly, pg_query, pg_list_schemas, and more) that the assistant can call on your behalf. Instead of copying data back and forth by hand, the assistant works with Postgres directly.