mix ecto.create即使Postgres正在运行并且凭据正确,也无法连接到Postgres

时间:2019-07-24 23:08:51

标签: postgresql elixir phoenix-framework ecto

我创建了一个新的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服务列表 名称状态用户Plist PostgreSQL开始了

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

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

postgres=#

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

2 个答案:

答案 0 :(得分:1)

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

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映射到postgres侦听的容器端口5432。如果我们现在将映射更改为5434:57885788是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侦听的端口。

答案 1 :(得分:0)

  

哪个给了我错误

     

localhost:blog alex $混合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