Heroku remote database must be empty error after pg:reset - ruby-on-rails

I have an app on Heroku and I'm trying to replace the database on Heroku with the database on my local machine.
I was under the impression that
heroku pg:reset DATABASE
would clear the remote database and allow me to push. However, after running pg:reset, I run
heroku pg:push
and I stil get this error:
Usage: heroku pg:push <LOCAL_SOURCE_DATABASE> <REMOTE_TARGET_DATABASE>
push from LOCAL_SOURCE_DATABASE to REMOTE_TARGET_DATABASE
REMOTE_TARGET_DATABASE must be empty.
Couple of questions:
1. do I have to specify local db and remote db? [I am in the root directory of my application on my local machine]
2. if so, how do I find what my local database name is? I know that heroku can divine the remote database via DATABASE_URL?

You have to specify BOTH the local database(where the data is coming from) and the remote database(where the data is going). The part about REMOTE_TARGET_DATABASE must be empty. is just a reminder to make sure you clear the Heroku database before you push.
You should be able to find your database from your database.yml file.
You can also find the heroku database by heroku pg:info
Reminder: Heroku runs PostgreSQL so you should be using PostgreSQL locally as well.

Related

How connect deployed rails app to exist Heroku database

I launched my Rails application on Heroku.
And I want to connect it to the database of an existing project of my friend, which is also deployed on Heroku. A friend gave me credentials and url to the database. However, I do not understand how to connect my project to my friend's database. I tried to change DABASE _URL but all is useless. Also, credential data was placed in database.yml and when I run the heroku run db:setup in the heroku terminal i get
FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
Couldn't create '*************' database. Please check your configuration.
rake aborted!
PG::ConnectionBad: FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege
Use following steps:
1) Add DATABASE_URL (complete url username, password, host) under settings / Config Vars.
2) For Rails apps you should look to use db:schema:load, db:structure:load or db:migrate instead of db:setup.
here is more info about issue https://help.heroku.com/63D7ALXT/why-am-i-seeing-user-does-not-have-connect-privilege-error-with-heroku-postgres-on-review-apps
You can share a single Heroku Postgres database between multiple apps with the heroku addons:attach command
heroku addons:attach my-originating-app::DATABASE --app new_app
for more info check here https://devcenter.heroku.com/articles/heroku-postgresql#sharing-heroku-postgres-between-applications
hope this helps
The reason you cannot update DATABASE_URL is because it's currently pointing at another database without any auxiliary attachments. This is a safety mechanism to prevent users from accidentally shooting themselves in the foot. This should allow for your changes.
heroku addons:attach <addon name, eg postgresql-cloudy-12345> --as ORIGINAL_DB --app <app name>
This will create an additional attachment name so you don't lose reference to the original database
heroku addons:detach DATABASE_URL --app <app name>
This will remove the Heroku-managed DATABASE_URL config var.
heroku config:set DATABASE_URL=<the connection string for the other database> --app <app name>
This sets DATABASE_URL to point at the other database which should put things into an operable state.*
* This caveat here is that this is a brittle configuration because hostnames can change for databases. If the db undergoes maintenance you will need to manually change the config var (ie repeat step 3).
I solved this situation by going to heroku.com to my app then setting then Config Vars
The DATABASE_URL was different than the ORIGINAL_DB_URL so I changed the DATABASE_URL to be similar to ORIGINAL_DB_URL
In my case: postgres://quxhxkemlsfzzv:964c7..........

Heroku still referencing old database

I've provisioned and switched to a new database in my heroku app. And it seemed to work fine, but now I attempted to run the console and it had the data from my old database. I ran heroku addons:destroy <DATABASE_URL> --remote live and now the hobby-dev db has been destroyed and now when I try to run a query from the console I get PG::ConnectionBad: FATAL: role "fgedboaquoao" is not permitted to log in
Anybody know how I can stop my app from referencing the old database?
I had it hard coded in my production.rb to share the databases. that was the problem

How to Upload data to Heroku ? Rails 4

My Rails app is about an Online Shopping site.
I'm trying Deploy it to Heroku.
I've done push to heroku,and rake db:migrate.
But now in the My web,it doesn't have any Data.
In the localhost:3000,I can show Products,Phones,Users,etc.
But in my web:https://boiling-bayou-5793.herokuapp.com/welcome/home
It doesn't show anything.
So what should I do to upload data from my PC to my web
The data from your local development database is not automatically transferred to the production environment. There is no rake task to do that.
You can use seed files and the rake db:seed task to get some initial data in your (production) database.
A more heroku-specific method could be just loading a database dump.
Assuming you're using Postgresql, n your local computer do
pg_dump -h localhost -U username -Fc dbname > db.dump
upload the file somewhere on the internet (like Amazon S3 so you can get a direct download link) and then on heroku run
heroku pgbackups:restore SHARED_DATABASE http://www.example.com/db.dump
Remember to remove the db dump right away if it's any kind of sensitive data!

