Quickest way to find out heroku database size - ruby-on-rails

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/

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.

Transfer initial PostgreSQL database from development to Heroku production

I have an initial set of production data stored locally in the development database that I'd like to migrate to production for a starting point for data. What's the best way to transfer this data?
It does not seem evident if there is a way to use pgbackups as per the instructions. Perhaps I have to run a manual backup of some sort locally and then push it over with pgbackups and if that is the case, I'd appreciate some specific instructions on accomplishing this.
After some additional digging and an answer from Heroku, the answer for importation of initial data is to:
1) If using PGSQL locally, first dump the data:
pg_dump -U your_username your_database_name -f backup.sql
2) Then follow the instructions found here for importation to Heroku's database:
http://devcenter.heroku.com/articles/pgbackups#importing_from_a_backup
First dump your local database using pg_dump:
pg_dump -Fc --no-acl --no-owner -h ... mydb > mydb.dump
and then use heroku pgbackups:restore:
heroku pgbackups:restore heroku-database-url url-to-your-mydb.dump-file
Note that the mydb.dump file needs to be accessible by the Heroku servers.
The Heroku Dev Center page has detailed instructions:
https://devcenter.heroku.com/articles/heroku-postgres-import-export

Remaining space in heroku database?

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.

Resources