Test Postgres Connection
test-postgres-connectionCheck if your PostgreSQL database is accessible. Returns connection status to verify connectivity before executing queries.
Instructions
Test the Postgres connection and return connection status
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/server.ts:91-131 (handler)The main handler function for the 'test-postgres-connection' tool. It attempts to connect to the database using initDb(), tests the connection via testDB(), and returns success/failure with configuration details.
async () => { try { // Show current configuration (without password) const config = { host: process.env.DB_HOST, port: process.env.DB_PORT || '5432', database: process.env.DB_NAME, username: process.env.DB_USERNAME, password: '***hidden***' }; // Attempt to connect and test const db = initDb(); const testResult = await testDB(db); if (testResult.success) { return { content: [{ type: "text", text: `✅ Database connection successful!\n\nConfiguration:\n${JSON.stringify(config, null, 2)}\n\nConnection test result: ${JSON.stringify(testResult.result, null, 2)}` }] }; } else { return { content: [{ type: "text", text: `❌ Database connection failed!\n\nConfiguration:\n${JSON.stringify(config, null, 2)}\n\nError: ${testResult.error}` }], isError: true }; } } catch (error) { return { content: [{ type: "text", text: `❌ Unexpected error during connection test: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } - src/server.ts:87-90 (schema)Input schema/parameter definition for the tool. No input parameters are defined — the tool takes no arguments.
{ title: "Test Postgres Connection", description: "Test the Postgres connection and return connection status", }, - src/server.ts:85-132 (registration)Registration of the tool via server.registerTool with name 'test-postgres-connection', a title, description, and the handler callback.
server.registerTool( "test-postgres-connection", { title: "Test Postgres Connection", description: "Test the Postgres connection and return connection status", }, async () => { try { // Show current configuration (without password) const config = { host: process.env.DB_HOST, port: process.env.DB_PORT || '5432', database: process.env.DB_NAME, username: process.env.DB_USERNAME, password: '***hidden***' }; // Attempt to connect and test const db = initDb(); const testResult = await testDB(db); if (testResult.success) { return { content: [{ type: "text", text: `✅ Database connection successful!\n\nConfiguration:\n${JSON.stringify(config, null, 2)}\n\nConnection test result: ${JSON.stringify(testResult.result, null, 2)}` }] }; } else { return { content: [{ type: "text", text: `❌ Database connection failed!\n\nConfiguration:\n${JSON.stringify(config, null, 2)}\n\nError: ${testResult.error}` }], isError: true }; } } catch (error) { return { content: [{ type: "text", text: `❌ Unexpected error during connection test: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } ); - src/server.ts:34-46 (helper)Helper function 'testDB' used to execute a simple 'SELECT 1 as test' query against the database to verify connectivity.
const testDB = async (db: postgres.Sql) => { try { // Simple query to test connection const result = await db`SELECT 1 as test`; return { success: true, message: "Connection successful", result }; } catch (error) { return { success: false, message: "Connection failed", error: error instanceof Error ? error.message : String(error) }; } }; - src/server.ts:49-54 (helper)Helper function 'initDb' that initializes (or returns existing) database connection using 'getDb'.
const initDb = () => { if(!dbConnection) { dbConnection = getDb(); } return dbConnection; };