Skip to main content
Glama

test-postgres-connection

Verify PostgreSQL database connectivity to confirm the connection is active and working properly before executing queries.

Instructions

Test the Postgres connection and return connection status

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The handler function for the 'test-postgres-connection' tool. It initializes the DB connection, tests it using testDB helper, and returns success or error status 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:85-132 (registration)
    Registers the 'test-postgres-connection' tool on the MCP server with title, description, and the handler function.
    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 }; } } );
  • Helper function that performs a simple SELECT query to test the database connection and returns success or failure with details.
    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) }; } };
  • Helper that initializes the global database connection if not already done, using getDb.
    const initDb = () => { if(!dbConnection) { dbConnection = getDb(); } return dbConnection; };
  • Helper function to create a new Postgres database connection using environment variables.
    const getDb = () => { const db = postgres({ host: process.env.DB_HOST, port: parseInt(process.env.DB_PORT || '5432'), database: process.env.DB_NAME, username: process.env.DB_USERNAME, password: process.env.DB_PASSWORD, ssl: 'prefer' }); return db; };

Latest Blog Posts

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/martymarkenson/Postgres-Connector-MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server