如何在 Elixir 中为 postgres 连接设置 application_name

How to set application_name for postgres connections in Elixir

下面 config.exs 中的代码会将 postgresql 中的 application_name 设置为 "myapp"。如何改用 Elixir 节点名称? (这里使用Kernel.node会导致参数错误)

config :db, DB.Repo,
  adapter: Ecto.Adapters.Postgres,
  database: "ahv2",
  username: "troy",
  password: "pass",
  hostname: "localhost",
  parameters: [
    {:application_name, "myapp"}
  ]

您可以使用在 v2.1.0 中添加到 Ecto 的 init/2 回调。下面的代码应该可以工作(但我还没有测试过)。您需要在 use Ecto.Repo, ...

之后将其添加到您的 Repo 模块
def init(_, config) do
  {:ok, put_in(config, [:parameters, :application_name], Node.self |> to_string)}
end

将元组中的 application_name 类型内部参数更改为列表完成工作并且对我有用。

config :db, DB.Repo,
  adapter: Ecto.Adapters.Postgres,
  database: "ahv2",
  username: "troy",
  password: "pass",
  hostname: "localhost",
  parameters: [
    application_name, "myapp"
  ]