Flyway "Unable to obtain connection from database" in CI
Flyway tried to open a JDBC connection to the configured URL and the driver could not establish it. In CI this is almost always a database that is not ready yet, or a wrong JDBC URL, user, or driver.
What this error means
flyway migrate fails with "Unable to obtain connection from database (jdbc:postgresql://...): Connection refused" before reading any migrations.
flyway output
ERROR: Unable to obtain connection from database (jdbc:postgresql://localhost:5432/app)
for user 'app': Connection to localhost:5432 refused.
SQL State : 08001Common causes
The database is not accepting connections yet
Flyway runs before the database service container is ready, so the JDBC connection is refused.
Wrong JDBC URL, credentials, or missing driver
The flyway.url, user, or password is incorrect, or the JDBC driver for the database is not on the classpath.
How to fix it
Wait for the database before running Flyway
Use a health check so migrate starts only when the database is listening.
.github/workflows/ci.yml
services:
postgres:
image: postgres:16
env: { POSTGRES_PASSWORD: postgres }
options: >-
--health-cmd "pg_isready -U postgres"
--health-interval 5s --health-retries 10Verify the JDBC URL and credentials
- Confirm
flyway.urlhost and port match the running database. - Check the user and password are correct and the database exists.
- Ensure the right JDBC driver is available to Flyway.
Terminal
flyway -url=jdbc:postgresql://localhost:5432/app -user=app -password=secret migrateHow to prevent it
- Gate Flyway behind a database health check.
- Source the JDBC URL and credentials from CI config consistently.
- Make sure the database driver is on the classpath.
Related guides
Flyway "Found non-empty schema(s) without schema history table" in CIFix Flyway "Found non-empty schema(s) ... but no schema history table" in CI - Flyway refuses to migrate an e…
Liquibase "Could not acquire change log lock" in CIFix Liquibase "Could not acquire change log lock. Currently locked by ..." in CI - a previous run died holdin…
Prisma P1001 "Can't reach database server" in CIFix Prisma P1001 "Can't reach database server at host:port" in CI - migrate could not open a TCP connection t…