Search code examples
elixirecto

How to set application_name for postgres connections in Elixir


The code below in config.exs will set the application_name in postgresql to "myapp". How can I use the Elixir node name instead? (using Kernel.node here causes an argument error)

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

Solution

  • You can use the init/2 callback added to Ecto in v2.1.0. The following code should work (but I have not tested it). You need to add this it to your Repo module after use Ecto.Repo, ...

    def init(_, config) do
      {:ok, put_in(config, [:parameters, :application_name], Node.self |> to_string)}
    end