n8n-io/n8n · critical · TypeORMError

Driver not Connected

Error message

Driver not Connected

What it means

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).

Source

Thrown at packages/@n8n/typeorm/src/driver/postgres/PostgresDriver.ts:1052

				type = `${column.type}(${column.spatialFeatureType})`;
			} else {
				type = column.type;
			}
		}

		if (column.isArray) type += ' array';

		return type;
	}

	/**
	 * Obtains a new database connection to a master server.
	 * Used for replication.
	 * If replication is not setup then returns default connection's database connection.
	 */
	async obtainMasterConnection(): Promise<[any, Function]> {
		if (!this.master) {
			throw new TypeORMError('Driver not Connected');
		}

		const connection = await this.master.connect();
		// Apply per-connection session settings (schema creation moved to afterConnect)
		const { schema, statementTimeout } = this.options;
		if (schema && schema !== 'public') {
			await connection.query(`SET search_path TO "${schema}",public`);
		} else {
			await connection.query('SET search_path TO public');
		}
		if (statementTimeout) {
			await connection.query(`SET statement_timeout = ${statementTimeout}`);
		}

		return [connection, () => connection.release()];
	}

	/**

View on GitHub (pinned to 5ac6606e81)

Solutions

  1. 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.
  2. Avoid calling DB methods in afterDestroy / shutdown hooks; check an 'isInitialized' flag first.
  3. If using a custom connect flow, verify this.master is assigned on the success path and that connect() did not swallow an error.
  4. In tests, use beforeAll to await initialization and afterAll to destroy, and never share a destroyed DataSource across tests.

Example fix

// before
const ds = new DataSource({...});
const repo = ds.getRepository(User);
await repo.find();  // ds.initialize() never awaited
// after
const ds = new DataSource({...});
await ds.initialize();
const repo = ds.getRepository(User);
await repo.find();
Defensive patterns

Strategy: validation

Validate before calling

if (!dataSource.isInitialized) {
  throw new Error('DataSource is not initialized; call await dataSource.initialize() first');
}

Type guard

function isDriverConnected(driver: unknown): boolean {
  return !!driver && typeof (driver as any).master === 'object' && (driver as any).master !== null;
}

Try / catch

import { TypeORMError } from '@n8n/typeorm';
try {
  await driver.obtainMasterConnection();
} catch (e) {
  if (e instanceof TypeORMError && /Driver not Connected/.test(e.message)) {
    throw new Error('DB not initialized yet; gate this call on the init lifecycle');
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of n8n-io/n8n@5ac6606e81 (2026-08-12). Data as JSON: /api/errors/639689581acd781b. Report an issue: GitHub.