Creating the Database

Building an app with Phoenix : part 3 of 9 published on Aug 24, 2015

To create the database, we need to run the ecto.create task with mix.

$ mix ecto.create

You will probably get the error message:

** (exit) exited in:<0.142.0>, {:query, "CREATE TABLE IF NOT EXISTS \"schema_migrations\" (\"version\" bigint PRIMARY KEY, \"inserted_at\" timestamp)", []}, :infinity)
    ** (EXIT) %Postgrex.Error{message: nil, postgres: %{code: :invalid_authorization_specification, file: "miscinit.c", line: "362", message: "role \"postgres\" does not exist", pg_code: "28000", routine: "InitializeSessionUserId", severity: "FATAL"}}
    (elixir) lib/gen_server.ex:356:
    (postgrex) lib/postgrex/connection.ex:102: Postgrex.Connection.query/4
    (ecto) lib/ecto/adapters/postgres/connection.ex:31: Ecto.Adapters.Postgres.Connection.query/4
    (ecto) lib/ecto/adapters/sql.ex:242: Ecto.Adapters.SQL.query/7
    (ecto) lib/ecto/pool.ex:159: Ecto.Pool.do_run/4
    (ecto) lib/ecto/adapters/sql.ex:230: Ecto.Adapters.SQL.query/6
    (ecto) lib/ecto/adapters/sql.ex:208: Ecto.Adapters.SQL.query/5
    (ecto) lib/ecto/adapters/sql.ex:169: Ecto.Adapters.SQL.query!/5

This is because Phoenix by default expects the local development Postgres database to have a user postgres with a password postgres. But we can change this configuration in our dev.exs file.

In the /config folder of our application we will find our configuration files for our different environments (dev, test, and prod).

To fix this issue we will edit the dev.exs to no longer require username and password for working with our database.

# /config/dev.exs
# Configure your database
config :readdit, Readdit.Repo,
  adapter: Ecto.Adapters.Postgres,
  # remove these lines
  # username: "postgres",
  # password: "postgres",
  database: "readdit_dev",
  size: 10 # The amount of database connections in the pool

Now we can run our task again and this time without any errors.

$ mix ecto.create
The database for Readdit.Repo has been created.
Next: We begin with Posts