Remaining space in heroku database? - ruby-on-rails

is there a way to find the remaining space in the (shared) heroku database? Doesn't matter whether this is done via commandline or from within the app, I'd just like to know how "big" my DB is.
Cheers

You can use pg:info from the command line:
$ heroku pg:info
Plan Ika
State available for 1 wk
Data size 2.56GB in 102 tables # <--------------------
PG version 9.0.4
Born 2011-03-03 16:01 PST
And then look at the "Data Size" value.

Related

Heroku + Rails: Heroku connecting to wrong database from CLI

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.

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.

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.

Heroku - how to pull data from one database and put it to another one?

We have 2 Heroku apps - the first one is production and the second one is staging. I would like to pull data from one table from the production app (it's table users with all user's data) and push it to the staging database.
After a little research I found the addon called pgbackups - I have just a few concerns:
Does this addon also allow to get data only from one table, not from the whole database?
The second thing is this - let's say that on production are users with IDs from 1 to 300. On the staging version users with IDs from 1 to 10. How to put those 300 users from the production to the staging version the way, that these 300 users would be counted from the ID 11 (we would like to keep our staging users in the staging database as well).
Thank you
There are ways to do this in straight SQL. If you're comfortable with that, go for it. This way is for devs comfortable in Rails -- so we pull data out using JSON, and create users with a new ID in the new database from that JSON.
Since you're pulling only 1 table, AND you want to reset the IDs to the new database, I recommend:
bring a copy of the database locally with pbackups
File.open('yourfile.json', 'wb') {|file| file << User.all.to_json }
Connect to your new database, and move yourfile.json up there
then:
users_json = JSON.parse(File.read('yourfile.json'))
users_json.each do |json|
json.delete("id")
User.create(json)
end
This script pulls data from your Heroku database into a local postgres database. You need the pgbackups addon installed. Execute it from the root directory of your Heroku app.
#!/bin/bash -ex
# This script asks Heroku to backup the current production data, then downloads that dump and imports it into the local db.
# It is at least 3 times quicker than heroku db:pull, and we can only do it because we are dumping from postgres to postgres.
heroku pgbackups:capture --expire
curl -o tmp/latest.dump `heroku pgbackups:url`
pg_restore --verbose --clean --no-acl --no-owner -d your_db_name tmp/latest.dump
You can check my answer in this thread. You can use a library called forceps to do exactly what you are asking for.

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/

Resources