我正在使用Phoenix创建一个简单的API Rest。 执行命令时出现以下错误: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
当我进行'混合测试'时,我得到:
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
答案 0 :(得分:1)
您需要运行mix ecto.create
,因为现在没有要迁移的数据库。完成后,您可以运行mix ecto.migrate
,然后执行迁移。要查看所有混合命令的列表,请使用mix help
。这是当前可用的ecto任务列表:
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
如果你正在使用postgresql,如果上述内容不清楚,你可以放心地假设repository storage
这个词的意思是database
。
如果mix ecto.create
失败,则表示postgresql未运行或未安装。
要查看它是否正在运行,请尝试:
ps auxwww | grep postgres
你应该看到类似的东西:
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
如果你没有(看到类似上面的内容),你需要启动它或安装它(如果你没有)。
在MacOS上,我强烈推荐Heroku的PostgresApp(https://postgresapp.com/)超过自制程序或系统postgresql。
如果您在评论中告诉我,如果以上操作无法运行您的操作系统,我会为您更新我的操作系统说明。