Skip to main content
Glama
melihbirim

PostgreSQL MCP Server

by melihbirim

connect_database

Establish a connection to a PostgreSQL database using specified host, port, database name, username, password, and SSL settings. Facilitates secure database interactions and query execution.

Instructions

Connect to a PostgreSQL database

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
databaseYesDatabase name
hostNoDatabase host (default: localhost)
passwordNoDatabase password
portNoDatabase port (default: 5432)
sslNoUse SSL connection (default: false)
usernameYesDatabase username

Implementation Reference

  • src/index.ts:65-115 (registration)
    Registration of the 'connect_database' tool using server.tool(). Includes tool name, description, input schema defined with Zod, and inline asynchronous handler function.
    server.tool( "connect_database", "Connect to a PostgreSQL database", { host: z.string().describe("Database host (default: localhost)").optional(), port: z.number().describe("Database port (default: 5432)").optional(), database: z.string().describe("Database name"), username: z.string().describe("Database username"), password: z.string().describe("Database password").optional(), ssl: z.boolean().describe("Use SSL connection (default: false)").optional(), }, async ({ host, port, database, username, password, ssl }) => { try { // Close existing connection if any if (dbClient) { await dbClient.end(); } // Create new connection dbClient = new Client({ host: host || "localhost", port: port || 5432, database, user: username, password, ssl: ssl || false, }); await dbClient.connect(); return { content: [ { type: "text", text: `Successfully connected to PostgreSQL database '${database}' on ${host || "localhost"}:${port || 5432}`, }, ], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : "Unknown connection error"; return { content: [ { type: "text", text: `Failed to connect to database: ${errorMessage}`, }, ], }; } } );
  • The handler function for the connect_database tool. It closes any existing database connection, creates a new PostgreSQL Client with the provided configuration (using defaults where applicable), connects to the database, and returns a success message or error details.
    async ({ host, port, database, username, password, ssl }) => { try { // Close existing connection if any if (dbClient) { await dbClient.end(); } // Create new connection dbClient = new Client({ host: host || "localhost", port: port || 5432, database, user: username, password, ssl: ssl || false, }); await dbClient.connect(); return { content: [ { type: "text", text: `Successfully connected to PostgreSQL database '${database}' on ${host || "localhost"}:${port || 5432}`, }, ], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : "Unknown connection error"; return { content: [ { type: "text", text: `Failed to connect to database: ${errorMessage}`, }, ], }; } }
  • Input schema for the connect_database tool, defined using Zod object with parameters for PostgreSQL connection: host, port, database, username, password, ssl, including descriptions and optionals.
    host: z.string().describe("Database host (default: localhost)").optional(), port: z.number().describe("Database port (default: 5432)").optional(), database: z.string().describe("Database name"), username: z.string().describe("Database username"), password: z.string().describe("Database password").optional(), ssl: z.boolean().describe("Use SSL connection (default: false)").optional(), },

Other Tools

Related Tools

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/melihbirim/pg-mcp'

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