Deploying ruby on rails with postgres

I have been trying to deploy my Ruby on Rails app to fly.io, and while I have got the database credentials sorted out, I seem to be having a problem with my database.

Whenever I try deploying there is an error, which says “ActiveRecord::NoDatabaseError: FATAL: database “e_recepti_production” does not exist”. That makes sense, since you usually have to run rake db:create, to create the databases. In fly.io’s case though, the deploy is fully automatic, and when it tries to start up the app it throws the NoDatabase error. The problem is that if the deploy fails the server doesn’t start, I cannot ssh to it and just run db:create manually, and I also can not connect to the db server to create the database.

Does anyone have any possible fixes for this, or any way I could make it run ‘rake db:create’ before trying to start the rails server?

Assuming you are using Fly Postgres, and the database exists, you will need to set a secret. Type fly secrets list and see if DATABASE_URL is set. If not, you can use flyctl postgres attach · Fly Docs to set it.

If you are using a different Postgres, can you describe your setup?

The database server connects, but the database is not yet created and that is why it fails. So the URL is set.

I disagree. You will see this exact error if the URL is not set, as Rails will default to looking for a database on your VM, and fail to find one.

If you want to use Fly postgres, flyctl postgres create · Fly Docs will create the database.

If you want to use a different postgres, and are using Rails 6 or later, you can change task :release => 'db:migrate' to task :release => 'db:prepare' in lib/tasks/fly.rake.

But without knowing more of what you want, it will be difficult to help you further.

As I thought, after I replaced db:migrate to db:prepare it lauched without an issue.

Thank you for the recommendation.