Check db status through DATABASE_URL

I've been given access to Heroku application with rather strange setup. It has one database but when I run heroku config, I get different DATABASE_URL and HEROKU_POSTGRESQL_BRONZE_URL.
When I run heroku pg:info I get the following result:
=== HEROKU_POSTGRESQL_BRONZE_URL
Plan: Dev
Status: available
Connections: 1
PG Version: 9.2.4
Created: 2013-09-05 11:02 UTC
Data Size: 6.5 MB
Tables: 0
Rows: 0/10000 (In compliance)
Fork/Follow: Unsupported
I realised that my database is at DATABASE_URL, but I can't access that database, only through heroku run console. All heroku pg commands fail with this message:
! Unknown database: DATABASE_URL. Valid options are: HEROKU_POSTGRESQL_BRONZE_URL
If I run heroku pg HEROKU_POSTGRESQL_BRONZE_URL, I get access to empty database from above.
Since I have some issues with running migrations, I think my database might be full, and I'd like to check it. Any ideas how I can do that?
Here's the error after I run heroku run rake db:migrate:
PG::Error: ERROR: permission denied for relation schema_migrations
: INSERT INTO "schema_migrations" ("version") VALUES ('20130918114202')
More information about the setup:
rails 3.2.12
RAILS_ENV: staging (I don't have access to production, but I know this is "dev" server and there's also real "staging" from which this app was forked).
I have same problem and i fixed it:
Just Keep a Backup from your database and restore it back again, here is the steps:
heroku pg:info <-- to get the Database Name
heroku addons:add pgbackups <-- make sure you have the addons for backup
heroku pgbackups:capture <-- Capture the backup
heroku pgbackups <-- check your backups and make sure its there
heroku pg:reset DATABASE_NAME <-- Reset your database don't worry we have a backup, replace DATABASE_NAME with database name
heroku pgbackups:restore DATABASE_NAME b001 <-- Restore the backup again, replace DATABASE_NAME with database name and b001 with your Database version you can see this version number in heroku pgbackups step
heroku run rake db:migrate <-- Now you can run your migration and Operate in normal mode.
This seems like something screwy on the Heroku side. Have you tried submitting a ticket with them? I've always had good luck with their support.
I just got an update from my client. Before, I couldn't drop the database, because of the data in it. At the end, we decided to drop the database since the data can be added back (it's dev server, doesn't matter if we loose some dummy data).
I didn't find a solution for the problem above, but promoting HEROKU_POSTGRESQL_BRONZE_URL and restoring from backup solved the issue about not being able to access the database.

Why do I get "We're sorry, but something went wrong." after restoring via pgbackups:restore with no migrations

This one took me a while to figure out. It's pretty tricky and seems like a bug in Heroku, so I will post my own answer in case it helps someone.
Scenario:
Rails application using postgresql adapter works fine on localhost.
All migrations are complete.
Deploying to Heroku via "git push heroku master" successfully updates Heroku instance.
Heroku PGBackups addon is installed and works fine.
pg_dump was used to generate a local db dump, and the file uploaded to an accessible internet location.
Problem:
After running
heroku pgbackups:restore DATABASE_URL 'http://mywebsite.com/pgbackup.dump'
I get the "something went wrong" message. Strange, considering all that happened was a database load, a database whose data works fine on the local machine. No migrations were performed, everything has been committed previously, no code updates... Only data in the database was changed, so why does the app no longer work if just data in the database changed?
Checking the heroku logs shows that it can't find tables that are clearly there. Lines like:
ActionView::Template::Error (PG::UndefinedTable: ERROR: relation "users" does not exist
Verifying with heroku pg:psql just makes the situation even stranger, because doing select * from users brings back the real results. Even performing a heroku restart at this stage won't get rid of the problem. I also tried combinations of that with heroku pg:reset and heroku run rake db:migrate.
Why does updating data in the database shut down the app?
Solution:
Run the pgbackups:restore as originally intended (and get error).
Run heroku restart.
Run heroku pg:psql (profit).
For some reason, it seems necessary to restart and then pg:psql before the database works after a pg:restore.
Even though pg:psql will gladly show that the tables are indeed there (contrary to the log messages), the app won't work until the dyno is restarted (and then it STILL won't work), not until you pg:psql.
As far as I know, pg:psql only establishes a connection to the remote database, and should not have any consequence on the state of the database server (though it seems it does).

Resources