PostgreSQL MCP Server
PostgreSQL MCP Server
A comprehensive Model Context Protocol (MCP) server for PostgreSQL database access. Provides 36 tools for querying, managing, and interacting with PostgreSQL databases through the MCP interface.
Features
36 Database Tools: Complete set of read-only and write operations
PostgreSQL-Specific Features: Schema support, JSONB operations, extensions, functions, triggers, views, sequences
Full SSL/TLS Support: CA certificates, client certificates, configurable TLS versions
Security First: Query validation, rate limiting, blocked dangerous operations
Connection Pooling: Efficient connection management with configurable limits
Audit Logging: Track all database operations
Installation
# Clone or copy to your tools directory
cd /path/to/tools/mav-postgresql-mcp-server
# Install dependencies
npm install
# Build the server
npm run buildConfiguration
Copy .env.example to .env and configure your PostgreSQL connection:
cp .env.example .envRequired Settings
Variable | Description | Default |
| PostgreSQL server hostname |
|
| PostgreSQL server port |
|
| Database username |
|
| Database password | - |
| Target database name | - |
| Default schema |
|
SSL Configuration
Variable | Description | Options |
| SSL connection mode |
|
| Reject self-signed certs |
|
| Path to CA certificate | - |
| Path to client certificate | - |
| Path to client key | - |
| Minimum TLS version |
|
Security Settings
Variable | Description | Default |
| Enable INSERT/UPDATE/DELETE |
|
| Max pool connections |
|
| Query timeout (ms) |
|
| Maximum rows returned |
|
Rate Limiting
Variable | Description | Default |
| Queries per minute |
|
| Queries per hour |
|
| Concurrent queries |
|
Usage
With Claude Desktop
Add to your Claude Desktop configuration (~/Library/Application Support/Claude/claude_desktop_config.json on macOS):
{
"mcpServers": {
"postgresql": {
"command": "node",
"args": ["/path/to/mav-postgresql-mcp-server/build/index.js"],
"env": {
"PG_HOST": "localhost",
"PG_PORT": "5432",
"PG_USER": "your_user",
"PG_PASSWORD": "your_password",
"PG_DATABASE": "your_database",
"PG_SCHEMA": "public",
"ALLOW_WRITE_OPERATIONS": "false"
}
}
}
}With MCP Inspector
npx @anthropic/mcp-inspector node build/index.jsAvailable Tools
Core Read-Only Tools (7)
Tool | Description |
| Execute SELECT queries |
| List all tables in schema |
| Get table structure and columns |
| Get database version and settings |
| List indexes on a table |
| Get query execution plan |
| List table constraints |
PostgreSQL-Specific Read-Only Tools (14)
Tool | Description |
| List all schemas in database |
| Get current search path |
| List installed extensions |
| Get detailed extension information |
| List user-defined functions |
| List triggers on a table |
| List views in schema |
| List sequences in schema |
| Get table statistics |
| Get current connection details |
| Get database/table sizes |
| Query JSONB columns |
| Execute JSON path queries |
Write Operation Tools (15)
Requires ALLOW_WRITE_OPERATIONS=true
Tool | Description |
| Insert a single row |
| Update rows with conditions |
| Delete rows with conditions |
| Create a new table |
| Modify table structure |
| Drop a table |
| Insert multiple rows |
| Call stored procedures |
| Create an index |
| Remove an index |
| Rename a table |
| Change schema search path |
| Create a new schema |
| Drop a schema |
| Update JSONB fields |
| Optimize table statistics |
MCP Resources
The server exposes database schema as MCP resources:
pg://database/schema- List all tables and columnspg://database/info- Database informationpg://table/{schema}.{table}- Individual table schema
Security Features
Blocked Operations
The server blocks dangerous operations by default:
File system operations (
COPY FROM/TO,pg_read_file, etc.)Permission modifications (
GRANT,REVOKE,ALTER ROLE)Administrative commands (
CREATE ROLE,DROP DATABASE, etc.)System catalog modifications
Protected Tables
Access to sensitive system tables is blocked:
pg_catalog.pg_authidpg_catalog.pg_shadowpg_catalog.pg_auth_members
Query Validation
All identifiers are validated (max 63 characters, safe characters only)
Query timeouts prevent long-running operations
Rate limiting prevents abuse
Setting Up a Read-Only User
For production use, create a dedicated read-only PostgreSQL user:
# Run as PostgreSQL superuser
psql -U postgres -f setup-readonly-user.sqlOr manually:
-- Create user
CREATE USER mcp_readonly WITH PASSWORD 'secure_password';
-- Grant connect
GRANT CONNECT ON DATABASE your_database TO mcp_readonly;
-- Grant schema usage
GRANT USAGE ON SCHEMA public TO mcp_readonly;
-- Grant read access to all tables
GRANT SELECT ON ALL TABLES IN SCHEMA public TO mcp_readonly;
-- Set default privileges for future tables
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT ON TABLES TO mcp_readonly;Development
# Run in development mode
npm run dev
# Build for production
npm run build
# Type checking
npm run typecheckTroubleshooting
Connection Issues
Verify PostgreSQL is running:
pg_isready -h localhost -p 5432Check credentials:
psql -h localhost -U your_user -d your_databaseEnable debug mode:
MCP_DEBUG=true
SSL Issues
Verify certificate paths are correct
Check certificate permissions (readable by the user running the server)
Try
PG_SSL_MODE=requirefirst, then upgrade toverify-caorverify-full
Rate Limiting
If you're hitting rate limits:
Increase
RATE_LIMIT_PER_MINUTEandRATE_LIMIT_PER_HOURBatch operations where possible
Use more specific queries to reduce call volume
License
MIT
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
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/derricksiawor/mav-postgresql-mcp-server'
If you have feedback or need assistance with the MCP directory API, please join our Discord server