Skip to main content
Glama

pg-mcp

MCP (Model Context Protocol) server for PostgreSQL databases. Enables Claude and other AI assistants to query databases, inspect schemas, and get DDL - with built-in read-only protection.

Features

  • pg_connect - Connect to PostgreSQL databases (URL or individual params)

  • pg_disconnect - Disconnect from databases

  • pg_query - Execute SQL queries

  • pg_list_schemas - List all schemas

  • pg_get_ddl - Get complete DDL (tables, indexes, constraints, foreign keys, views)

  • Read-only mode - Blocks INSERT, UPDATE, DELETE, and DDL operations (enabled by default)

Installation

npx -y @shedyhs/pg-mcp

Global install

npm install -g @shedyhs/pg-mcp

From source

git clone https://github.com/shedyhs/pg-mcp
cd pg-mcp
npm install
npm run build

Configuration

Claude Desktop

Add to your claude_desktop_config.json:

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": ["-y", "@shedyhs/pg-mcp"]
    }
  }
}

Claude Code

Add to your .claude/settings.json:

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": ["-y", "@shedyhs/pg-mcp"]
    }
  }
}

Or if installed from source, add to ~/.mcp.json:

{
  "mcpServers": {
    "postgres": {
      "command": "node",
      "args": ["/path/to/pg-mcp/dist/index.js"]
    }
  }
}

With environment variables

When env vars are configured, the server auto-connects on startup with connection ID "default" — no need to call pg_connect.

Using DATABASE_URL

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": ["-y", "@shedyhs/pg-mcp"],
      "env": {
        "DATABASE_URL": "postgres://user:pass@localhost:5432/mydb"
      }
    }
  }
}

Using libpq env vars

Standard PostgreSQL environment variables are supported (same ones used by psql, pg_dump, etc.):

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": ["-y", "@shedyhs/pg-mcp"],
      "env": {
        "PGHOST": "localhost",
        "PGPORT": "5432",
        "PGDATABASE": "mydb",
        "PGUSER": "postgres",
        "PGPASSWORD": "secret"
      }
    }
  }
}

pg-mcp specific env vars

Variable

Description

Default

PG_MCP_READ_ONLY

Enable read-only mode for auto-connect

true

Set PG_MCP_READ_ONLY=false to allow write operations on the default connection.

Usage

If DATABASE_URL or PGHOST/PGDATABASE are set, the server auto-connects as "default". Use it directly:

pg_query({ connectionId: "default", sql: "SELECT * FROM users LIMIT 10" })

Manual connect

pg_connect({
  connectionId: "main",
  url: "postgres://user:pass@localhost:5432/mydb"
})

Or with individual parameters:

pg_connect({
  connectionId: "main",
  host: "localhost",
  port: 5432,
  database: "mydb",
  user: "postgres",
  password: "secret"
})

Or relying on libpq env vars:

pg_connect({ connectionId: "main" })

Read-only mode

By default, connections are read-only. This blocks:

  • DML: INSERT, UPDATE, DELETE, TRUNCATE, MERGE

  • DDL: CREATE, ALTER, DROP, RENAME

  • DCL: GRANT, REVOKE

To disable (use with caution):

pg_connect({
  connectionId: "main",
  url: "postgres://...",
  readOnly: false
})

Or via env var for auto-connect: PG_MCP_READ_ONLY=false

Query examples

-- List tables
SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';

-- Query data
SELECT * FROM users LIMIT 10;

-- Get table structure
pg_get_ddl({ connectionId: "main", schema: "public" })

Tools

Tool

Description

pg_connect

Connect to a PostgreSQL database

pg_disconnect

Disconnect from a database

pg_query

Execute a SQL query

pg_list_schemas

List all schemas in the database

pg_get_ddl

Get complete DDL for the database

pg_dump

Dump a database using pg_dump

pg_restore

Restore a database using pg_restore

License

MIT

Install Server
A
license - permissive license
A
quality
C
maintenance

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/shedyhs/pg-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server