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 (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
Using npx (recommended)
npx -y @shedyhs/pg-mcpGlobal install
npm install -g @shedyhs/pg-mcpFrom source
git clone https://github.com/shedyhs/pg-mcp
cd pg-mcp
npm install
npm run buildConfiguration
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 |
| Enable read-only mode for auto-connect |
|
Set PG_MCP_READ_ONLY=false to allow write operations on the default connection.
Usage
Auto-connect (recommended)
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 |
| Connect to a PostgreSQL database |
| Disconnect from a database |
| Execute a SQL query |
| List all schemas in the database |
| Get complete DDL for the database |
| Dump a database using pg_dump |
| Restore a database using pg_restore |
License
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