'mix ecto.create' errors with: role "postgres" does not exist

Just posting this here in case it helps anyone else in future.

on mix ecto.create I was getting:

00:54:48.033 [error] GenServer #PID<0.214.0> terminating
** (Postgrex.Error) FATAL 28000 (invalid_authorization_specification) role "postgres" does not exist
    (db_connection) lib/db_connection/connection.ex:84: DBConnection.Connection.connect/2
    (connection) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
** (Mix) The database for Hello.Repo couldn't be created: killed

and psql was reporting:

psql: FATAL:  database "Aston" does not exist

See answer belowā€¦

7 Likes

Fixed with:

createdb 'Aston' 
psql

(Change Aston to whatever the psql command in the post above said was missing)

Then:

CREATE USER postgres SUPERUSER;
CREATE DATABASE postgres WITH OWNER postgres;
exit

(Although it told me the postgres db already exists - something you can check with psql -l)

mix ecto.create
$ The database for Hello.Repo has been created

:smiley:

17 Likes

Thatā€™s kind of weird. PostgreSQL should always come with a created postgres user. Iā€™d wager on a broken installation.

4 Likes

Yeah something weird is going on. I havenā€™t done a clean install for a while, just update after (MacOS) update - I generally hate settling with updates as things always break and start to feel sluggish after a while. But Apple being Apple have made clean installs harder - last time a clean install totally broke my email settings and I had to report it as a bug to get an answer on fixing it! :icon_eek: Iā€™ll only do a clean install now if I have a week or two spare :lol:

Reason I posted the thread here is because when searching I noticed someone had commented they got the same error after running mix ecto.create too! So thought having it more easily and directly google-able might help others in future :smiley:

3 Likes

Agreed, but the Homebrew installation creates a SQL user named after your OS user and no ā€œpostgresā€ user to go with it. Just something to document and be aware of like @AstonJ has done here :+1:

3 Likes

Ah that explains why when using Rails it works fine - because I guess they use your account name user rather than ā€˜postgresā€™.

I wonder if Ecto could detect whether youā€™re using OSX and do the same? Otherwise every Mac user who have installed PG via homebrew will get the same.

Alternatively maybe hombrew could create the postgres role on installation too?

So for me the disconnect is that the generators hard-code the literal value of ā€œpostgresā€ for username on the Ecto connection, but really what it needs is a SQL user that is a superuser or that has DB creation rights. I would prefer some explicitness/agnosticism in that direction be included in the Phoenix generator output, and/or in the Hex docs, instead of assuming Iā€™ve left the default ā€œpostgresā€ user intact and with superuser privileges.

I donā€™t do that on my dev workstation or my production DB either, so I wind up using a Repo.init/2 callback and a DATABASE_URL basically immediately on new projects.

1 Like

Not sure. I installed PG through homebrew as well and I seem to remember it having the postgres user and a fresh Phoenix app working right away. But then again, that was a while ago, canā€™t swear the details on my life.

As for the Ecto default, youā€™re not wrong but then again they apply for local-only config so I see no harm done.

1 Like

But if youā€™re working on a shared project, youā€™re probably better off with everyone using the postgres user locally, or not setting one at all (which I think will default to the logged in user)

1 Like

Hi guys! I got the same error:

16:05:28.295 [error] GenServer #PID<0.220.0> terminating
** (Postgrex.Error) FATAL 28000 (invalid_authorization_specification) no PostgreSQL user name specified in startup packet
    (db_connection) lib/db_connection/connection.ex:84: DBConnection.Connection.connect/2
    (connection) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
** (Mix) The database for Getaways.Repo couldn't be created: killed

and fixed it. It was due to the config on the dev.exs file (at the end of it):

Originally:

# Configure your database
config :getaways, Getaways.Repo,
  username: "postgres",
  password: "postgres",
  database: "getaways_dev",
  hostname: "localhost",
  pool_size: 10

Fixed:

# Configure your database
config :getaways, Getaways.Repo,
  username: "yourcomputeruser",
  password: "yourcomputeruser",
  database: "getaways_dev",
  hostname: "localhost",
  pool_size: 10

The setup by default is ā€œpostgresā€ => updated them to my computer user.

I hope it helps!

9 Likes

Iā€™m thinking you need to have a look at your pg_hba.conf and make sure you are actually permitted to log into Postgres.

Have you tried getting in simply with psql -U postgres?

3 Likes

From here: https://stackoverflow.com/questions/15301826/psql-fatal-role-postgres-does-not-exist#comment-91332745

/usr/local/opt/postgres/bin/createuser -s postgres

Fixed it for me. The problem is homebrew doesnā€™t install the postgres user.

12 Likes

2 posts were split to a new topic: Where can I see my Postgres database?

Got the same issue with PostgreSQL on macOS installed from brew.
Resolved with the next config:

config :p_core, PCore.Repo,
  username: System.cmd("whoami", [])
            |> elem(0)
            |> String.trim(),
  database: "p_core_dev",
  hostname: "localhost",
  show_sensitive_data_on_connection_error: true,
  pool_size: 10
3 Likes

Thank you, this ended up being my issue!

1 Like

This ended up being my issue as well on Mac OS - I needed to replace user / password with my local machine credentials.

2 Likes

Just create a user:
createuser --superuser postgres

check with the psql:

$ psql default
psql (14.1)
Type "help" for help.

default=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB                          | {}
 my_username | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
4 Likes

A couple years later, this still works. Thanks :slight_smile:

3 Likes

Same here. I just altered dev.exs to use my MacOS username

1 Like