Skip to content
Latchkey

Flyway "Unable to obtain connection from database" in CI

Flyway could not open a JDBC connection to the database. The driver never reached a usable server - a connectivity, readiness, or URL problem, frequently a service container that has not finished starting.

What this error means

flyway migrate/info fails up front with "Unable to obtain connection from database", wrapping a JDBC "Connection refused" or timeout. It frequently passes on retry once the database service is up.

flyway
ERROR: Unable to obtain connection from database
(jdbc:postgresql://db:5432/app) for user 'app':
Connection to db:5432 refused.

Common causes

Database service not ready

A database service container is still starting when Flyway runs, so the JDBC connection is refused. Transient.

Wrong JDBC URL or credentials

An incorrect host/port in the JDBC URL, or wrong user/password, prevents the connection entirely.

Transient network blip

A brief connectivity drop to a remote database can fail a connection that succeeds on retry.

How to fix it

Let Flyway retry the connection

connectRetries makes Flyway wait out a slow-starting database.

Terminal
flyway -connectRetries=10 -connectRetriesInterval=5 migrate

Wait for readiness, then migrate

Terminal
until pg_isready -h db -p 5432; do sleep 1; done
flyway -url=jdbc:postgresql://db:5432/app migrate

How to prevent it

  • Use connectRetries so Flyway tolerates a slow-starting database.
  • Keep the JDBC URL and credentials in sync with the CI network.
  • On managed runners (Latchkey), self-healing auto-retries transient failures such as a database slow to accept connections.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →