【问题标题】:mix ecto.create can't connect to Postgres even though Postgres is running and the credentials are correct即使 Postgres 正在运行并且凭据正确,混合 ecto.create 也无法连接到 Postgres
【发布时间】:2021-01-01 03:33:35
【问题描述】:

我创建了一个新的 Phoenix 项目并检查了 config/dev.exs 中的凭据,它们是:

config :blog, Blog.Repo,
  adapter: Ecto.Adapters.Postgres,
  username: "postgres",
  password: "postgres",
  database: "blog_dev",
  hostname: "localhost",
  pool_size: 10

现在,数据库blog_dev 不存在,但我的(总体初学者级别)理解mix ecto.create 应该在它不存在的情况下创建它。于是我跑了:

mix ecto.create

这给了我错误

localhost:blog alex$ mix ecto.create
warning: found quoted keyword "test" but the quotes are not required. Note that keywords are always atoms, even when quoted. Similar to atoms, keywords made exclusively of Unicode letters, numbers, underscore, and @ do not require quotes
  mix.exs:57


18:04:24.675 [error] GenServer #PID<0.212.0> terminating
** (DBConnection.ConnectionError) tcp recv: closed
    (db_connection) lib/db_connection/connection.ex:163: 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 Blog.Repo couldn't be created: an exception was raised:
    ** (DBConnection.ConnectionError) tcp recv: closed
        (db_connection) lib/db_connection/connection.ex:163: 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

This answer on StackOverflow 提示这种错误通常发生在 Postgres 未运行时。所以我这样检查:

localhost:blog alex$ brew services list
Name                       Status  User Plist
postgresql                 started

我还认为我的凭据不正确,因此我尝试使用 config/dev.exs 中的凭据手动登录 Postgres,如下所示:

localhost:blog alex$ psql postgres postgres -W
Password: 
psql (11.3)
Type "help" for help.

postgres=#

我输入的密码是“postgres”。有人对这里可能发生的事情有任何建议吗?谢谢!

【问题讨论】:

  • 我创建了一个新的 Phoenix 项目 -- 信息不足。
  • 从 v10 开始,PostgreSQL 默认使用5433 端口进行加密连接。检查您的 /etc/postgresql/10/main/postgresql.conf 以获取 port 条目。
  • @AlekseiMatiushkin,我认为这不是端口问题。当我使用错误的端口时,我收到错误:(DBConnection.ConnectionError) tcp connect (localhost:55): connection denied - :econnrefused
  • @7stud 我的意思是我通过运行命令mix phx.new blog 创建了我的项目,抱歉含糊不清。

标签: postgresql elixir phoenix-framework ecto


【解决方案1】:

当应用程序连接到默认的 5432 端口时,从 docker 提取的 Postgres 11.4 和在 Elixir 1.8.2/OTP 21 上运行的 Phoenix 1.4.9 工作正常。

docker pull postgres:11.4
docker run --rm --name pg-docker -e POSTGRES_PASSWORD=postgres -d -p 5434:5432 postgres:11.4
mix archive.install hex phx_new 1.4.9
mix phx.new blog
mix ecto.create

# Compiling 13 files (.ex)
# Generated blog app
# The database for Blog.Repo has been created

请注意,在上面的示例中,本地端口 5434 映射到容器的端口 5432,postgres 会在该端口进行侦听。如果我们现在将映射更改为 5434:5788,5788 是 postgres 不使用的随机端口值,设置会中断并出现类似于您所看到的错误。

docker container stop pg-docker
docker run --rm --name pg-docker -e POSTGRES_PASSWORD=postgres -d -p 5434:5788 postgres:11.4
mix ecto.create

# 11:25:32.876 [error] GenServer #PID<0.217.0> terminating
# ** (DBConnection.ConnectionError) tcp recv: closed
#     (db_connection) lib/db_connection/connection.ex:87: 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 Blog.Repo couldn't be created: killed

这意味着您的应用会尝试与关闭的端口建立 TCP 连接。

换句话说,检查您的 postgres 配置并确保您连接的端口是 postgres 侦听的端口。

【讨论】:

    【解决方案2】:

    这给了我错误

    localhost:blog alex$ mix ecto.create 警告:找到引用的关键字 “测试”,但不需要引号。 mix.exs:57

    您应该做的第一件事是修复所有可以修复的警告/错误。那么,第 57 行的 mix.exs 文件是什么样的?

    创建数据库需要做四件事:

    1) 在 mix.exs 中:

      defp deps do [
        ...
        ...
        {:ecto_sql, "3.0.3"},  # whatever versions you want here
        {:postgrex, "0.14.1"},
        {:phoenix_ecto, "~>4.0"}
      ]
    

    2) 在config/config.exs:

    config :blog,
      ecto_repos: [Blog.Repo]
    

    3) 在config/dev.exs:

    config :blog, Blog.Repo,
      database: "blog_dev",
    
      username: "postgres",
      password: "postgres",
    
      hostname: "localhost",
      port: "5432",
    
      show_sensitive_data_on_connection_error: true,
      pool_size: 10
    

    您需要弄清楚您的 postgres 服务器在哪个端口上运行。我的(Postgres.app)默认在端口5432 上运行。

    3) 在lib/blog/repo.ex:

    defmodule Blog.Repo do
      use Ecto.Repo,
        otp_app: :blog,
        adapter: Ecto.Adapters.Postgres
    end
    

    4) .../phoenix_apps/blog$ mix ecto.create

    【讨论】:

    • 旁注:警告可能是由 test 的别名中的 test 的引号引起的:"test": ["ecto.drop", "ecto.create --quiet", "ecto.migrate", "test"] 应该是 test: [...]。
    • 我拥有所有这些东西,我在默认端口 (5432) 上运行 Postgres,我得到完全相同的错误:(
    猜你喜欢
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-17
    • 2021-06-22
    • 2020-07-31
    相关资源
    最近更新 更多