MCP PostgreSQL Server
A Model Context Protocol (MCP) server that provides PostgreSQL database access and operations.
Power your agent
Lệnh của chính nhà phát hành. Không phải endpoint của PlugMCP.
Host a copy with our keys and logs.
npx -y [email protected]Claude Code không có liên kết cài đặt cho lệnh cục bộ. Sao chép lệnh bên dưới và chạy trong terminal.
claude mcp add mcp-postgres -- npx -y mcp-postgres@1.4.2Chạy trong terminal.
{ "mcpServers": { "mcp-postgres": { "command": "npx", "args": [ "-y", "[email protected]" ] } }}claude_desktop_config.json
URL từ xa hay một lệnh trên máy của bạn? Hai cách cài đặt khác nhau thế nào
Chạy trên PlugMCP
A new server you create. It is not this listing's official endpoint. Use the publisher install when you want their URL and their credentials.
MởYou can use this MCP server with any MCP-compatible client by installing it via npm:
npm install -g mcp-postgres
Or run it directly with npx:
npx mcp-postgres@latest
Requires Node.js 22 or newer.
Add this to your MCP client configuration (e.g., .kiro/settings/mcp.json):
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["mcp-postgres@latest"],
"env": {
"DB_HOST": "localhost",
"DB_PORT": "5432",
"DB_USER": "postgres",
"DB_PASSWORD": "your_password",
"DB_NAME": "your_database",
"DB_SSL_MODE": "require"
},
"disabled": false,
"autoApprove": ["list_tables", "get_schema"]
}
}
}
Alternative using DATABASE_URL:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["mcp-postgres@latest"],
"env": {
"DATABASE_URL": "postgresql://username:password@localhost:5432/database_name?sslmode=require"
},
"disabled": false,
"autoApprove": ["list_tables", "get_schema"]
}
}
}
The server supports multiple configuration methods:
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=your_password
DB_NAME=your_database
DB_SSL_MODE=require # Optional: require, disable, or omit for default
DB_READ_ONLY=true # Optional: disables write/DDL tools and makes the DB session read-only
DB_STATEMENT_TIMEOUT=30000 # Optional: query timeout in ms (default: 30000)
Alternative PostgreSQL-style variable names are also supported:
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=your_password
POSTGRES_DB=your_database
POSTGRES_SSL_MODE=require # Optional: require, disable, or omit for default
DATABASE_URL=postgresql://username:password@localhost:5432/database_name?sslmode=require
Create a config.json file in your working directory:
{
"db": {
"host": "localhost",
"port": 5432,
"user": "postgres",
"password": "your_password",
"database": "your_database",
"sslmode": "require"
}
}
The server supports SSL connections with the following modes:
require - Forces SSL connection (useful for cloud databases)disable - Explicitly disables SSL (default for local development)SSL can be configured via:
DB_SSL_MODE or POSTGRES_SSL_MODE?sslmode=require"sslmode": "require"The server automatically detects AWS RDS endpoints (hosts containing .rds.amazonaws.com) and:
https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem.aws-certs/ directory for 30 daysThis means you can connect to AWS RDS instances without manually downloading or configuring SSL certificates. Simply provide your RDS endpoint and the server handles the rest:
DB_HOST=mydb.cluster-xyz.us-east-1.rds.amazonaws.com
DB_USER=postgres
DB_PASSWORD=your_password
DB_NAME=your_database
# No need to set DB_SSL_MODE - automatically configured for RDS
Features:
.aws-certs/rds-global-bundle.pem and persists between sessionscheck_certificate_cache tool to view cache statusThe auto-configuration ensures secure, verified connections to AWS RDS while maintaining convenience and reliability.
list_tablesLists all tables in the database with their types.
get_schemaGets database schema information including tables and columns.
table_name - Get schema for a specific tableexecute_queryExecutes a SQL query (SELECT statements only for safety).
query - The SQL SELECT query to executedescribe_tableGet detailed information about a specific table including indexes and constraints.
table_name - Name of the table to describeget_table_sampleGets a sample of rows from a table.
table_name - Name of the table to samplelimit - Number of rows to return (default: 10, max: 100)check_certificate_cacheChecks the status of the AWS RDS certificate cache.
Set DB_READ_ONLY=true to disable all write and DDL tools at the server level. When enabled, the following tools are hidden from the tool list and will return an error if called:
update_datadelete_datainsert_dataexecute_raw_querycreate_tablealter_table{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["mcp-postgres@latest"],
"env": {
"DB_HOST": "localhost",
"DB_READ_ONLY": "true",
...
},
"autoApprove": ["list_tables", "get_schema"]
}
}
}
This is enforced server-side — it cannot be bypassed by the MCP client, and is independent of the client's autoApprove list.
Read-only mode is enforced at three levels:
default_transaction_read_only = on.query_data call runs inside an explicit BEGIN TRANSACTION READ ONLY block, so a query cannot switch the session back to read-write for a later statement.In read-only mode, query_data also rejects calls to VOLATILE PostgreSQL functions. Volatile functions are the ones PostgreSQL allows to have side effects, which includes dblink_exec() and similar functions that can open a second connection and write outside the current read-only transaction. This is enforced by looking up the function in pg_proc, not by matching a fixed list of names. A few harmless volatile functions (for example random()) are also rejected while DB_READ_ONLY=true.
query_data in read-only mode is intended for ordinary SELECT queries and built-in read-only functions. If you need extension functions that are genuinely read-only, they will work when they are marked STABLE or IMMUTABLE. For the strongest isolation, still connect with a SELECT-only database role — that also covers functions, operators, or type input functions that declare themselves non-volatile while performing external actions.
For defence in depth, connect with a PostgreSQL user that only has the permissions it needs. Even with DB_READ_ONLY=true, using a restricted database role ensures the server cannot accidentally or maliciously access data beyond its scope. A SELECT-only role is the strongest boundary and is the recommended setup for any server exposed to untrusted callers.
Read-only role (schema inspection + SELECT):
CREATE ROLE mcp_readonly WITH LOGIN PASSWORD 'strong_password';
-- Allow connecting to the database
GRANT CONNECT ON DATABASE your_database TO mcp_readonly;
-- Allow reading schema metadata
GRANT USAGE ON SCHEMA public TO mcp_readonly;
-- Allow SELECT on all current tables
GRANT SELECT ON ALL TABLES IN SCHEMA public TO mcp_readonly;
-- Allow SELECT on future tables too
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO mcp_readonly;
Read-write role (no DDL):
CREATE ROLE mcp_readwrite WITH LOGIN PASSWORD 'strong_password';
GRANT CONNECT ON DATABASE your_database TO mcp_readwrite;
GRANT USAGE ON SCHEMA public TO mcp_readwrite;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO mcp_readwrite;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO mcp_readwrite;
This role can use all tools except create_table, alter_table, and execute_raw_query (which can run DDL). Consider whether those tools are needed and restrict accordingly.
What to avoid: connecting as a superuser (postgres) or a user with CREATEDB / CREATEROLE privileges. If execute_raw_query is enabled, a superuser connection has no effective guardrails.
Only a single SELECT statement is allowed through the query_data tool; comments and string literals are parsed before the multi-statement check, so a marker such as -- inside a string cannot hide a second statement. In read-only mode, volatile functions are rejected as described above. The execute_raw_query tool has no such restriction — disable it via DB_READ_ONLY=true or use a role without write permissions if you don't need it.
You can test the server locally using the MCP Inspector tool:
# Install the MCP inspector
npm install -g @modelcontextprotocol/inspector
# Set your database credentials
$env:DB_HOST="localhost"
$env:DB_USER="postgres"
$env:DB_PASSWORD="your_password"
$env:DB_NAME="your_database"
# Run the inspector
mcp-inspector node server.mjs
The inspector opens a web UI where you can interactively test each tool and see the responses.
Once configured in your .kiro/settings/mcp.json, you can test the tools directly:
server.mjs Entry point: server wiring and shutdown handling
src/
sql.mjs Pure helpers: quoting, validation, query checks
ssl.mjs AWS RDS certificate handling and SSL config
config.mjs Configuration loading
db.mjs Database connection lifecycle
tools/
index.mjs Tool definitions and dispatch
read.mjs Read-only tool handlers
write.mjs Write and DDL tool handlers
test/
sql.test.mjs Unit tests for the pure helpers
test-tools.mjs Integration tests (need a running PostgreSQL)
Run npm run test:unit for the unit tests and npm test for everything.
MIT
Model Context Protocol server for PostgreSQL database operations
{ "mcpServers": { "mcp-postgres": { "command": "npx", "args": [ "-y", "[email protected]" ] } }}Công cụ
Chưa có danh sách công cụ. Máy chủ từ xa sẽ có danh sách sau lần kiểm tra tình trạng đầu tiên.
Câu hỏi thường gặp
Sao chép cấu hình stdio cho Cursor, Claude Desktop, Claude Code hoặc VS Code trên trang này. Cấu hình dùng bản cài đặt của nhà phát hành, không phải proxy của PlugMCP.
Bản ghi này không liệt kê biến môi trường bắt buộc nào.
MCP Postgres chạy cục bộ qua stdio.
Kiểm tra lần cuối: 2026-11-15. Model Context Protocol server for PostgreSQL database operations