Postgrex - 没有针对主机“xxx.xxx.xxx.xxx”的pg_hba.conf条目

9

我有一个单独托管的Postgres数据库,我正在尝试将其与我的Phoenix应用程序一起使用。 我的prod配置如下:

config :my_app, MyApp.Repo,
  adapter: Ecto.Adapters.Postgres,
  url: "postgres://username:password@myhost:5432/my_database",
  size: 20 

在我的应用程序托管在Heroku时,这个工作得很好,但是自从我将其移动到VPS以后,我一直收到这个错误:

17:07:13.665 [info] GET /subjects
17:07:13.707 [info] Processing by MyApp.SubjectController.index/2
  Parameters: %{"format" => "html"}
  Pipelines: [:browser, :authorize]
17:07:13.951 [error] GenServer #PID<0.515.0> terminating
** (exit) %Postgrex.Error{message: nil, postgres: %{code: :invalid_authorization_specification, file: "auth.c", line: "474", message: "no pg_hba.conf entry for host \"xxx.xxx.xxx.xxx\", user \"username\", database \"my_database\", SSL off", pg_code: "28000", routine: "ClientAuthentication", severity: "FATAL"}}
17:07:13.970 [info] Sent 500 in 305ms
17:07:13.972 [error] #PID<0.513.0> running MyApp.Endpoint terminated
Server: xxx.xxx.xxx.xxx:80 (http)
Request: GET /subjects
** (exit) exited in: GenServer.call(#PID<0.515.0>, {:query, "SELECT s0.\"id\", s0.\"name\", s0.\"inserted_at\", s0.\"updated_at\" FROM \"subjects\" AS s0", []}, 5000)
    ** (EXIT) %Postgrex.Error{message: nil, postgres: %{code: :invalid_authorization_specification, file: "auth.c", line: "474", message: "no pg_hba.conf entry for host \"xxx.xxx.xxx.xxx\", user \"username\", database \"my_database\", SSL off", pg_code: "28000", routine: "ClientAuthentication", severity: "FATAL"}}

我也可以从Postgres GUI工具连接到数据库,所以没有任何问题。我注意到的一件事是它将我的VPS IP标记为主机,而不是在URL中指定的主机。
尽管数据库完全位于另一台主机上,但这应该并不重要,但我仍然尝试了以下解决方案: 真的很苦恼,请帮忙!
更新:这是我的pg_hba.conf文件:
# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            md5
#host    replication     postgres        ::1/128                 md5
host    all             all             0.0.0.0/0               md5

1
pg_hba.conf是一个文件,列出了你可以从哪里连接到数据库,所以你尝试连接的主机名没有被提及并不奇怪。 - IMSoP
另外,你具体尝试了什么?也许可以[编辑]问题并包含你当前的pg_hba.conf文件。 - IMSoP
已更新。我添加了最后一行以允许从所有地址访问所有内容。 - Sheharyar
请删除此问题,因为这是一个简单的配置问题。 - Patrick
3
为什么?这仍然可以帮助其他开发人员。 :) - MartinElvar
1个回答

12

这很蠢,但我找出了问题所在。我尝试连接的主机期望进行安全的数据库连接。默认情况下,SSL是关闭的,因此我必须启用它:

config :my_app, MyApp.Repo,
  adapter: Ecto.Adapters.Postgres,
  url: "postgres://username:password@myhost:5432/my_database",
  ssl: true,
  size: 20 

感谢这个答案:Node.js,PostgreSQL错误:没有pg_hba.conf条目的主机


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接