failed to connect: ** (Postgrex.Error) FATAL (invalid_catalog_name): database "api_example_dev" does not exist
Asked Answered
L

1

6

I'm creating a simple API Rest using Phoenix. I am getting the following error when executing the command: mix ecto.migrate

22:25:14.197 [error] Postgrex.Protocol (#PID<0.193.0>) failed to connect: ** (Postgrex.Error) FATAL (invalid_catalog_name): database "api_example_dev" does not exist
** (DBConnection.ConnectionError) connection not available because of disconnection
    (db_connection) lib/db_connection.ex:934: DBConnection.checkout/2
    (db_connection) lib/db_connection.ex:750: DBConnection.run/3
    (db_connection) lib/db_connection.ex:1141: DBConnection.run_meter/3
    (db_connection) lib/db_connection.ex:592: DBConnection.prepare_execute/4
    (ecto) lib/ecto/adapters/postgres/connection.ex:91: Ecto.Adapters.Postgres.Connection.execute/4
    (ecto) lib/ecto/adapters/sql.ex:235: Ecto.Adapters.SQL.sql_call/6
    (ecto) lib/ecto/adapters/sql.ex:185: Ecto.Adapters.SQL.query!/5
    (ecto) lib/ecto/adapters/postgres.ex:71: Ecto.Adapters.Postgres.execute_ddl/3
    (ecto) lib/ecto/migrator.ex:43: Ecto.Migrator.migrated_versions/2
    (ecto) lib/ecto/migrator.ex:142: Ecto.Migrator.run/4
    (ecto) lib/mix/tasks/ecto.migrate.ex:84: anonymous fn/4 in Mix.Tasks.Ecto.Migrate.run/2
    (elixir) lib/enum.ex:737: Enum."-each/2-lists^foreach/1-0-"/2
    (elixir) lib/enum.ex:737: Enum.each/2
    (mix) lib/mix/task.ex:314: Mix.Task.run_task/3
    (mix) lib/mix/cli.ex:80: Mix.CLI.run_task/2
    (elixir) lib/code.ex:677: Code.require_file/2

When I run 'mix test', I get:

Generated api_example app
** (Mix) The database for ApiExample.Repo couldn't be created: exited in: :gen_server.call(#PID<0.2449.0>, {:checkout, #Reference<0.2107810362.96468993.2762>, true, 15000}, 5000)
    ** (EXIT) time out

22:24:29.587 [error] Task #PID<0.2448.0> started from #PID<0.74.0> terminating
** (stop) exited in: :gen_server.call(#PID<0.2449.0>, {:checkout, #Reference<0.2107810362.96468993.2762>, true, 15000}, 5000)
    ** (EXIT) time out
    (db_connection) lib/db_connection/connection.ex:54: DBConnection.Connection.checkout/2
    (db_connection) lib/db_connection.ex:928: DBConnection.checkout/2
    (db_connection) lib/db_connection.ex:750: DBConnection.run/3
    (db_connection) lib/db_connection.ex:1141: DBConnection.run_meter/3
    (db_connection) lib/db_connection.ex:592: DBConnection.prepare_execute/4
    lib/ecto/adapters/postgres/connection.ex:91: Ecto.Adapters.Postgres.Connection.execute/4
    lib/ecto/adapters/postgres.ex:203: anonymous fn/2 in Ecto.Adapters.Postgres.run_query/2
    (elixir) lib/task/supervised.ex:88: Task.Supervised.do_apply/2
Function: #Function<4.100334649/0 in Ecto.Adapters.Postgres.run_query/2>

file config/dev.exs:

# Configure your database
config :api_example, ApiExample.Repo,
  adapter: Ecto.Adapters.Postgres,
  username: "postgres",
  password: "postgres",
  database: "api_example_dev",
  hostname: "localhost",
  pool_size: 10
Logrolling answered 2/5, 2018 at 1:29 Comment(2)
Did you run mix ecto.create first?Kitchen
Do you have PostgreSQL server running on the localhost in the first place?Yama
D
8

You need to run mix ecto.create as there is no database to migrate right now. Once you've done that you can run mix ecto.migrate and it'll perform the migrations. To see a list of all mix commands, use mix help. Here's a list of the current ecto tasks available:

mix ecto                 # Prints Ecto help information
mix ecto.create          # Creates the repository storage
mix ecto.drop            # Drops the repository storage
mix ecto.dump            # Dumps the repository database structure
mix ecto.gen.migration   # Generates a new migration for the repo
mix ecto.gen.repo        # Generates a new repository
mix ecto.load            # Loads previously dumped database structure
mix ecto.migrate         # Runs the repository migrations
mix ecto.migrations      # Displays the repository migration status
mix ecto.rollback        # Rolls back the repository migrations

If you're using postgresql, you can safely assume the term repository storage means database if the above is unclear.

If mix ecto.create fails, it means postgresql is not running or not installed.

To see if it's running, try:

ps auxwww | grep postgres

and you should see something like:

username 952 0.0 0.0 2656344 388 ?? S 19Apr18 0:47.05 /Applications/Postgres.app/Contents/Versions/9.6/bin/postgres -D /Users/username/Library/Application Support/Postgres/var-9.6

If you don't (see something similar to the above), you'll need to start it or install it (if you don't have it).

On MacOS, I'd highly recommend Heroku's PostgresApp (https://postgresapp.com/) over homebrew or system postgresql.

If you let me know in a comment what OS you're on if the above doesn't work, and I'll update my answer with OS instructions for you.

Dart answered 2/5, 2018 at 23:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.