Search code examples
postgresqlelixirphoenix-frameworkecto

Elixir-Phoenix: mix ecto.create and mix phx.server provoke same error but contain different postgres db parameters


When running mix ecto.create I get the following error:

17:47:14.118 [error] GenServer #PID<0.299.0> terminating
** (Postgrex.Error) FATAL 28000 (invalid_authorization_specification) no pg_hba.conf entry for host "10.110.0.3", user "staging", database "postgres", SSL off
    (db_connection 2.4.0) lib/db_connection/connection.ex:100: DBConnection.Connection.connect/2
    (connection 1.1.0) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib 3.15.2) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol

Then, if I try to run mix phx.server I get the following error:

[error] Postgrex.Protocol (#PID<0.358.0>) failed to connect: ** (Postgrex.Error) FATAL 28000 (invalid_authorization_specification) no pg_hba.conf entry for host "10.110.0.3", user "staging", database "contact", SSL off

My database config in dev.exs is:

config :contact_api, ContactApi.Repo,
   username: System.get_env("DB_USER") || "postgres",
   password: System.get_env("DB_PWD")  || "postgres",
   database: System.get_env("DB_NAME") || "contact",
   hostname: System.get_env("DB_HOST") || "localhost",
   port: System.get_env("DB_PORT")     || "5432",
   show_sensitive_data_on_connection_error: true,
   pool_size: 10
  • I have already checked that env variables are properly setter.
  • I have also tried to test different env values but mix ecto.create still shows postgres for the database name at the error trace.
  • I can connect to the database using psql from the same server as the app.
  • When running a local DB with docker; mix ecto.create, mix ecto.migrate, mix test, and mix phx.server run as expected.

Any help will be much appreciated!


Solution

  • I have solved my problem. I am working with a cloud managed postgres database that is hosted by Digital Ocean. The only connections allowed are encrypted ones using ssl certificates. Error trace just informs about no user entry in pg_hba.conf but no reference to the ssl issue. Hence I had to modify config/dev.exs:

    config :contact_api, ContactApi.Repo,
       username: System.get_env("DB_USER") || "postgres",
       password: System.get_env("DB_PWD")  || "postgres",
       database: System.get_env("DB_NAME") || "contact",
       hostname: System.get_env("DB_HOST") || "localhost",
       port: System.get_env("DB_PORT")     || "5432",
       show_sensitive_data_on_connection_error: true,
       pool_size: 10
       ssl: true,
       ssl_opts: [
         cacertfile: "cert/ca-certificate.crt"
       ]