diff --git a/backend/src/services/database/factory.ts b/backend/src/services/database/factory.ts index ddf2a2a..8724c1c 100644 --- a/backend/src/services/database/factory.ts +++ b/backend/src/services/database/factory.ts @@ -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); } diff --git a/backend/src/services/database/postgresAdapter.ts b/backend/src/services/database/postgresAdapter.ts index 66d7c82..4f3062b 100644 --- a/backend/src/services/database/postgresAdapter.ts +++ b/backend/src/services/database/postgresAdapter.ts @@ -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