【问题标题】:Why does Phoenix (ecto/Postgresx) fail to Connect in dev为什么 Phoenix(ecto/Postgres)无法在开发中连接
【发布时间】:2016-12-29 16:56:29
【问题描述】:

我正在开始我的 Elixir/Phoenix 之旅,但我的 postgres 连接遇到了一些问题。

当我启动我的服务器时,我得到:

 $ mix phoenix.server
 [error] Postgrex.Protocol (#PID<0.214.0>) failed to connect: ** (Postgrex.Error) tcp connect: connection refused - :econnrefused
 [error] Postgrex.Protocol (#PID<0.217.0>) failed to connect: ** (Postgrex.Error) tcp connect: connection refused - :econnrefused
 [error] Postgrex.Protocol (#PID<0.218.0>) failed to connect: ** (Postgrex.Error) tcp connect: connection refused - :econnrefused
 [error] Postgrex.Protocol (#PID<0.211.0>) failed to connect: ** (Postgrex.Error) tcp connect: connection refused - :econnrefused
 [error] Postgrex.Protocol (#PID<0.215.0>) failed to connect: ** (Postgrex.Error) tcp connect: connection refused - :econnrefused
 [error] Postgrex.Protocol (#PID<0.219.0>) failed to connect: ** (Postgrex.Error) tcp connect: connection refused - :econnrefused
 [error] Postgrex.Protocol (#PID<0.216.0>) failed to connect: ** (Postgrex.Error) tcp connect: connection refused - :econnrefused
 [error] Postgrex.Protocol (#PID<0.213.0>) failed to connect: ** (Postgrex.Error) tcp connect: connection refused - :econnrefused
 [error] Postgrex.Protocol (#PID<0.212.0>) failed to connect: ** (Postgrex.Error) tcp connect: connection refused - :econnrefused
 [error] Postgrex.Protocol (#PID<0.210.0>) failed to connect: ** (Postgrex.Error) tcp connect: connection refused - :econnrefused
 [info] Running Rumbl.Endpoint with Cowboy using http://localhost:4000
 [error] Postgrex.Protocol (#PID<0.215.0>) failed to connect: ** (Postgrex.Error) tcp connect: connection refused - :econnrefused

作为 Elixir、Phoenix 和 Ecto 的新手,我不清楚如何调试此问题。任何关于我的问题是什么或如何调试它的建议将不胜感激。

我的应用已设置

我有一个基本的应用程序

mix phoenix.new rumbl
cd rumbl
mix deps.get
mix deps.compile

我的 config/dev.exs 有以下数据库设置

# Configure your database
config :rumbl, Rumbl.Repo,
  adapter: Ecto.Adapters.Postgres,
  username: "elixir",
  database: "rumbl_dev",
  hostname: "localhost",
  pool_size: 10

当我运行 mix ecto.create 时没有错误,当我在 psql 中运行 \l 时,我可以看到 rumbl_dev。它也归 elixir 用户所有。

运行 mix ecto.migrate 会引发相同的连接错误

我的 pg_hba.conf 文件有以下内容

local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust

这是我使用 psql 登录时看到的内容

$ psql --dbname=rumbl_dev --username=elixir --host=127.0.0.1 --password
Password for user elixir:
psql (9.4.5)
Type "help" for help.

rumbl_dev=>

【问题讨论】:

  • 您可以尝试将密码添加到配置中,看看是否有帮助。 config :rumbl, Rumbl.Repo, adapter: Ecto.Adapters.Postgres, username: "elixir", database: "rumbl_dev", hostname: "localhost", password: "***password***" pool_size: 10
  • 我也尝试过,但得到了相同的结果 - 感谢@stephen_m 的建议
  • 您可以尝试从终端运行以下命令吗:psql --dbname=rumbl_dev --username=elixir --host=127.0.0.1 --password
  • hostname: "localhost"hostname: "127.0.0.1" 可能会有所帮助。
  • @stephen_m 我将结果附加到我的问题中。它让我没有问题。

标签: elixir phoenix-framework ecto boxen


【解决方案1】:

这发生在我身上两次,在 ma​​cOS 崩溃之后。

最近我使用this

修复 1

  1. 运行postgres -D /usr/local/var/postgres
  2. 如果您看到类似这样的内容,请复制PID 后面的数字:

    FATAL:  lock file "postmaster.pid" already exists 
    HINT:   Is another postmaster (PID 379) running in data directory "/usr/local/var/postgres"?
    
  3. kill -9 PID 用进程 # 代替 PID


以前为我工作的steps

修复 2

brew update
brew upgrade
brew postgresql-upgrade-database

【讨论】:

  • Fix 1 对我有用(在 macOS 崩溃后)。谢谢。
  • 这里也一样。我的 macOS 崩溃了。似乎最近发生了。回到 postgres 给出了同样的问题,这个 Fix 1 为我修复了它。接受的答案似乎是一个不同的问题。
【解决方案2】:

好的,所以我想通了。最后,这是我的一个简单错误。虽然很容易制作。

我在我的 Mac 上使用 Boxen,它出于某种原因将端口更改为 15432

如果mix ecto.create 失败了,我可能会更早地找到它。不知道为什么会这样。

希望这将在未来对其他人有所帮助

【讨论】:

  • 很高兴它成功了。不知道为什么mix ecto.create 部分有效。您也可以在控制台上检查端口psql --dbname=rumbl_dev --username=elixir --host=127.0.0.1 --port=5432 --password
  • @stephen_m - 它在端口 = 5432 的控制台上不起作用。奇怪的是,创建工作。
  • 太棒了!非常感谢,我也在用boxen。在config.dev.exs 上指定port: 15432 解决了它。
【解决方案3】:
  • 在 postgresql 中删除数据库 rumbl_dev 以重新开始。
  • 如果你愿意,你可以在本地主机上尝试 md5 auth 的开发版本,方法是将以下行添加到 pg_hba.conf

    # host DATABASE USER ADDRESS METHOD
    
    host rumbl_dev elixir localhost md5
    

    注意:有关设置/etc/postsgresql/9.4/pg_hba.conf 的更多信息,请参阅此处并根据需要更改设置。

  • 将完整的设置和密码添加到dev.exs

    config :rumbl, Rumbl.Repo, 
    adapter: Ecto.Adapters.Postgres, 
    username: "elixir", 
    database: "rumbl_dev", 
    hostname: "localhost", 
    password: "***password***",
    pool_size: 10
    
  • 尝试运行mix do ecto.create, ecto.migrate,看看效果如何。

希望这有帮助,如果没有,我在这里没有想法。

【讨论】:

  • 同样的结果恐怕:(
【解决方案4】:

您需要在配置块中包含数据库用户的密码。

config :rumbl, Rumbl.Repo,
  adapter: Ecto.Adapters.Postgres,
  username: "elixir",
  password: "???",
  database: "rumbl_dev",
  hostname: "localhost",
  pool_size: 10

【讨论】:

    【解决方案5】:

    每次我在调用mix phoenix.server 之前不启动 Postgres 时都会遇到同样的问题。我使用https://postgresapp.com 来启动它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多