Why create a Posgres Database App

Hi there,

This may seem like a really stupid question but this is my first time hosting and deploying a rails app. If I’ve created a test, development and production database locally, why is it that I also need to create a Posgres Database App in Fly? Is it possible to connect the production database I created locally to the one in Fly? Or does the Fly.io production database I created simply become my production database?

Thank you, I really appreciate all the help I can get :slight_smile:

1 Like

The Fly.io production database becomes the production database in Rails. The most normal setup is to have development/test locally, then use the production configuration block for the “cloud” database used after deploy.

You could theoretically connect back to your local database. It would be fun. :slight_smile: It’s probably not what you want once your app is deployed though.

Thanks so much for the help! So I guess what I’m not understanding is that I have now created the postgres app, and now I’m trying to attach the postgres app to my app, once I do that should I be able to run RAILS_ENV=production rails db:migrate and get the correct schema in the fly.io database?
Also, another beginner question, not sure where to find the DATABASE_URL env variable or if that’s something I should even be able to find in the app at this point, before I’ve attached the db…

I also get the following error when trying to attach the database or when trying to connect to it via proxy:

The agent failed to start with the following error log:
2022/07/11 23:44:05.283804 srv another instance of the agent is already running

Again thanks for the help

Ah, you can’t run RAILS_ENV=production rails db:migrate locally. You need to run that within your app. The best way to do this is with a release_command. Add this to your fly.toml and we run it for you when you deploy:

[deploy]
  release_command = "bundle exec rails db:migrate"

Attaching the DB adds the DATABASE_URL as a secret. It will print it out, too.

That agent conflict error is super weird. Try running fly agent stop and see if it works after that?

Yep that all works! And my last question for the evening, if I’m trying to run a proxy to connect to the production database through a database manager like TablePlus, should the credentials be the same as the ones I was given upon database creation?

I’m trying to connect now and entering the database name in the database input and the correct username and password doesn’t seem to be working for me. Am I missing something here?

Yeah make sure you put the user, password, and database name from the URL we generated for you.

When you run fly pg attach we give you information specifically for that DB. The credentials we gave you when you created the DB are closer to root level access.

Things to check from your config: make sure database is filled in (it’s blank in the screenshot) and also make sure you started the proxy with port 5432. The test button should give you some information about what’s failing.

Yeah weird, I did fill in database and the correct user/password and was running the proxy on port 5432. Again, thank you so much, this has been incredibly helpful so I really appreciate it

Could it have anything to do with me setting the DATABASE_URL like this?

flyctl secrets set DATABASE_URL=postgres://postgres:secret123@postgresapp.internal:5432/yourdb