Heroku + Rails: Heroku connecting to wrong database from CLI - ruby-on-rails

I recently created a basic level database for my rails app and I noticed while I was attempting to run:
heroku run rake db:migrate
from the command line, that it was connecting to my old database. How do you control which database heroku connects to from the heroku run command?

I don't think it's possible to specify the database when running Heroku Rake commands, but you could set a specific database as primary (and thus automatically connecting to it when running said commands).
Run heroku pg:info, which should output something like (two databases should be listed in your case):
=== HEROKU_POSTGRESQL_MAROON_URL (DATABASE_URL)
Plan: Ronin
Status: Available
Data Size: 26.1 MB
Tables: 5
PG Version: 9.5.3
Connections: 2
Fork/Follow: Available
Rollback: Unsupported
Created: 2012-05-02 21:54 UTC
Maintenance: not required (Mondays 23:00 to Tuesdays 03:00 UTC)
Infrastructure: Legacy
You can then choose a database to promote:
heroku pg:promote HEROKU_POSTGRESQL_MAROON_URL
You can read more in this Heroku article.

Related

How to deploy a local postgresql database of Ruby on rails app to heroku?

I have been trying to deploy a local database which has some data into heroku. My app's name is myFirstBlog. But after deploying when I refresh my app, it says something went wrong. After so troubeshooting. I found out this:
Before exporting data into heroku database, I ran the command
heroku pg:info
And the output was:
Plan:Hobby-dev
Status:Available
Connections:1/20
PG Version: 9.4.1
Created: 2015-07-09 08:20 UTC
Data Size:6.6 MB
Tables: 3
Rows: 2/10000 (In compliance)
Fork/Follow: Unsupported
Rollback: Unsupported
And after I exported data to heroku and ran the same command, output was:
Plan: Hobby-dev
Status: Available
Connections:1/20
PG Version:9.4.1
Created:2015-07-09 08:20 UTC
Data Size:6.5 MB
Tables:0
Rows: 0/10000 (In compliance)
Fork/Follow:Unsupported
Rollback:Unsupported
The number of tables becomes 0 after export. Why is it happenning?
This is how I am exporting my local database's data to heroku:
PGPASSWORD="password" pg_dump -Fc --no-acl --no-owner -h localhost -U aditya9509 myFirstBlog_development > backup.dump //This command dumps the data in the backup.dump
Then I saved backup.dump in my github account because the tutorials stated that in order to export data from local database to heroku, it must be at a location which can be retrieved using a http protocol. I did not understand why but I did what it said.
Then finally I ran this command:
heroku pg:backups restore "http://github.com/aditya9509/rubyOnRails/blob/master/backup.dump" DATABASE -a stark-beach-9626
The "stark-beach-9626 is the name of the app given by heroku.
After running this command, when I access the app, it shows "something went wrong". What am I missing here?
P.S. I am new to ruby on rails so please be as simple as you can when you answer. I have been busting my head to solve thi problem for hours now. Also, let me know if you need some additional info. I gave all the info I thought was relevant.
You are trying to restore the git blob and not the raw file, use https://github.com/aditya9509/rubyOnRails/raw/master/backup.dump for the url and it will work.

Restore Production pgbackup to Development database on Heroku

I have a very basic application deployed on Heroku that has gathered data over the last week and a half. I now want to do some more development on the site and would like to copy the data from my production application database across to the database being used for my development.
I added pgbackups using the following command:
heroku addons:add pgbackups
I then captured the production database using
heroku pgbackups:capture
When I run heroku pgbackups I can see the following:
ID Backup Time Status Size Database
---- ------------------------- ------------------------------------ ------ --------- ---------------------------------
b001 2014/06/19 13:31.08 +0000 Finished # 2014/06/19 13:31.10 +0000 27.7KB HEROKU_POSTGRESQL_AMBER_URL (DATABASE_URL)
I'm now tring to use the following command to restore this backup to my development database on Heroku (orange)
heroku pgbackups:restore HEROKU_POSTGRESQL_ORANGE b001
but I'm getting the following message:
! Unknown database: HEROKU_POSTGRESQL_563f103f. Valid options are: DATABASE_URL, HEROKU_POSTGRESQL_AMBER_URL
Is it possible to do what I want and if so how do I do it? I've been trying to find an answer the last couple of hours but am getting confused with the concept of development-staging-production. Everything I'm finding refers to copying a db from production to staging but as far as I can see I'm trying to copy from production to development. Thanks for looking
Are the databases in two separate apps, i.e. a development app and a production app? If so, try using the pgbackups:transfer command to transfer data between applications. Docs available here.
Basically try:
heroku pgbackups:transfer HEROKU_POSTGRESQL_AMBER_URL <development app name>::HEROKU_POSTGRESQL_ORANGE -a <production app name>

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.

Quickest way to find out heroku database size

What is the quickest way to find out the current size of my shared database in Heroku?
running heroku info shows:
Data size: 480k
Addons: Basic Logging, Shared Database 5MB
Data size being the size of the shared database, here with a limit of 5MB.
From the same doc page as posted by nate c:
heroku pg:info
This only seems to work if you're not using the shared DB, but using PG as an add-on
Also, make sure your heroku gem is up to date:
sudo gem update heroku
Any ideas on how to see the size of a shared db? I just did a heroku db:pull and then a mysqldump and looked at the file size, figured that was a good rough estimate.
The new way seems to be:
heroku pg:info -a myapp
You'll see something like:
=== HEROKU_POSTGRESQL_PURPLE_URL (DATABASE_URL)
Plan: Dev
Status: available
Connections: 1
PG Version: 9.1.5
Created: 2012-10-19 01:27 UTC
Data Size: 12.1 MB
Tables: 31
Rows: 36068/10000 (Above limits, access disruption imminent)
Fork/Follow: Unavailable
Based on Nate's answer:
For shared DB w/o installing Postgres on your local machine
heroku console
ActiveRecord::Base.connection.execute("SELECT pg_size_pretty(pg_database_size('postgres'))").first
'postgres' is the name of my shared DB, when I put in template0 or template1 I get the same number back.
You can log into Posgtgresql directly.
type heroku pg:psql.
But you have to have postgres on your local system as well so you can use pgsql (which is the command line interface for pg.)
If you do not know what the db name is then type \l in pgsql to list databases. (postgres, template0, and template1 are system databases in every install.)
Then
SELECT pg_size_pretty(pg_database_size('dbname'));
In heroku postgres panel you can see everything https://postgres.heroku.com/

