mcp-postgres
Provides comprehensive tools for PostgreSQL database management, including connection management, query execution, schema introspection, CRUD operations, server management, and HA monitoring.
Integrates with HashiCorp Vault for fetching PostgreSQL credentials securely using AppRole authentication, eliminating the need to store passwords in environment variables or config files.
Click on "Deploy 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., "@mcp-postgres show me the 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.
mcp-postgres
A comprehensive PostgreSQL MCP (Model Context Protocol) server providing 27 tools for database management and administration.
Features
Connection Management — connect, disconnect, pool health monitoring
Query Execution — parameterized queries, EXPLAIN ANALYZE, prepared statements
Schema Introspection — tables, indexes, constraints, views, functions, enums, extensions
CRUD Operations — type-safe insert, update, delete, upsert with injection protection
Server Management — version, settings, config reload, uptime
Database Sizing — database and table sizes with index/toast breakdown
Related MCP server: postgres-mcp-server
Installation
npm install @itunified.io/mcp-postgresOr run directly:
npx @itunified.io/mcp-postgresConfiguration
Set one of the following environment variables:
# Option 1: Connection string (preferred)
export POSTGRES_CONNECTION_STRING="postgresql://myuser:mypassword@your-database.example.com:5432/mydb"
# Option 2: Individual variables
export PGHOST="your-database.example.com"
export PGPORT="5432"
export PGUSER="myuser"
export PGPASSWORD="mypassword"
export PGDATABASE="mydb"
export PGSSLMODE="require" # optionalMulti-Database Configuration
Create a config file at ~/.config/mcp-postgres/databases.yaml:
databases:
production:
host: db.example.com
port: 5432
user: admin
password: ${DB_PROD_PASSWORD}
database: myapp
ssl: true
staging:
host: staging-db.example.com
port: 5432
user: admin
password: ${DB_STAGING_PASSWORD}
database: myapp
default: productionEnvironment variables in ${VAR_NAME} syntax are automatically expanded.
Config file discovery order:
POSTGRES_CONFIG_FILEenv var (explicit path)~/.config/mcp-postgres/databases.yamlordatabases.jsonPOSTGRES_CONNECTION_STRINGenv var (single database)Individual
PG*env vars (single database)
Override the config path with POSTGRES_CONFIG_FILE env var:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["@itunified.io/mcp-postgres"],
"env": {
"POSTGRES_CONFIG_FILE": "/path/to/databases.yaml"
}
}
}
}Use pg_list_connections to see all configured databases, pg_switch_database to change the active one.
HashiCorp Vault Integration (Optional)
mcp-postgres supports opportunistic secret loading from HashiCorp Vault via AppRole authentication. When configured, it fetches PostgreSQL credentials from a KV v2 path — so you never need to put database passwords in environment variables or config files.
How it works:
On startup, the server checks for
NAS_VAULT_ADDR,NAS_VAULT_ROLE_ID, andNAS_VAULT_SECRET_IDin the environmentIf all three are set, it logs in via AppRole and reads the configured KV v2 path
It populates
POSTGRES_CONNECTION_STRINGandPG*env vars from the Vault secret — but only for vars not already setIf Vault is not configured or unreachable, the server silently falls back to env vars
Precedence: Explicit env vars → Vault → config file fallback → (error if nothing set)
Variable | Required | Description |
| Yes* | Vault server address (e.g., |
| Yes* | AppRole role ID for this server |
| Yes* | AppRole secret ID for this server |
| No | KV v2 mount path (default: |
* Only required if using Vault. Without these, the server uses env vars / config files directly.
Vault KV v2 secret structure:
# Path: kv/your/postgres/secret
{
"connection_string": "postgresql://myuser:mypassword@your-database.example.com:5432/mydb",
"host": "your-database.example.com",
"port": "5432",
"user": "myuser",
"password": "mypassword",
"database": "mydb"
}Key mapping: connection_string → POSTGRES_CONNECTION_STRING, host → PGHOST, port → PGPORT, user → PGUSER, password → PGPASSWORD, database → PGDATABASE
Tip: You can store either
connection_string(for single-database setups) or individual fields (host/port/user/password/database), or both. The loader maps whatever keys are present.
Vault setup steps:
Write PG credentials to a KV v2 path:
vault kv put kv/your/postgres/secret \ connection_string="postgresql://myuser:mypassword@your-database.example.com:5432/mydb" \ host="your-database.example.com" \ port="5432" \ user="myuser" \ password="mypassword" \ database="mydb"Create a read-only policy:
path "kv/data/your/postgres/secret" { capabilities = ["read"] }Create an AppRole and get credentials:
vault write auth/approle/role/mcp-postgres \ token_policies="mcp-postgres" token_ttl=1h vault read auth/approle/role/mcp-postgres/role-id vault write -f auth/approle/role/mcp-postgres/secret-idConfigure the server with Vault env vars (no PG creds needed):
{ "mcpServers": { "postgres": { "command": "npx", "args": ["@itunified.io/mcp-postgres"], "env": { "NAS_VAULT_ADDR": "https://vault.example.com:8200", "NAS_VAULT_ROLE_ID": "your-role-id", "NAS_VAULT_SECRET_ID": "your-secret-id" } } } }
Note: Config file options (
POSTGRES_CONFIG_FILE,databases.yaml) andPGSSLMODEare not loaded from Vault — set them via env vars if needed.
Claude Desktop / MCP Settings
Add to your settings.json:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["@itunified.io/mcp-postgres"],
"env": {
"POSTGRES_CONNECTION_STRING": "postgresql://myuser:mypassword@your-database.example.com:5432/mydb"
}
}
}
}Tools
Connection (5 tools)
Tool | Description |
| Connect to a database (default or named) |
| Disconnect from a database or all |
| Pool health for active or named database |
| List all configured databases and status |
| Switch the active database context |
Query (3 tools)
Tool | Description |
| Execute parameterized SELECT/DML query |
| Run EXPLAIN ANALYZE on a query |
| Manage named prepared statements (PREPARE/EXECUTE/DEALLOCATE) |
Schema Introspection (9 tools)
Tool | Description |
| List all schemas |
| List tables (with optional schema filter) |
| Describe table columns, types, defaults, constraints |
| List indexes for a table |
| List constraints (PK, FK, unique, check) |
| List views with definitions |
| List functions/procedures with signatures |
| List enum types and values |
| List installed extensions |
CRUD (4 tools)
Tool | Description |
| Insert row(s) with parameterized values |
| Update rows (requires |
| Delete rows (requires |
| Insert or update on conflict (requires |
Server (4 tools)
Tool | Description |
| PostgreSQL version |
| Show/search server configuration |
| Reload configuration (requires |
| Server uptime and start time |
HA Monitoring (4 tools)
Tool | Description |
| Streaming replication state and lag |
| List replication slots |
| WAL generation rate and archive status |
| Primary vs standby detection |
Database Management (2 tools)
Tool | Description |
| Size of all databases |
| Table sizes with index/toast breakdown |
Enterprise Edition
For advanced PostgreSQL operations, mcp-postgres-enterprise extends this server with:
DBA Monitoring — VACUUM, ANALYZE, REINDEX, pg_stat_activity, table/index stats, locks, cache hit ratio, bloat detection
CloudNativePG (CNPG) — K8s cluster management, failover, switchover, backup orchestration
HA Operations — Replication slot management, PgBouncer pool control
Backup / PITR — pg_dump/pg_restore orchestration, point-in-time recovery
RBAC — Role management, privilege grants, row-level security policies
Audit — Query log analysis, connection audit, permission mapping
Compliance — SSL enforcement, connection limit checks
Available as a private GitHub package. Contact itunified.io for access.
Security
Query Safety Model
CRUD tools (
pg_insert,pg_update,pg_delete,pg_upsert): All use parameterized queries ($1,$2, ...) — safe from SQL injection. Destructive operations requireconfirm: true.pg_query: Unrestricted raw SQL runner by design — intended for power users who need full SQL flexibility. No injection protection is applied because the tool's purpose is to execute arbitrary SQL.pg_query_explain: Defaults to safeplanmode (EXPLAIN only, no execution).mode=analyzealways requiresconfirm: truebecause EXPLAIN ANALYZE executes the statement.pg_query_prepared: Deprecated. Prepared statements are session-local in PostgreSQL and unreliable with connection pools. Statement names are validated as SQL identifiers. Use parameterizedpg_queryinstead.
Destructive Operations
These tools require confirm: true to execute:
pg_update,pg_delete,pg_upsert— data modificationpg_reload_config— server configurationpg_query_explain(analyze mode) — statement execution
Credentials
Connection credentials are read from environment variables or JSON/YAML config — never logged or stored
All identifiers (table, column, schema names) are validated against a strict regex pattern
License
This project is dual-licensed:
AGPL-3.0 — Free for open-source and non-commercial use
Commercial License — For proprietary and commercial use
See COMMERCIAL_LICENSE.md for details.
Contributing
Contributions are welcome! Please open an issue first to discuss proposed changes.
Available Tools
27 toolspg_connectA
Connect to a PostgreSQL server target. If multiple targets are configured, specify which one. Otherwise connects to the default target.
Inputs: target (required), profile.
pg_connection_statusC
Check connection pool health for the active profile or a specific named profile.
Inputs: target (required), profile.
pg_constraint_listB
List constraints (primary key, foreign key, unique, check) for a table.
Inputs: table (required), target (required), schema.
pg_database_sizeC
Show size of all databases.
Inputs: target (required).
pg_deleteA
Delete rows matching a WHERE clause. Requires confirm=true.
Inputs: table (required), where (required), target (required), confirm (required), +2 more.
pg_disconnectA
Disconnect from a PostgreSQL server target. Omit profile to disconnect all.
Inputs: target (required), profile.
pg_enum_listC
List all enum types and their values.
Inputs: target (required).
pg_extension_listA
List installed PostgreSQL extensions with versions.
Inputs: target (required).
pg_function_listC
List functions and procedures with their signatures.
Inputs: target (required), schema.
pg_index_listA
List indexes for a table with type, columns, uniqueness, and size.
Inputs: table (required), target (required), schema.
pg_insertB
Insert one or more rows into a table. Returns inserted rows.
Inputs: table (required), target (required), values (required), columns (required), +1 more.
pg_list_connectionsB
List all configured connection profiles and their status. Each profile represents a PostgreSQL server instance.
Inputs: target (required).
pg_queryB
Execute a parameterized SQL query. Returns rows as JSON. Use $1, $2, ... for parameters.
Inputs: sql (required), target (required), params.
pg_query_explainA
Run EXPLAIN on a query. mode=plan (default, safe) shows the plan without executing. mode=analyze executes the statement and shows actual timing — requires confirm=true.
Inputs: sql (required), target (required), mode, params, +1 more.
pg_query_preparedA
[DEPRECATED] Manage named prepared statements: PREPARE, EXECUTE, DEALLOCATE. Prepared statements are session-local and unreliable with connection pools. Use parameterized pg_query instead.
Inputs: name (required), target (required), action_type (required), sql, +1 more.
pg_reload_configA
Reload server configuration files (postgresql.conf). Requires confirm=true.
Inputs: target (required), confirm (required).
pg_schema_listB
List all schemas in the database.
Inputs: target (required).
pg_settingsB
Show or search server configuration parameters.
Inputs: target (required), name.
pg_switch_databaseA
Switch the active connection profile. All subsequent queries will use this profile unless overridden.
Inputs: target (required), profile (required).
pg_table_describeA
Describe a table: columns, data types, defaults, nullability, and constraints.
Inputs: table (required), target (required), schema.
pg_table_listB
List tables in the database. Optionally filter by schema name.
Inputs: target (required), schema.
pg_table_sizesA
Show table sizes with index and toast breakdown, sorted by total size descending.
Inputs: target (required), schema.
pg_updateB
Update rows matching a WHERE clause. Requires confirm=true.
Inputs: set (required), table (required), where (required), target (required), +3 more.
pg_upsertC
Insert or update on conflict. Requires confirm=true.
Inputs: table (required), target (required), values (required), columns (required), +3 more.
pg_uptimeB
Show server uptime and start time.
Inputs: target (required).
pg_versionB
Get PostgreSQL version string and numeric version.
Inputs: target (required).
pg_view_listB
List views with their definitions. Optionally filter by schema.
Inputs: target (required), schema.
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 27 tools
All 27 tools have clearly distinct purposes. Overlap between pg_query, pg_query_explain, and pg_query_prepared is minimal and explained, and pg_connect/pg_switch_database are well-differentiated. No ambiguity for an agent.
Tools consistently use the 'pg_' prefix followed by a descriptive verb or verb_noun pattern. Although pg_connection_status is slightly irregular, the overall pattern is uniform and predictable.
27 tools is on the higher end of typical (3-15), but each tool serves a necessary PostgreSQL operation: connection, DML, introspection, admin. The scope justifies the count, but fewer tools could suffice if combined.
The toolset covers all major PostgreSQL operations: connection management, CRUD, schema introspection, indexes/constraints, querying with parameters/explain, admin (version, settings, reload, uptime, sizes). No obvious gaps for a general-purpose database client.
Maintenance
Related MCP Connectors
PostgreSQL, MySQL, and SQL Server in one session. 26 read-only MCP tools for AI agents.
Hosted MCP server for PostgreSQL diagnostics: slow queries, missing indexes, connection pressure.
Hosted PostgreSQL MCP server for Claude, ChatGPT and Cursor, with each action switched per account.
Managed Postgres MCP: projects, SQL, docs search, and storage buckets via OAuth.
Related MCP Servers
- AlicenseAqualityBmaintenanceMCP server with 14 tools for PostgreSQL database operations. Query databases, explore schemas, analyze tables, with SQL injection prevention and read-only mode by default.1412MIT
- AlicenseAqualityDmaintenanceProvides PostgreSQL database management and analysis via MCP, enabling schema exploration, query execution, performance monitoring, and database health checks.3640 npmMIT
- AlicenseAqualityCmaintenanceFull-featured MCP server that exposes 36 tools for interacting with PostgreSQL databases, covering schema introspection, query execution, data exploration, performance monitoring, security auditing, and maintenance.365 npmMIT
- FlicenseNot gradedqualityDmaintenanceMCP server for PostgreSQL database management, enabling AI-assisted schema exploration, stored procedure analysis, test data generation, and safe transaction management via Claude Desktop.-