How to access database in heroku - ruby-on-rails

So I would like to make an user to admin in the app I have on heroku. When developing my app I can do it via rails c or I just open up the database file with some SQL viewer. But how can I do it for heroku?

To run the rails console in heroku, you have to use heroku run rails console (assuming you have the heroku toolbelt installed).
See https://devcenter.heroku.com/articles/getting-started-with-rails3#console or https://devcenter.heroku.com/articles/getting-started-with-rails4#console

Related

Does anyone know why my heroku app doesn't access any of the data from my seeds.rb file?

I recently deployed an app to heroku but for some reason the heroku app doesn't access any of the seed data from the seed.rb file but when I run the app locally the data shows up just fine. Would love assistance
Try to run the rails task as follows:
heroku run rails db:seed -a name_of_your_heroku_app
You may need to do heroku login first.

rails console, local vs something like heroku

So I know I can do things like run rails console and change user values in my local environment. How does this apply to something like my rails app when deployed to heroku? Can I use the rails console there?
Of course you can, with Heroku Toobelt you can run heroku run rails console, more here: https://devcenter.heroku.com/articles/getting-started-with-rails4#console

how can I access my postgresql database on heroku and offline?

I am learning ruby on rails and heroku.
I have some questions.
Could somebody help me?
After running heroku create, the terminal is somehow connected to the repository/project in heroku. After that the developer can see logs, access database, ect. But, how can I access via terminal my completed project,which is online already, in heroku without creating the same apps and upload it again to heroku?
How can access my pg database in heroku or offline?
I saw this and try to run heroku pg:psql. it gives me hidden-atoll-4790::DATABASE=> help I try \? to see pg command but I cannot use it.
I saw this link to see pg db offline. But why cannot I access /var/lib/postgresql/9.3/main directory ? it is said that I don't have permission eventhough my account is administrator account.
1.Run heroku run rails console
Run
$ heroku apps # to see all apps created by your account
$ heroku open # to open you current project in browser (run it form root of your project)
$ heroku pg:psql DATABASE_URL # to gain access to heroku postgresql terminal, here you can run SQL queries
Refer to
CLI and Heroku PG for more info

Create a Record in Heroku Database

I just pushed my rails app to heroku, I actually have some fake data locally that I directly inserted into my table through rails console.
What is the equivalent of that in heroku, the tables are configured on heroku but there is no data.
I don't want to create a seed.rb in my db and then push it to github and then run rake db seed on heroku
can i do it manually?
You can enter your application console on heroku with:
heroku run rails console
Check out this gem. It is all ready dependency for heroku gem. With it bundled in your app you can do stuff like:
heroku db:pull
heroku db:push
Commands are quite self explanatory.

running Rails console in production

I have just gone live with my first Rails site, but now I have a problem. When I run the project in development mode on my IDE I can run the console to something like:
User.first.name='whatever' to change a users name.
How do I accomplish the same task on a live site in production mode?
if you're running rails 3.0 or greater, you can also use
rails console production
production can of course be substituted with development or test (value is development by default)
Adding the option --sandbox makes it so that any changes you make to your database in the console will be rolled back after you exit
If this isn't working for you, you may need to try
bundle exec rails console production
If you are actually trying to run the rails console on your production server, try googling "run rails console [your cloud hosting provider]" e.g. "run rails console heroku"
As of Rails 6 you need to use
RAILS_ENV=production bundle exec rails c
or
RAILS_ENV=production rails c
depending on your setup
Pretty easy:
RAILS_ENV=production rails console
If you have already deployed your site to the server, you can also use:
bundle exec rails console production
...in the webroot of your rails app. That is if you haven't installed the rails package directly on the server yet or if you want to run console within the context of your web app.
Try below command.
rails c -e production
Note: This answer assumes you are using Heroku as your hosting service.
It depends on what hosting service you are using. For Heroku, you can go to your terminal and type in
heroku run rails console
This will load up the rails console for your production site and will allow you to create records for your live site.
You can also look into seeding a database but that is generally meant for testing. RailsCasts has some videos on the topic but they are a bit outdated.
With Rails 6.1.6 on AlmaLinux8, the below command worked for me.
bundle exec rails console -e production
today with rails 6 run in console RAILS_ENV=production rails console

Resources