Database named postgres is required to avoid error

If there is not present a database named postgres besides the database required by my app I get the following error every time I hit the the phoenix server.

[error] GenServer #PID<0.519.0> terminating
** (Postgrex.Error) FATAL 3D000 (invalid_catalog_name) database "postgres" does not exist
    (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.7) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol

I noticed this when making some experiments connecting my local phoenix app to a database in heroku. At first I thought that there was a problem with heroku itself.

Then I dumped the database from heroku and restored it to Digital Ocean. I got the same error. When I created another database named postgres in Digital Ocean, the error disappeared.

Finally I tested in my local machine. I had no problems until I renamed the existing database postgres to postgres2, after which I got the error again,

It is important to note that in my local machine I had only one role named hectorsq. I created a roled named postgres and the problems persists. It is required that a database named postgres exists.

The same problem was discussed here N00b ecto problem - database "postgres" does not exist

In one of your config files, you will have a configuration for an ecto repo. That configuration specifies the need for a user and database, both called postgres by default. You can change these configuration options to be whatever you want them to be.

2 Likes

You can config the name with :maintenance_database, but you will need to connect to a DB to create your own DB.

I double checked and there in no configuration that refers to a postgres database. I did a search in all my project for the word postgres and it exists only in repo.ex

defmodule MyApp.Repo do
  use Ecto.Repo,
    otp_app: :my_app,
    adapter: Ecto.Adapters.Postgres
end

It should be present in config/dev.exs, config/test.exs and You probably need to pass some arguments when in prod.

Interesting, I was not aware of this option. https://hexdocs.pm/ecto_sql/Ecto.Adapters.Postgres.html#module-connection-options

However, what puzzles me is that my app shows this error every time I request a page. Perhaps it is only related to development mode, I will test in production later.

I added this option to my config pointing to the same development database and the error does not appear anymore.

Like this, in config/dev.exs

config :my_app, MyApp.Repo,
  username: "xxx",
  password: "yyy",
  database: "my_app_dev",
  hostname: "localhost",
  show_sensitive_data_on_connection_error: true,
  pool_size: 10

or in prod.secret…

database_url =
  System.get_env("DATABASE_URL") ||
    raise """
    environment variable DATABASE_URL is missing.
    For example: ecto://USER:PASS@HOST/DATABASE
    """

config :my_app, MyApp.Repo,
  # ssl: true,
  url: database_url,
  pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10")

secret_key_base =
  System.get_env("SECRET_KEY_BASE") ||
    raise """
    environment variable SECRET_KEY_BASE is missing.
    You can generate one by calling: mix phx.gen.secret
    """
1 Like