Skip to content
Latchkey

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)
** (Mix) The database for MyApp.Repo couldn't be created: connection refused
    tcp connect (localhost:5432): connection refused

Common 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

  1. Set MIX_ENV: test for the ecto and test steps.
  2. Ensure the Postgres service block has a health check so the runner waits.
  3. Run mix ecto.create then mix ecto.migrate.
.github/workflows/ci.yml
- run: |
    mix ecto.create
    mix ecto.migrate
  env:
    MIX_ENV: test

Match credentials to the service

Use the service's POSTGRES_USER/POSTGRES_PASSWORD in test.exs (or a DATABASE_URL) so ecto.create authenticates.

config/test.exs
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.

Frequently asked questions

What causes ""mix ecto.create" failed"?
Without MIX_ENV=test, ecto.create reads dev config and targets a database or credentials that do not exist in CI.
How do I fix "mix ecto.create" failed?
Run ecto.create with MIX_ENV=test after the DB is ready

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card