Failing to mix ecto.create because of missing username


#1

Hi!

Trying to setup an Elixir + Phoenix + Ecto + PG project under CI, but my build stops at mix ecto.create. My config is:

$ cat .circleci/config.yml
version: 2
jobs:
  build:
    working_directory: ~/bus-chat-erl
    docker:
      - image: elixir:1.4.2
      - image: postgres:9.6.2
        environment:
          POSTGRES_USER: ubuntu
    steps:
      - checkout
      - run: mix local.hex --force
      - run: mix local.rebar --force
      - run: mix deps.get
      - run: mix ecto.create
      - run: MIX_ENV=test mix do compile --warnings-as-errors, test

$ cat config/test.exs
use Mix.Config

# We don't run a server during test. If one is required,
# you can enable the server option below.
config :bus_chat_web, BusChatWeb.Endpoint,
  http: [port: 4001],
  server: false

# Print only warnings and errors during test
config :logger, level: :warn

# Configure your database
database = case System.get_env("CIRCLECI") do
  nil -> "sharethebus_test"
  _ -> "ubuntu"
end

username = case System.get_env("CIRCLECI") do
  nil -> System.get_env("USER")
  _ -> "ubuntu"
end

IO.puts "Getting ready to configure Ecto DB"

config :bus_chat_web, BusChatWeb.Repo,
  adapter: Ecto.Adapters.Postgres,
  hostname: "localhost",
  database: database,
  username: username,
  password: "",
  pool: Ecto.Adapters.SQL.Sandbox,
  pool_size: 4

The error I get is:

mix ecto.create
Shell: /bin/bash -eo pipefail

==> connection
Compiling 1 file (.ex)
Generated connection app
==> fs (compile)
Compiled src/fs_event_bridge.erl
Compiled src/fs_app.erl
Compiled src/fs.erl
Compiled src/fs_server.erl
Compiled src/fs_sup.erl
Compiled src/sys/inotifywait_win32.erl
Compiled src/sys/inotifywait.erl
Compiled src/sys/fsevents.erl
==> gettext
Compiling 1 file (.yrl)
Compiling 1 file (.erl)
Compiling 20 files (.ex)
Generated gettext app
===> Compiling ranch
==> poolboy (compile)
Compiled src/poolboy_worker.erl
Compiled src/poolboy_sup.erl
Compiled src/poolboy.erl
==> decimal
Compiling 1 file (.ex)
Generated decimal app
==> poison
Compiling 4 files (.ex)
warning: HashDict.size/1 is deprecated, use maps and the Map module instead
  lib/poison/encoder.ex:283

Generated poison app
==> db_connection
Compiling 23 files (.ex)
Generated db_connection app
==> phoenix_pubsub
Compiling 12 files (.ex)
Generated phoenix_pubsub app
===> Compiling cowlib
src/cow_multipart.erl:392: Warning: crypto:rand_bytes/1 is deprecated and will be removed in a future release; use crypto:strong_rand_bytes/1

===> Compiling cowboy
==> uuid
Compiling 1 file (.ex)
Generated uuid app
==> mime
Compiling 1 file (.ex)
Generated mime app
==> plug
Compiling 44 files (.ex)
Generated plug app
==> phoenix_html
Compiling 8 files (.ex)
Generated phoenix_html app
==> phoenix
Compiling 60 files (.ex)
Generated phoenix app
==> phoenix_live_reload
Compiling 3 files (.ex)
Generated phoenix_live_reload app
==> postgrex
Compiling 62 files (.ex)
Generated postgrex app
==> ecto
Compiling 69 files (.ex)
Generated ecto app
==> phoenix_ecto
Compiling 4 files (.ex)
Generated phoenix_ecto app
==> bus_chat_web
Compiling 15 files (.ex)
Generated bus_chat_web app
** (Mix) The database for BusChatWeb.Repo couldn't be created: an exception was raised:
    ** (KeyError) key :username not found in: [types: Postgrex.DefaultTypes, backoff_type: :stop, pool: DBConnection.Connection, database: "template1", otp_app: :bus_chat_web, repo: BusChatWeb.Repo, migration_source: "schema_migrations_bus_chat_web", adapter: Ecto.Adapters.Postgres, hostname: "localhost", password: "", pool_size: 10]
        (elixir) lib/keyword.ex:343: Keyword.fetch!/2
        (postgrex) lib/postgrex/protocol.ex:548: Postgrex.Protocol.startup/2
        (postgrex) lib/postgrex/protocol.ex:475: Postgrex.Protocol.handshake/2
        (db_connection) lib/db_connection/connection.ex:134: DBConnection.Connection.connect/2
        (connection) lib/connection.ex:622: Connection.enter_connect/5
        (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3

14:33:45.924 [error] GenServer #PID<0.3241.0> terminating
** (KeyError) key :username not found in: [types: Postgrex.DefaultTypes, backoff_type: :stop, pool: DBConnection.Connection, database: "template1", otp_app: :bus_chat_web, repo: BusChatWeb.Repo, migration_source: "schema_migrations_bus_chat_web", adapter: Ecto.Adapters.Postgres, hostname: "localhost", password: "", pool_size: 10]
    (elixir) lib/keyword.ex:343: Keyword.fetch!/2
    (postgrex) lib/postgrex/protocol.ex:548: Postgrex.Protocol.startup/2
    (postgrex) lib/postgrex/protocol.ex:475: Postgrex.Protocol.handshake/2
    (db_connection) lib/db_connection/connection.ex:134: DBConnection.Connection.connect/2
    (connection) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
Exited with code 1

I can see that username is unspecified in the structure that is present there, but my config does specify the user. What am I doing wrong?

Thanks!
François


#2

Are you connecting via TCP? If not, you should, because there’s no Unix socket.

If you’re already using TCP, can you try specifying a password, too?


#3

In the config I setup in test.exs, I do pass a username key. The error message is complaining that there is no username key. The config I setup is not being read by ecto.create. Where was that config read from? That’s what I’m trying to answer.


#4

I wouldn’t know how to configure your app, but it’s definitely not using the config you specified above. For example, the error says the pool size is 10, but your config dictates 4.

I recommend rebuilding with SSH and logging in to poke around.


#5

@francois

I notice this line:

IO.puts "Getting ready to configure Ecto DB"

But I don’t see that string in the build output. Do you see this line when you set your build up locally?


#6

Was a solution ever found for this? I’m running into the same problem.


#7

Im having the same issue… Couldn’t find a solution yet


#8

Adding MIX_ENV=test before ecto.create fixed this for me.

version: 2
jobs:
  build:
    working_directory: ~/bus-chat-erl
    docker:
      - image: elixir:1.5.2
      - image: circleci/postgres:10.1
        environment:
        - POSTGRES_USER: postgres
    steps:
      - checkout
      - run: mix local.hex --force
      - run: mix local.rebar
      - run: mix deps.get
      - run: MIX_ENV=test mix ecto.create
      - run: mix test

#9