How do I give ENV['DATABASE_URL'] to Whenever gem in Fly.io Rails app?

184 views Asked by At

I'm trying to use the Whenever gem in my Rails app to schedule some rake tasks.

I'm hosting the app on Fly.io, and the rakes are aborting because they can't connect to the Postgres database in the production environment.

I wondered if the credentials stored in the secrets were somehow wrong; to troubleshoot this, I deleted the database and set up a new one, and set the secrets (username, URL, password) which are meant to be accessible in the ENV['DATABASE_URL'] environment variable.

I understand that the ENV['DATABASE_URL'] takes precedence over the database.yml file when it's present. I've tried all sorts of configurations, currently I have this in my schedule.rb file:

`env :DATABASE_URL, ENV['DATABASE_URL']

set :environment, :production`

And this in my database.yml file:

production: <<: *default

But I still get the following errors in my cron.log file:

rake aborted! ActiveRecord::ConnectionNotEstablished: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: database "thomaskitson" does not exist /Users/thomaskitson/code/tkitson/newsguessr/rails-authentication/lib/tasks/newspaper.rake:17:in block (2 levels) in ' /Users/thomaskitson/.rbenv/versions/3.1.2/bin/bundle:25:in load' /Users/thomaskitson/.rbenv/versions/3.1.2/bin/bundle:25:in '

Caused by: PG::ConnectionBad: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: database "thomaskitson" does not exist /Users/thomaskitson/code/tkitson/newsguessr/rails-authentication/lib/tasks/newspaper.rake:17:in block (2 levels) in <main>' /Users/thomaskitson/.rbenv/versions/3.1.2/bin/bundle:25:in load' /Users/thomaskitson/.rbenv/versions/3.1.2/bin/bundle:25:in <main>' Tasks: TOP => newspaper:new_images (See full trace by running task with --trace)

"thomaskitson" is not the name of the database, so I don't know why it's searching for one called that. There is an env variable called DATABASE_URL present in the fly secrets, so I believe that this is set up properly. Any help appreciated!

Thanks

0

There are 0 answers