{"record":{"id":"639689581acd781b","repo":"n8n-io/n8n","slug":"driver-not-connected","errorCode":null,"errorMessage":"Driver not Connected","messagePattern":"Driver not Connected","errorType":"exception","errorClass":"TypeORMError","httpStatus":null,"severity":"critical","filePath":"packages/@n8n/typeorm/src/driver/postgres/PostgresDriver.ts","lineNumber":1052,"sourceCode":"\t\t\t\ttype = `${column.type}(${column.spatialFeatureType})`;\n\t\t\t} else {\n\t\t\t\ttype = column.type;\n\t\t\t}\n\t\t}\n\n\t\tif (column.isArray) type += ' array';\n\n\t\treturn type;\n\t}\n\n\t/**\n\t * Obtains a new database connection to a master server.\n\t * Used for replication.\n\t * If replication is not setup then returns default connection's database connection.\n\t */\n\tasync obtainMasterConnection(): Promise<[any, Function]> {\n\t\tif (!this.master) {\n\t\t\tthrow new TypeORMError('Driver not Connected');\n\t\t}\n\n\t\tconst connection = await this.master.connect();\n\t\t// Apply per-connection session settings (schema creation moved to afterConnect)\n\t\tconst { schema, statementTimeout } = this.options;\n\t\tif (schema && schema !== 'public') {\n\t\t\tawait connection.query(`SET search_path TO \"${schema}\",public`);\n\t\t} else {\n\t\t\tawait connection.query('SET search_path TO public');\n\t\t}\n\t\tif (statementTimeout) {\n\t\t\tawait connection.query(`SET statement_timeout = ${statementTimeout}`);\n\t\t}\n\n\t\treturn [connection, () => connection.release()];\n\t}\n\n\t/**","sourceCodeStart":1034,"sourceCodeEnd":1070,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/typeorm/src/driver/postgres/PostgresDriver.ts#L1034-L1070","documentation":"TypeORMError('Driver not Connected') is raised in PostgresDriver.obtainMasterConnection when this.master is falsy. The master connection pool is created during connect(), so a missing master means the driver was used before connect completed (or after disconnect).","triggerScenarios":"Code calls repository.query / any pool-acquiring operation before DataSource.initialize()/driver.connect() has resolved, or after DataSource.destroy() has torn the pool down. Also reached if connect() failed silently and the master pool was never assigned.","commonSituations":"A service starts issuing DB queries in a constructor or module init that races ahead of the DB initialization step; a test forgets to initialize the DataSource; a reconnection path queries during the window the pool is null; shutdown logic still runs queries after destroy.","solutions":["Ensure DataSource.initialize() (or the n8n DB init lifecycle) has fully resolved before any query path can run; gate query-issuing services on the init promise.","Avoid calling DB methods in afterDestroy / shutdown hooks; check an 'isInitialized' flag first.","If using a custom connect flow, verify this.master is assigned on the success path and that connect() did not swallow an error.","In tests, use beforeAll to await initialization and afterAll to destroy, and never share a destroyed DataSource across tests."],"exampleFix":"// before\nconst ds = new DataSource({...});\nconst repo = ds.getRepository(User);\nawait repo.find();  // ds.initialize() never awaited\n// after\nconst ds = new DataSource({...});\nawait ds.initialize();\nconst repo = ds.getRepository(User);\nawait repo.find();\n","handlingStrategy":"validation","validationCode":"if (!dataSource.isInitialized) {\n  throw new Error('DataSource is not initialized; call await dataSource.initialize() first');\n}\n","typeGuard":"function isDriverConnected(driver: unknown): boolean {\n  return !!driver && typeof (driver as any).master === 'object' && (driver as any).master !== null;\n}\n","tryCatchPattern":"import { TypeORMError } from '@n8n/typeorm';\ntry {\n  await driver.obtainMasterConnection();\n} catch (e) {\n  if (e instanceof TypeORMError && /Driver not Connected/.test(e.message)) {\n    throw new Error('DB not initialized yet; gate this call on the init lifecycle');\n  }\n  throw e;\n}\n","preventionTips":["Gate every query-issuing service on the resolved DB init promise.","Never issue queries in constructors or before init resolves.","Stop issuing queries once destroy() has run.","Add a startup readiness probe that blocks traffic until the pool is up."],"tags":["typeorm","postgres","database","lifecycle","connection-pool"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}