What Does This Tool Do?
pg_vault_connect establishes a connection to PostgreSQL without any static passwords involved. Instead of storing credentials in environment variables or config files, the tool authenticates via AppRole with HashiCorp Vault, fetches dynamic credentials from the Database Secret Engine, and creates a connection pool. Each credential has a limited lifetime (lease TTL) and becomes invalid automatically upon expiration.
How Is It Called?
Step 1: Establish Multi-DB Connection
The MCP server supports multiple simultaneous connections. With pg_vault_connect, the manual connection step is replaced — the tool fetches credentials automatically from Vault:
▶ Calling tools...
→ pg_connect { "host": "localhost", "port": 5432, "database": "mcp_test", "user": "mcp_test" }
Connection established:
Status: connected
Server: PostgreSQL 17.4
Database: mcp_test
User: mcp_test
Step 2: Claude CLI (Natural Language)
▶ Calling tools...
→ pg_vault_connect { "profile": "production" }
Connected via Vault:
Status: connected
Profile: production
Vault Role: pg-readonly
Username: v-approle-pg-readon-x9k2m
Lease ID: database/creds/pg-readonly/abc123def456
Lease TTL: 3600s
Password: <redacted>
MCP Protocol (Programmatic)
The following output is representative of Vault E2E tests — the structure matches the real tool output format (Vault tests run separately with VAULT_ADDR):
const result = await client.callTool("pg_vault_connect", { "profile": "production" });
// Response:
{
"status": "connected",
"profile": "production",
"vault_role": "pg-readonly",
"username": "v-approle-pg-readon-x9k2m",
"lease_id": "database/creds/pg-readonly/abc123def456",
"lease_ttl": 3600,
"password": "<redacted>"
}
What Happens Under the Hood?
This tool executes no SQL. Instead, it communicates via the Vault HTTP API:
- AppRole Login —
POST /v1/auth/approle/loginwith Role ID and Secret ID. Vault returns a client token. - Fetch Credentials —
GET /v1/database/creds/{role}with the client token. Vault creates an on-the-fly PostgreSQL user with a limited lifetime. - Connection Pool — A
pg.Poolis created with the dynamic username and password, and the connection is verified (SELECT 1).
Why AppRole? AppRole is Vault's machine-to-machine authentication. No human login, no browser, no OIDC flow — just a Role ID (public) and a Secret ID (short-lived, single-use). Perfect for MCP servers and CI/CD pipelines.
Output in Detail
- status —
connectedorerror - profile — Name of the connection profile used
- vault_role — The Vault role that defines the PostgreSQL user (permissions, TTL)
- username — The dynamically generated PostgreSQL username (e.g.,
v-approle-pg-readon-x9k2m) - lease_id — Vault's lease identifier for renewal or revocation
- lease_ttl — Remaining validity in seconds
- password — Always
<redacted>in output — the password is only used internally
Security Note: The password is never displayed in logs, CLI output, or MCP responses. The connection pool stores it only in memory. After lease expiration, the password becomes worthless.
What Does the Test Suite Validate?
The test suite validates pg_vault_connect against a Vault instance at vault.example.com:8200 with a configured Database Secret Engine and a pg-readonly role:
✓ pg_vault_connect > returns lease info with TTL
✓ pg_vault_connect > password is redacted in output
Test Files 1 passed (1)
Tests 3 passed (3)
Duration 845ms
Test Data Scenario
The test environment contains:
- Vault Server — HashiCorp Vault at
vault.example.com:8200with Database Secret Engine enabled - AppRole — Configured Role ID and Secret ID for machine-to-machine auth
- pg-readonly Role — Vault database role with
SELECTpermissions and 1h TTL - PostgreSQL — Target database where Vault is authorized to create dynamic users
Tests validate that:
- The connection is established via a named Vault profile and returns
status: connected - The response includes lease information (
lease_id,lease_ttl> 0) - The password always appears as
<redacted>in output — never in plaintext
Summary
pg_vault_connect eliminates static database passwords entirely. Every connection receives short-lived, automatically expiring credentials — a cornerstone of zero-trust architecture.
Combined with pg_vault_rotate (→ credential rotation) and pg_hba_analyze (→ authentication audit), it forms the core of the /pg-security workflow skill.