pg-mcp
pg-mcp is a PostgreSQL MCP server that enables AI assistants to securely interact with PostgreSQL databases, with built-in read-only protection by default.
pg_connect: Connect to a PostgreSQL database via a connection URL or individual parameters (host, port, user, password, database), with optional SSL and read-only mode settings.pg_disconnect: Close and clean up an existing database connection by its connection ID.pg_query: Execute SQL queries against a connected database, with support for parameterized/prepared statements.pg_list_schemas: List all schemas available in the connected database.pg_get_ddl: Retrieve the complete DDL for a database (CREATE TABLE statements, indexes, constraints, foreign keys, sequences, and views), optionally filtered by schema.Read-only mode: Enabled by default, blocking DML (INSERT, UPDATE, DELETE, TRUNCATE, MERGE) and DDL/DCL operations (CREATE, ALTER, DROP, GRANT, REVOKE). Can be explicitly disabled per connection.
Auto-connect: Automatically connects on startup if
DATABASE_URLor standard libpq environment variables are set, using connection ID"default".
Enables querying of PostgreSQL databases, schema inspection, and retrieval of DDL with built-in read-only protection. It provides tools for connecting to databases, executing SQL queries, listing schemas, and fetching complete DDL structures.
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@pg-mcplist all tables in the public schema"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
pg-mcp
MCP server for PostgreSQL. Query databases, inspect schemas, backup data, and manage dumps — with built-in read-only protection.
Quick Start
npm install -g @shedyhs/pg-mcpAdd to your AI provider config:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["-y", "@shedyhs/pg-mcp"],
"env": {
"DATABASE_URL": "postgres://user:pass@localhost:5432/mydb"
}
}
}
}With DATABASE_URL or PGHOST/PGDATABASE set, the server auto-connects as "default" on startup.
Set PG_MCP_READ_ONLY=false in env to allow write operations (read-only by default).
Tools
Tool | Description | Requires |
| Connect to a PostgreSQL database (URL, params, or libpq env vars) | — |
| Disconnect from a database | — |
| Execute SQL queries with read-only protection | — |
| List all user schemas | — |
| Get complete DDL (tables, indexes, constraints, FKs, sequences, enums, views) | — |
| Backup specific rows as INSERT statements before destructive operations | — |
| Dump a database or specific tables to a file |
|
| Restore a database from a dump file (custom, directory, or tar format) |
|
Where to Put the Config
Provider | Config file |
Claude Desktop |
|
Claude Code |
|
Cursor |
|
Windsurf |
|
Codex |
|
All providers above use the same JSON format from Quick Start.
GitHub Copilot (VS Code) uses a slightly different format in .vscode/mcp.json:
{
"servers": {
"postgres": {
"type": "stdio",
"command": "npx",
"args": ["-y", "@shedyhs/pg-mcp"],
"env": {
"DATABASE_URL": "postgres://user:pass@localhost:5432/mydb"
}
}
}
}From source — replace command/args with "command": "node", "args": ["/path/to/pg-mcp/dist/index.js"].
Installing pg_dump & pg_restore
Only needed if you use pg_dump or pg_restore. All other tools work without external dependencies.
OS | Command |
macOS |
|
Debian/Ubuntu |
|
RHEL/Fedora |
|
Windows |
|
Verify with pg_dump --version.
Installation from Source
git clone https://github.com/shedyhs/pg-mcp
cd pg-mcp
npm install && npm run buildLicense
MIT
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