Elixir/Phoenix 无法连接到云托管的 Postgres 数据库

Elixir/Phoenix can't connect to cloud hosted Postgres DB

试图将我的第一个 Phoenix 应用程序连接到 Compose 的云托管 Postgres 数据库,但无法连接。我已验证它已启动并且 运行。我收到以下错误:

[error] GenServer #PID<0.178.0> terminating
** (DBConnection.ConnectionError) tcp connect (postgres://*****@aws-us-east-1-portal.5.dblayer.com:16786/compose:5432): non-existing domain - :nx
domain
    (db_connection) lib/db_connection/connection.ex:148: DBConnection.Connection.connect/2
    (connection) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
** (Mix) The database for HelloPhoenix.Repo couldn't be created: an exception was raised:
    ** (DBConnection.ConnectionError) tcp connect (postgres://****@aws-us-east-1-portal.5.dblayer.com:16786/compose:5432): non-existing domain -
:nxdomain
        (db_connection) lib/db_connection/connection.ex:148: DBConnection.Connection.connect/2
        (connection) lib/connection.ex:622: Connection.enter_connect/5
        (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3

这些是我的依赖项:

defp deps do
  [{:phoenix, "~> 1.2.1"},
  {:phoenix_pubsub, "~> 1.0"},
  {:phoenix_ecto, "~> 3.0"},
  {:postgrex, ">= 0.0.0"},
  {:phoenix_html, "~> 2.6"},
  {:phoenix_live_reload, "~> 1.0", only: :dev},
  {:gettext, "~> 0.11"},
  {:cowboy, "~> 1.0"}]
end

我的数据库配置:

# Configure your database
config :hello_phoenix, HelloPhoenix.Repo,
  adapter: Ecto.Adapters.Postgres,
  username: "admin",
  password: "******",
  database: "compose",
  hostname: "postgres://admin:******@aws-us-east-1-portal.5.dblayer.com:16786/compose",
  pool_size: 10

并使用此版本的 PostgresSQL:PostgreSQL 9.4.10

如有任何帮助,我们将不胜感激。 :-) 你们都保重! -克里斯

你的数据库的hostnameaws-us-east-1-portal.5.dblayer.com,不是整个postgres://...。此外,您需要指定端口,因为它不是默认的 PostgreSQL 端口。这应该有效:

config :hello_phoenix, HelloPhoenix.Repo,
  adapter: Ecto.Adapters.Postgres,
  username: "admin",
  password: "******",
  database: "compose",
  hostname: "aws-us-east-1-portal.5.dblayer.com",
  port: 16786,
  pool_size: 10

因为您似乎有一个完整的数据库 URL,对您来说最简单的解决方案可能是使用该 URL 并让 ecto 将其解码为连接所需的组成部分。

config :hello_phoenix, HelloPhoenix.Repo,
  adapter: Ecto.Adapters.Postgres,
  url: "postgres://admin:******@aws-us-east-1-portal.5.dblayer.com:16786/compose",
  pool_size: 10

这是该功能的文档:https://hexdocs.pm/ecto/Ecto.Repo.html#module-urls