Fix PostgreSQL SSL configuration for Azure

- Explicitly configure SSL in PostgresAdapter Pool
- Always require SSL for Azure PostgreSQL connections
- Add logging for database connection details
This commit is contained in:
2026-01-22 01:51:20 +01:00
parent b8d7e7a229
commit 71480cedd6
2 changed files with 12 additions and 1 deletions

View File

@@ -30,10 +30,13 @@ export function createDatabaseAdapter(dbType?: string, dbPath?: string, allowClo
const name = process.env.DATABASE_NAME || 'cmdb';
const user = process.env.DATABASE_USER || 'cmdb';
const password = process.env.DATABASE_PASSWORD || '';
const ssl = process.env.DATABASE_SSL === 'true' ? '?sslmode=require' : '';
// Azure PostgreSQL requires SSL - always use sslmode=require for Azure
const isAzure = host.includes('.postgres.database.azure.com');
const ssl = (process.env.DATABASE_SSL === 'true' || isAzure) ? '?sslmode=require' : '';
const constructedUrl = `postgresql://${user}:${password}@${host}:${port}/${name}${ssl}`;
logger.info('Creating PostgreSQL adapter with constructed connection string');
logger.info(`Database: ${name}, SSL: ${ssl ? 'required' : 'not required'}`);
return new PostgresAdapter(constructedUrl, allowClose);
}

View File

@@ -18,11 +18,19 @@ export class PostgresAdapter implements DatabaseAdapter {
constructor(connectionString: string, allowClose: boolean = true) {
this.connectionString = connectionString;
this.allowClose = allowClose;
// Parse connection string to extract SSL requirement
const url = new URL(connectionString);
const sslRequired = url.searchParams.get('sslmode') === 'require' ||
url.searchParams.get('sslmode') === 'require' ||
process.env.DATABASE_SSL === 'true';
this.pool = new Pool({
connectionString,
max: 20, // Maximum number of clients in the pool
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 10000, // Increased timeout for initial connection
ssl: sslRequired ? { rejectUnauthorized: false } : false, // Azure PostgreSQL requires SSL
});
// Handle pool errors