transfer db from one heroku app to another faster

Is there a faster way to transfer my production database to a test app?
Currently I'm doing a heroku db:pull to my local machine then heroku db:push --app testapp but this is becoming time consuming. I have some seed data but it is not nearly as accurate as simply testing with my real-world data. And since they're both stored on a neighboring AWS cloud, there must be a faster way to move the data?
I thought about using a heroku bundle, but I noticed the animate command is gone?
bundles:animate <bundle> # animate a bundle into a new app
It's quite common to migrate databases between staging, testing and production environments for Rails Apps. And heroku db:pull/push is painfully slow. The best way I have found so far is using Heroku PG Backups add-on and it's free. I followed following steps to migrate
production database to staging server:
1) Create the backup for the production-app db
heroku pg:backups capture --app production-app
This will generate b001 backup file from the main database (usually production db in database.yml)
2) To view all the backups (OPTIONAL)
heroku pg:backups --app production-app
3) Now use the pg:backups restore command to populate staging server database from the last backup file on production server
heroku pg:backups restore $(heroku pg:backups public-url --app production-app) DATABASE_URL --app staging-app
Remember that restore is a destructive operation, it will delete existing data before replacing it with the contents of the backup file.
So things are even easier now .. checkout the transfer command as part of pgbackups
heroku pgbackups:transfer HEROKU_POSTGRESQL_PINK sushi-staging::HEROKU_POSTGRESQL_OLIVE -a sushi
https://devcenter.heroku.com/articles/upgrading-heroku-postgres-databases#4b-alternative-transfer-data-between-applications
This has worked beautifully for me taking production code back to my staging site.
The correct answer has changed again as of March 11, 2015.
heroku pg:backups restore $(heroku pg:backups public-url --app myapp-production) DATABASE_URL --app myapp-staging
Note specifically that the argument is now public-url.
https://blog.heroku.com/archives/2015/3/11/pgbackups-levels-up
Update for mid-2015...
The pgbackups add-on has been deprecated. No more pgbackups:transfer. pg:copy is ideal for this scenario.
To copy a database from yourapp (example db name: HEROKU_POSTGRESQL_PINK_URL to yourapp_staging (example db name: HEROKU_POSTGRESQL_WHITE_URL)
# turn off the web dynos in staging
heroku maintenance:on -a yourapp-staging
# if you have non-web-dynos, do them too
heroku ps:scale worker=0 -a yourapp-staging
# backup the staging database if you are paranoid like me (optional)
heroku pg:backups capture -a yourapp-staging
# execute the copy to splat over the top of the staging database
heroku pg:copy yourapp::HEROKU_POSTGRESQL_PINK_URL HEROKU_POSTGRESQL_WHITE_URL -a yourapp-staging
Then when it's complete, turn staging back on:
# this is if you have workers, change '1' to whatever
heroku ps:scale worker=1 -a yourapp-staging
heroku maintenance:off -a yourapp-staging
Reminder: you can use heroku pg:info -a yourapp-staging (and yourapp) to get the database constants.
(source: https://devcenter.heroku.com/articles/upgrading-heroku-postgres-databases#upgrade-with-pg-copy-default)
psql -h test_host -c 'drop database test_db_name; create database test_db_name;'
pg_dump -h production_host production_db_name | psql -h test_host test_db_name`
This can be done on production_host or on test_host — will work both ways.
Have not tested this, but it might work.
Do this to get the URL of your source database:
heroku console "ENV['DATABASE_URL']" --app mysourceapp
Then try executing db:push with that.
heroku db:push database_url_from_before --app mytargetapp
This might not work if Heroku doesn't allow access to the DB machines from outside their network, which is probably the case. You could, perhaps, try using taps (gem that heroku db commands use internally) from within your app code somewhere (maybe a rake task). This would be even faster than the above approach because everything stays completely within AWS.
Edit:
Here's an (admittedly hacky) way to do what I described above:
Grab the database URL as in the first code snippet above. Then from a rake task (you could do it on console but you risk running into the 30 second timeout limit on console commands), execute a shell command to taps (couldn't easily determine whether it's possible to use taps directly from Ruby; all docs show use of the CLI):
`taps pull database_url_from_source_app #{ENV['DATABASE_URL']}`
The backticks are important; this is how Ruby denotes a shell command, which taps is. Hopefully the taps command is accessible from the app. This avoids the problem of accessing the database machine from outside Heroku, since you're running this command from within your app.
Heroku enables you to fork existing applications in production. Use heroku fork to copy an existing application, including add-ons, config vars, and Heroku Postgres data.
Follow the instructions on Heroku: https://devcenter.heroku.com/articles/fork-app
Update for mid-2016...
Heroku now have a --fast flag when creating forks, however they will be up to 30 hours out-of-date.
$ heroku addons:create heroku-postgresql:standard-4 --fork HEROKU_POSTGRESQL_CHARCOAL --fast --app sushi
https://devcenter.heroku.com/articles/heroku-postgres-fork#fork-fast-option

Resources