phoenixframework/phoenix_ecto

Phoenix.Ecto.CheckRepoStatus throws error in CLI if database "postgres" is not used

svrcekmichal opened this issue · 9 comments

Environment

  • Elixir version (elixir -v): Elixir 1.10.4 (compiled with Erlang/OTP 23)
  • Phoenix version (mix deps): 1.5.5
  • Operating system: macos catalina
  • Browsers you attempted to reproduce this bug on (the more the merrier): chrome

Actual behavior

  1. setup fresh project mix phx.new test
  2. since I'm using heroku postgres, I'm not able to setup database with mix ecto.create. Heroku also doesn't allow you to change any database credentials. Instead I changed config in /config/dev.exs to
# Configure your database. 
config :test, Test.Repo,
  adapter: Ecto.Adapters.Postgres,
  url: "postgres://...",
  pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
  ssl: true
  1. on server start mix phx.server all looks good, I've got following in CLI
/Users/michalsvrcek/workspace/elixir/test (master*) $ mix phx.server
Compiling 1 file (.ex)
[info] Running TestWeb.Endpoint with cowboy 2.8.0 at 0.0.0.0:4000 (http)
[info] Access TestWeb.Endpoint at http://localhost:4000

webpack is watching the files…

[hardsource:2a797f91] Using 1 MB of disk space.
[hardsource:2a797f91] Tracking node dependencies with: package-lock.json.
[hardsource:2a797f91] Reading from cache 2a797f91...
Hash: dd875ba43d3afcc1aac8
Version: webpack 4.41.5
Time: 153ms
Built at: 09/23/2020 3:19:34 PM
                Asset       Size  Chunks             Chunk Names
       ../css/app.css   10.7 KiB     app  [emitted]  app
       ../favicon.ico   1.23 KiB          [emitted]  
../images/phoenix.png   13.6 KiB          [emitted]  
        ../robots.txt  202 bytes          [emitted]  
               app.js   13.5 KiB     app  [emitted]  app
Entrypoint app = ../css/app.css app.js
[0] multi ./js/app.js 28 bytes {app} [built]
    + 5 hidden modules
  1. when I open http://localhost:4000
[error] GenServer #PID<0.513.0> terminating
** (Postgrex.Error) FATAL 42501 (insufficient_privilege) permission denied for database "postgres"

User does not have CONNECT privilege.
    (db_connection 2.2.2) lib/db_connection/connection.ex:87: DBConnection.Connection.connect/2
    (connection 1.0.4) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib 3.13.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
[info] GET /
[debug] Processing with TestWeb.PageController.index/2
  Parameters: %{}
  Pipelines: [:browser]
[info] Sent 200 in 67ms

Expected behavior

As talked on freenode with @ericmj and @Nicd, this comes from plug Phoenix.Ecto.CheckRepoStatus, otp_app: :test which is not using Repo config, but instead trying to connect to default postgres database. I'm new to elixir, so it's hard to me to say what is expected behaviour, but I would expect it to either use config from Repo used everywhere in the app, or pop some message in CLI that will be more user friendly instead of connection error. As a newcomer to Elixir, I spent few hours trying to save this till the guys helped me on freenode.

Hi @svrcekmichal! One quick question: you are running in dev mode on Heroku? Because the plug should only run in the dev environment exactly due to the issues you listed. :)

Hi @josevalim. I'm running in dev, but not on heroku. I'm using only heroku postgres, otherwise running all locally since I'm learning and don't need deployed app

@svrcekmichal oh, so you are running against a heroku postgres database from development? :)

@josevalim Yes, that's correct. If by any chance you or somebody who is able to fix this need some heroku postgres for testing, you can use my setup

# Configure your database
config :discuss, Discuss.Repo,
  adapter: Ecto.Adapters.Postgres,
  url: "postgres://bxhirenqdvhqtu:528c613ddc05e69807b09fdc33197fbf6b9b8012595cd24de64517aa666bdb77@ec2-52-213-173-172.eu-west-1.compute.amazonaws.com:5432/diufhptto6ilr",
  pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
  ssl: true

The steps are us above. I know it contains username, password etc, but it's heroku free tier and I can drop and create whatever amount I want, so feel free to test on it.

@svrcekmichal did u solve this issue? getting the same in the same situation (using heroku psql) and just cant solve it

Nicd commented

@caiquejjx have you tried with the fix in commit 6e1c394 that is contained in version 4.2.1 of this library?

@caiquejjx have you tried with the fix in commit 6e1c394 that is contained in version 4.2.1 of this library?

yes, it's the version I'm using

Can confirm, I get the same issue, even using version 4.2.1, and even 4.3.0

Here's the error log, which seems pretty much identical to the one OP has:

15:14:54.453 [error] GenServer #PID<0.191.0> terminating
** (Postgrex.Error) FATAL 42501 (insufficient_privilege) permission denied for database "postgres"

User does not have CONNECT privilege.
    (db_connection 2.2.2) lib/db_connection/connection.ex:87: DBConnection.Connection.connect/2
    (connection 1.0.4) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib 3.14) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: nil
15:14:54.485 [error] GenServer #PID<0.205.0> terminating
** (Postgrex.Error) FATAL 42501 (insufficient_privilege) permission denied for database "postgres"

User does not have CONNECT privilege.
    (db_connection 2.2.2) lib/db_connection/connection.ex:87: DBConnection.Connection.connect/2
    (connection 1.0.4) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib 3.14) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: nil