Change default postgres schema in Ecto Repo Config

923 views
Skip to first unread message

axedbits

unread,
Oct 13, 2016, 2:13:55 PM10/13/16
to elixir-ecto
Hi everyone,

In a Phoenix project I'm using a non-public postgres schema and I need a way to make every Ecto Query to use the non-public postgres schema by default.

The Phoenix project connects to the database using a custom postgres user which only has access to his non-public postgres schemas.

I can't make it in the Ecto Schema with @schema_prefix because I'm using generic Ecto schemas that need to be reused by another project using a different non-public schema.

If we could change the default from the 'public' postgres schema to a specific one it would work, and it would enable us to query different postgres schemas if needed.

--

This is not exactly a request, but a way to start discussing the viability of this, since I could make it work by duplicating the generic Ecto Schemas and use the @schema_prefix.

--

Thanks for this awesome language and work.




José Valim

unread,
Oct 13, 2016, 2:55:40 PM10/13/16
to elixi...@googlegroups.com
The default schema in postgres is configured via the search path. So I would advise to set the search path in the connection right after you connect.

You can do something like this:

 config :your_app, YourRepo,
    after_connect: {YourApp.SearchPath, :set, ["search_path"]}

Then:

defmodule YourAppSearchPath do
  def set(conn, value) do
    Postgrex.query!(conn, "SET search_path TO ?", [value])
  end
end

Or something of sorts. Please check the search_path or SET schema syntaxes in postgrex docs.



José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/cb85207f-a01d-41f4-ac98-57ff2dde8152%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

axedbits

unread,
Oct 13, 2016, 9:09:14 PM10/13/16
to elixir-ecto, jose....@plataformatec.com.br
Many thanks, you pointed me in the right direction, also found this stack overflow thread - http://stackoverflow.com/questions/35131338/change-postgres-search-path-with-ecto - that helped.

Thanks and all the best
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages