N00b ecto problem - database "postgres" does not exist

So I’ve got a brand new ecto repo, and I’m following the instructions here: https://hexdocs.pm/ecto/getting-started.html.

After configuring everything like so:

use Mix.Config

config :osdi, Osdi.Repo,
  adapter: Ecto.Adapters.Postgres,
  database: "osdi_repo",
  username: "postgres",
  password: "postgres",
  hostname: "localhost"

config :osdi, ecto_repos: [Osdi.Repo]

And running

mix ecto.create

I get:

** (Mix) The database for Osdi.Repo couldn’t be created: FATAL 3D000 (invalid_catalog_name): database “postgres” does not exist

18:06:39.552 [error] GenServer #PID<0.152.0> terminating
** (Postgrex.Error) FATAL 3D000 (invalid_catalog_name): database "postgres" does not exist
    (db_connection) lib/db_connection/connection.ex:148: 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

After psqling and running create database osdi_repo;, I got the same result. After psqlling and running create database postgres;, I got:

The database for Osdi.Repo has already been created

Why is ecto / postgrex trying to create the database postgres instead of osdi_repo? Even if I change username and password to not be postgres, it still tries to create postgres, even after cleaning and recompiling.

Any ideas?

I’m using ecto ~> 2.0 and postgrex ~> 0.13.3

Maybe try to create postgres database first, then You could run your migration…

$ createdb postgres

(If You are on *nix)

BTW ecto 2.0 is not the latest

See latest ecto release annoucement

Thanks – I upgraded, still have the problem.

Yeah, I can create the database directly and everything works, but I don’t know why it’s trying to connect to postgres instead of what I put in config.exs

I think it is postgres related, not ecto, nor postgrex

You need to have a db corresponding to your user account… I don’t remember why

Oh yeah, I need a database postgres to match my user, and then mix ecto.create creates a database osdi_repo. Oh ok!

config :osdi, Osdi.Repo,
  adapter: Ecto.Adapters.Postgres,
  database: "osdi_repo"

config :osdi, ecto_repos: [Osdi.Repo]

You can omit unnecessary options in the config list.

@ben-pr-p have a look https://github.com/cockroachdb/cockroach/issues/23893#issuecomment-381357481