Flyway "Unable to obtain connection from database" in CI
By Daniel Zoghalchali·Latchkey
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.
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.
Frequently asked questions
What causes ""Unable to obtain connection from database""?
A database service container is still starting when Flyway runs, so the JDBC connection is refused. Transient.
How do I fix "Unable to obtain connection from database"?
connectRetries makes Flyway wait out a slow-starting database.
Can Latchkey fix this automatically?
Yes. Latchkey runs your GitHub Actions on managed runners that detect this failure, apply the fix, and retry the job automatically - self-healing is on by default.