Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgrex - No pg_hba.conf entry for host "xxx.xxx.xxx.xxx"

I have a Postgres Database hosted separately which I'm trying to use with my Phoenix Application. My prod config is:

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

This worked fine while my application was hosted on Heroku, but since I've moved it to a VPS, I keep getting this error:

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"}}

I can also connect to the Database from Postgres GUI tools, so there isn't anything wrong with that. One thing that I noted was that it states my VPS IP as the host instead of the host specified in the url.

It shouldn't matter since the Database is on a separate host altogether, but I still tried the solutions here:

  • No pg_hba.conf entry for host
  • Connect to PostgreSQL server: FATAL: no pg_hba.conf entry for host

Really struggling with this, please help!

Update: Here's my 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
like image 538
Sheharyar Avatar asked Jul 27 '15 21:07

Sheharyar


People also ask

What is Pg_hba Conf entry?

Client authentication is controlled by a configuration file, which traditionally is named pg_hba. conf and is stored in the database cluster's data directory. ( HBA stands for host-based authentication.) A default pg_hba. conf file is installed when the data directory is initialized by initdb.

Where can I find Pg_hba conf?

By default, the file is named pg_hba. conf. By default, on RHEL 7 , the file is at /var/lib/pgsql/data/, and on Windows, the file is at C:\Program Files\PostgreSQL\ version_number \data\.

How do I edit Pg_hba conf?

To modify the pg_hba. conf file, open the file with your choice of editor. After modifying the authentication settings in the pg_hba. conf file, restart the server and apply the changes.

Where is Pg_hba conf in Ubuntu?

Where is the Pg_hba conf file Ubuntu? pg_hba. conf is the PostgreSQL access policy configuration file, which is located in the /var/lib/pgsql/10/data/ directory (PostgreSQL10) by default.


1 Answers

This is so stupid, but I figured out the problem. The host I was trying to connect to, expected a secure database connection. By default, SSL is turned off, so I had enable it:

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

Thanks to this answer: Node.js, PostgreSQL error: no pg_hba.conf entry for host

like image 188
Sheharyar Avatar answered Sep 28 '22 09:09

Sheharyar