TypeORM "DataSource is not initialized" in CI
TypeORM requires AppDataSource.initialize() to resolve before any query or migration runs. If code touches the DataSource first, or initialize threw, TypeORM reports that the connection is not established.
What this error means
A migration or seed step fails with "Connection is not established with ... database" or "DataSource is not initialized", typically right after startup in CI.
Error: Connection is not established with postgres database
at DataSource.query (.../DataSource.js)Common causes
Querying before initialize() completes
Code calls a repository or query before awaiting AppDataSource.initialize(), so there is no live connection yet.
initialize() failed silently earlier
The connection attempt threw (bad credentials, unreachable host) but the rejection was not awaited, so a later call reports the connection as not established.
How to fix it
Await initialize() before using the DataSource
Make sure initialization is awaited and any error surfaces before queries run.
await AppDataSource.initialize();
await AppDataSource.runMigrations();Check the initialize error first
- Wrap
initialize()in try/catch and log the real cause. - Confirm host, port, and credentials reach the CI database service.
- Only run migrations after initialize resolves successfully.
How to prevent it
- Always
await initialize()and handle its rejection. - Verify connection settings against the CI database service.
- Run migrations only inside the initialized DataSource lifecycle.