I have a Postgres DB setup with two servers called primary_replica and primary. I have a rake task that reads the data only from the server, not any writing actions. How can I use/connect that specific rake only to the primary_replica server? It is nice to hear a solution for connecting the same config for workers also.
Thanks in advance .
Have you considered passing a different
DATABASE_URL ENV variable when running the task? You probably wouldn’t need to change the code, just do something like:
DATABASE_URL="postgres://my-replica" rake my:task
For bg workers it’s different. If they run inline in the same process as your rake task, then DATABASE_URL works. If they run separately, then you would need to either build a worker just for this rake task, that connects to the correct database, or you would have to write a custom job implementation to create a separate db connection based on, say, a passed-in parameter.