如何解决Elixir Phoenix和 "角色postgres不存在"的问题

100 阅读1分钟

如果你试图按照Elixir Phoenix Up and Running指南进行操作,你可能会直接遇到这个错误,如果你使用homebrew的Postgres安装,当你运行时mix ecto.create

$ mix ecto.create
18:54:52.762 [error] GenServer #PID<0.292.0> terminating
** (Postgrex.Error) FATAL 28000 (invalid_authorization_specification) role "postgres" does not exist
    (db_connection 2.4.0) lib/db_connection/connection.ex:100: DBConnection.Connection.connect/2
    (connection 1.1.0) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib 3.15.2) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol

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

这并不理想!该堆栈跟踪错误的核心问题是role "postgres" does not exist

有几种方法可以解决这个问题。

一:修改Phoenix的配置,使之与自制软件相匹配

打开config/dev.exs ,把配置改成你的用户名和一个空白密码。

默认情况下,Phoenix假定 "postgres "为用户名和密码。

config :hello, Hello.Repo,
  username: "sdball",
  password: "",
  database: "hello_dev",
  hostname: "localhost",
  show_sensitive_data_on_connection_error: true,
  pool_size: 10

二:修改postgres的配置以与Phoenix相匹配

  1. 连接到postgres
$ psql -d postgres
  1. 创建 "postgres "用户
CREATE ROLE postgres LOGIN CREATEDB;
  1. (ctrl-d)关闭与postgres的连接

事情应该开始工作了!

在这两种方法中,你都应该能够创建postgres数据库:

$ mix ecto.create

The database for Hello.Repo has been created