Add connection timeout support for PostgreSQL in MCP Database Server

Enhanced the PostgreSQL connection handling by introducing a `--connection-timeout` parameter, allowing users to specify a custom timeout in milliseconds. Updated relevant documentation in README and postgresql-setup.md to reflect this new option. Modified the PostgresqlAdapter to utilize the connection timeout setting during database connections.
This commit is contained in:
Karthik KK
2025-04-22 16:44:36 +12:00
parent bd819d923f
commit c71779fcb5
6 changed files with 22 additions and 2 deletions

View File

@@ -18,6 +18,7 @@ export class PostgresqlAdapter implements DbAdapter {
port?: number;
ssl?: boolean | object;
options?: any;
connectionTimeout?: number;
}) {
this.host = connectionInfo.host;
this.database = connectionInfo.database;
@@ -29,7 +30,9 @@ export class PostgresqlAdapter implements DbAdapter {
port: connectionInfo.port || 5432,
user: connectionInfo.user,
password: connectionInfo.password,
ssl: connectionInfo.ssl
ssl: connectionInfo.ssl,
// Add connection timeout if provided (in milliseconds)
connectionTimeoutMillis: connectionInfo.connectionTimeout || 30000,
};
}
@@ -39,6 +42,15 @@ export class PostgresqlAdapter implements DbAdapter {
async init(): Promise<void> {
try {
console.error(`[INFO] Connecting to PostgreSQL: ${this.host}, Database: ${this.database}`);
console.error(`[DEBUG] Connection details:`, {
host: this.host,
database: this.database,
port: this.config.port,
user: this.config.user,
connectionTimeoutMillis: this.config.connectionTimeoutMillis,
ssl: !!this.config.ssl
});
this.client = new pg.Client(this.config);
await this.client.connect();
console.error(`[INFO] PostgreSQL connection established successfully`);