【问题标题】:Phoenix framework: how to run ecto.create with custom port?Phoenix 框架:如何使用自定义端口运行 ecto.create?
【发布时间】:2022-01-22 15:19:27
【问题描述】:

如何在Phoenix 框架中为mix ecto.create 命令指定自定义服务器端口

我使用mix phx.new hello 命令创建了一个入门项目。 我在hello\config\dev.exs路径中配置数据库如下,

config :hello, Hello.Repo,
   username: "postgres",
   password: "postgres",
   hostname: "localhost",
   database: "hello_dev",
   show_sensitive_data_on_connection_error: true,
   pool_size: 10

但是,当我尝试使用 mix ecto.create 命令配置 Postgres 数据库 时,出现以下错误。

** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused

我使用 5433 作为 Postgres 端口(注意:它适用于我的 spring java 项目),因为默认端口 5432 在我的本地不可用。

所以在hello\deps\ecto_sql\lib\ecto\adapters\postgres\connection.ex 路径中 我在路径中定义了默认端口如下,

@default_port 5433

但如果我再次运行该命令,它仍会尝试连接到 5432。

如何获得连接到 5433 而不是 5432 的命令?

【问题讨论】:

  • 一些忠告:不要碰/deps 文件夹中的代码! 这些依赖项由mix 根据您在mix.exs 中的项目设置进行管理.如果您想自定义库:克隆库,进行一些调整并参考您的 deps 中的 github 存储库或本地路径,如 here 所述。
  • 非常感谢zwippie的建议。

标签: postgresql elixir phoenix-framework


【解决方案1】:

要在 Postgres 的自定义端口上运行 Ecto 迁移,请将 port: <port_number>, 添加到配置中。连接的其他选项在Ecto docs here 中。因此,要将其设置为端口 5433 而不是默认端口 5432,您的配置将如下所示:

config :hello, Hello.Repo,
   username: "postgres",
   password: "postgres",
   hostname: "localhost",
   database: "hello_dev",
   port: 5433,
   show_sensitive_data_on_connection_error: true,
   pool_size: 10

【讨论】:

  • 谢谢 DogEatDog,它成功了。
猜你喜欢
  • 2017-07-01
  • 2015-12-23
  • 2020-06-21
  • 1970-01-01
  • 1970-01-01
  • 2018-10-24
  • 2022-01-20
  • 2020-09-06
  • 1970-01-01
相关资源
最近更新 更多