Rakefile can't connect to postgres

Hi there

I’m using a cronjob to run a rake task in my rails app, but it’s failing, with this error:

rake aborted!
ActiveRecord::DatabaseConnectionError: There is an issue connecting to your database with your username/password, username: rails_authentication

Please check your database configuration to ensure the username/password are valid.
/path/to.rake:17:in `block (2 levels) in <main>'


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

I have set up the postgres secrets, and my database.yml production config looks like this:

production:
  <<: *default
  database: newsguessr_production
  username: rails_authentication
  password: <%= ENV["RAILS_AUTHENTICATION_DATABASE_PASSWORD"] %>

However I had to start over with a PG database, so do I need to manually update the database.yml file manually? The latest database is not ‘newsguessr_production’, it has a different name generated by Fly.io.

Many thanks

What is likely happening is that the DATABASE_URL environment variable is not available to the cronjob. It takes precedence over config/database.yml. See: Configuring Rails Applications — Ruby on Rails Guides

1 Like

I did some more digging, and I might not have been connected to the right database. When I connect to the database I set up (and set secrets for) with Fly.io, and list the databases and users, I get this:

postgres=# \du
                                    List of roles
 Role name  |                         Attributes                         | Member of 
------------+------------------------------------------------------------+-----------
 flypgadmin | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 newsguessr | Superuser                                                  | {}
 postgres   | Superuser                                                  | {}
 repluser   | Replication                                                | {}

postgres=# \l
                                    List of databases
    Name    |   Owner    | Encoding |  Collate   |   Ctype    |     Access privileges     
------------+------------+----------+------------+------------+---------------------------
 newsguessr | flypgadmin | UTF8     | en_US.utf8 | en_US.utf8 | 
 postgres   | flypgadmin | UTF8     | en_US.utf8 | en_US.utf8 | 
 template0  | flypgadmin | UTF8     | en_US.utf8 | en_US.utf8 | =c/flypgadmin            +
            |            |          |            |            | flypgadmin=CTc/flypgadmin
 template1  | flypgadmin | UTF8     | en_US.utf8 | en_US.utf8 | =c/flypgadmin            +
            |            |          |            |            | flypgadmin=CTc/flypgadmin

I now have my database.yml file set to:

production:
  <<: *default
  url: <%= ENV["DATABASE_URL"] %>

I don’t know why I have four databases listed there, and which one I should be connecting to - or if I’m trying to connect to it in the correct way. Thanks!

UPDATE:

Another clue perhaps - echo $DATABASE_URL returns a blank line. I’ve added env :DATABASE_URL, ENV[‘DATABASE_URL’] to my schedule.rb file and the error is now ‘rake aborted!
Database URL cannot be empty’.

Is it possible that I’ve not set the secrets properly for the database?

Hi there!

After going round in circles on this all day, I think you were right all along :sweat_smile:

I started from scratch with a new postgres cluster just to make sure that the secrets weren’t somehow out of sync. So now I’m just struggling with how to pass them to Whenever gem / schedule.rb. Thanks so much!