Elixir "mix ecto.create" failed in CI
mix ecto.create builds the database for the current MIX_ENV. In CI it fails when it runs against the wrong environment, before the Postgres service is ready, or with credentials that do not match the service.
What this error means
The ecto.create step fails with a Postgrex connection error, "database ... already exists", or a permission error, and tests never run.
** (Mix) The database for MyApp.Repo couldn't be created: connection refused
tcp connect (localhost:5432): connection refusedCommon causes
MIX_ENV is not test for the DB steps
Without MIX_ENV=test, ecto.create reads dev config and targets a database or credentials that do not exist in CI.
DB service not ready when ecto.create runs
The step executes before the Postgres service passes its health check, so the connection is refused.
How to fix it
Run ecto.create with MIX_ENV=test after the DB is ready
- Set
MIX_ENV: testfor the ecto and test steps. - Ensure the Postgres service block has a health check so the runner waits.
- Run
mix ecto.createthenmix ecto.migrate.
- run: |
mix ecto.create
mix ecto.migrate
env:
MIX_ENV: testMatch credentials to the service
Use the service's POSTGRES_USER/POSTGRES_PASSWORD in test.exs (or a DATABASE_URL) so ecto.create authenticates.
config :my_app, MyApp.Repo,
username: "postgres",
password: "postgres",
hostname: "localhost",
database: "my_app_test"How to prevent it
- Always set MIX_ENV=test for ecto and test steps.
- Health-check the Postgres service so create waits for readiness.
- Keep test.exs credentials aligned with the service container.