NoDatabaseError when building database with schema (Rails, Heroku) - ruby-on-rails

I have a live app on Heroku, and I'm attempting to build a database for my staging app using the following command:
heroku run rake db:schema:load --remote staging
The first roadblock I hit is this error:
ActiveRecord::NoEnvironmentInSchemaError:
Environment data not found in the schema. To resolve this issue, run:
bin/rails db:environment:set RAILS_ENV=production
After running this command, I get a second error:
rails aborted!
ActiveRecord::NoDatabaseError: FATAL: role "appname" does not exist
Not great at dealing with database issues. Any help would be appreciated.

This is a common thing you may have to provision a new database you can check the following link
https://devcenter.heroku.com/articles/postgres-logs-errors#fatal-role-role-name
But for locally you can do the following
sudo -u postgres createuser --superuser appname
or
createuser -P -d -e appname

Related

Unable to perform rake tasks in production environment due to psql permissions

I have set up a rake task that I want to run once an hour using a cron expression. I got it working locally on my dev environment, but I can't get it to work on my production environment (hosted on digitalocean) because I'm running into postgres permission issues.
This is the code I have in /lib/tasks/cron.rake:
task :cron => :environment do
puts "starting job"
Schedule.run_schedules
puts "finished job"
end
When I run rake cron in development, it results in this output:
starting job
finished job
When I ssh into my production environment and run the following command:
rake cron RAILS_ENV=production
I get the following output:
starting job
rake aborted!
ActiveRecord::NoDatabaseError: FATAL: role "root" does not exist
I figured I need to run this as the user that owns psql, so I ran the following commands:
su user
rake cron RAILS_ENV=production
But then I get this message:
rake aborted!
LoadError: cannot load such file -- bundler/setup
Running
bundler
results in:
Command 'bundler' not found, but can be installed with:
apt install ruby-bundler
Please ask your administrator.
I tried installing bundler by sudo apt install ruby-bundler, but then it tells me I don't have the correct permissions.
I'm completely lost here. Any help is much appreciated!
EDIT:
I also tried
psql -U user
psql -U user -w
both result in
psql: FATAL: Peer authentication failed for user "user"
I think this problem is related to the user authentication on database.yml
Because whenever you change into production, you are applying a different database, and therefor a different user and password.
Try the same command after changing your database.yml config, use the same database in the production as you use in development just for testing, and then you can refactor to run accordingly in the server.

How to specify an app's name while running a Heroku command?

I've just run heroku run rails db:migrate to add some new columns to my production tables after git push heroku master but I receive the error
Simons-MBP:gourmet_coffee Simon$ heroku run rake db:migrate
▸ Error: Could not find git remote stagegcl in /Users/Simon/gourmet_coffee
▸ remotes: heroku staging
So it's clearly trying to run on my staging app. How do I change this so it's my production app?
If you have multiple apps for the same project, you can use --app or -a flag to specify the name of the app.
heroku run rails db:migrate --app <the-name-of-your-app>

Rails - Heroku error, nothing in logs, works locally

I've just made some minor modifications to my rails app and now one route works perfectly locally, but raises an error on Heroku.
This is a simple route that directs to 'new' of my controller.
Everything works fine locally, but in prod: It raise the Heroku error
We're sorry, but something went wrong
There is nothing in the logs, I've also put some Rails.logger.warn in the method, but nothing appears.
How is it possible to get a specific error in production Heroku but not in local?
Can I get the same process in local to see the error and fix it?
You should definitely try to reproduce the issue locally by fetching the data to local database.
First make sure your current branch (most likely master) is same as heroku master where the app is deployed.
Then pull the database to local and try to re do the same steps. Follow commands will help you capture latest snapshot and store the database to local.
heroku pg:backups capture --app sushi
curl -o latest.dump `heroku pg:backups public-url --app sushi`
rake db:drop db:create
pg_restore --verbose --clean --no-acl --no-owner [-U user_name] -d database_name latest.dump
https://devcenter.heroku.com/articles/heroku-postgres-import-export
Did you migrate the database in Heroku? heroku run rake db:migrate

Rake db:reset 'user does not have CONNECT privilege'

So I'm using Heroku Postgres in my Rails app, but I'm not hosting my app on Heroku itself. I used the Active Record connection details from Heroku in my database.yml, and it looks like this:
development:
adapter: postgresql
encoding: unicode
pool: 5
database: [database]
username: [username]
password: [password]
host: ec2-54-227-243-78.compute-1.amazonaws.com
port: 5432
However, now I'm trying to rake db:migrate my app so the database gets all set up with my models. Running that command doesn't do anything, so I tried rake db:reset and I get this:
Couldn't drop df2cokjfj0k4vu : #<PG::Error: FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
df2cokjfj0k4vu already exists
-- initialize_schema_migrations_table()
-> 1.3997s
-- assume_migrated_upto_version(20130924040351, ["/home/action/braindb/db/migrate"])
-> 0.0882s
Any idea what I'm doing wrong. I'm still pretty new to Rails so sometimes I forget how to get my Postgres database setup properly when migrating hosts.
Use heroku pg:reset DATABASE instead as noted in https://devcenter.heroku.com/articles/rake
You cannot drop databases in Heroku with rake db:reset because user has no privileges.
You can't drop your PG database on Heroku.
I recently had this problem and resolved it through the following steps.
heroku pg --help gets the name of commands for using postgres
heroku pg:reset DATABASE # resets the db
Answer the prompt to confirm
Like others I had a similar problem but running pg:reset did not help: that's where I ran into the user does not have CONNECT privilege issue.
1) heroku pg:reset you will be prompted to enter the project name to confirm.
2) From Heroku's docs run heroku rake db:schema:load
I had the same problem. I fixed it by running:
heroku pg:reset DATABASE
(note: must specify DATABASE as above)
If you got this because you were running rake db:create, instead, simply skip to the migration. For whatever reason, postgresql on heroku doesn't require the rake db:create step as may be required on local installations.
In other words, instead of
heroku run rake db:create db:migrate db:seed
run this instead
heroku run rake db:migrate db:seed
The solution to me was to just go straight to migration because the database is already created (don't go on "heroku run rails db:create")
heroku run rails db:migrate -a YOURAPPNAME
For one of my apps, upgrading to the first paid tier of a Heroku database seemed to work for me: https://devcenter.heroku.com/articles/upgrade-heroku-postgres-with-pgbackups#provision-new-plan
Reason was that 'heroku pg:info' revealed that I was over my row limit as shown here:
Rows: 12392/10000 (Write access revoked)

Heroku and Rails: transferring db to staging and db:migrate

I've been working on a Heroku app for several months. We've recently set up a staging server and occasionally sync the staging db with the production db. The three main commands in use are:
$ heroku pgbackups:capture --app myapp
$ heroku pg:reset DATABASE --app myapp-staging --confirm myapp-staging"
$ heroku pgbackups:restore DATABASE `heroku pgbackups:url --app myapp` --app myapp-staging
The problem is that after running the third command, I need to run heroku run rake db:migrate --app myapp-staging. We have a few dozen migrations now, including some that refer to Ruby classes that we've deleted or renamed.
This causes the migrations to fail to fully run. What's the solution here? Should I delete the old migrations that fail and commit these changes to the git repo?
Re-running this script fixed the error, so it seems like the schema should just copy over. For anyone seeing a failing migration like I did, the pgbackups:restore command probably failed for you, so re-run that.
You can also checkout the transfer command now as part of pgbackups .. see this post
How do I transfer production database to staging on Heroku using pgbackups? Getting error

Resources