Create database during release

I’m trying to do deploy an elixir umbrella app with elixir 1.9+(using mix release). I’ve gotten to the point where I’m running a migration. The error is that the db doesn’t exist yet. How do I create the database programmatically during the release?

(similar process to https://elixirschool.com/blog/releasing-an-umbrella-app-with-docker-and-mix-release/)

docker run -p 4000:4000 -p 5432:5432 --env DATABASE_URL=ecto://postgres:postgres@postgres/registrar_dev --env SECRET_KEY_BASE=bazbazbaz mhanna/registrar:latest

Starting app from bash script...
running env.sh.eex...
Beginning migration script...
running env.sh.eex...
Starting dependencies..
Starting repos..
Running migrations for registrar repo: Elixir.Registrar.Repo migrations_path: /app/lib/registrar-0.1.0/priv/repo/migrations
03:35:46.550 [error] Postgrex.Protocol (#PID<0.223.0>) failed to connect:  (DBConnection.ConnectionError) tcp connect (postgres:5432): non-existing domain - :nxdomain
03:35:46.550 [error] Postgrex.Protocol (#PID<0.222.0>) failed to connect:  (DBConnection.ConnectionError) tcp connect (postgres:5432): non-existing domain - :nxdomain
03:35:48.690 [error] Postgrex.Protocol (#PID<0.222.0>) failed to connect:  (DBConnection.ConnectionError) tcp connect (postgres:5432): non-existing domain - :nxdomain
03:35:49.109 [error] Postgrex.Protocol (#PID<0.223.0>) failed to connect:  (DBConnection.ConnectionError) tcp connect (postgres:5432): non-existing domain - :nxdomain
03:35:49.525 [error] Could not create schema migrations table. This error usually happens due to the following:

  * The database does not exist
  * The "schema_migrations" table, which Ecto uses for managing
    migrations, was defined by another library
  * There is a deadlock while migrating (such as using concurrent
    indexes with a migration_lock)

To fix the first issue, run "mix ecto.create".

The Ecto.Adapter.Storage.storage_up/1 should be of help

    opts = Application.get_env(:myapp, MyApp.Repo)
    MyApp.Repo.__adapter__().storage_up(opts)
1 Like

Usually manually, so the app doesn‘t need credentials, which allow the creation of tables.

3 Likes

Can we have the credentials on the prod.secret.exs file which the app can access?

You can as well create a database role with only the given permissions on the database and use that on the prod.secret.exs if you dont want to give full access to the application

when the container was up I did:
docker exec -ti registrar_umbrella_postgres_1 bash
and then psql, created the database.

On next run I got past the issue.