I'm on Ruby on Rails 3.2.13.
SQLite3 in local development environment.
Production environmet at Heroku (PostgreSQL).
What is the most updated (!= deprecated) and easiest way to pull the db from production to development with the above prerequisites?
you can do that with the taps gem
heroku db:pull
there are some posts about this topic at heroku as well:
https://blog.heroku.com/archives/2009/3/18/push_and_pull_databases_to_and_from_heroku
https://devcenter.heroku.com/articles/heroku-postgres-import-export
You can pull your latest production env from Heroku with a command like this:
curl -o latest.dump `heroku pgbackups:url`
From there you will want to convert the dump from PG to something that SQLite can read. The steps for that are:
Remove the lines starting with SET
Remove the lines starting with SELECT pg_catalog.setval
Replace true for âtâ
Replace false for âfâ
Add BEGIN; as first line and END; as last line
Lastly, you need to import that into SQLite.
sqlite3 db/development.sqlite3
sqlite> delete from schema_migrations;
sqlite> .read latest.sql
You can read details on this procedure here.
Related
We need to add the credentials of a database in database.yml file under a different environment like remote_database:
remote_database:
adapter:
encoding:
username:
...
And after adding all this, running the following command from the local terminal does the job:
RAILS_ENV=remote_database rails db:migrate
I'm trying to accomplish the same thing on Heroku. I have pushed the changes in config/database.yml, and I'm trying to execute the following command:
RAILS_ENV=remote_database heroku run rake db:migrate
# or
heroku run rake db:migrate RAILS_ENV=remote_database
Seems like Heroku is completely ignoring RAILS_ENV or the settings for remote_database env in config/database.yml file. Heroku always makes the changes in the regular database server connected with it that can be found at DATABASE_URL.
Is there a way to run the migrations on a different database server through Heroku?
Heroku injects database.yml and overrides it completely with Rails under 4.1 version or overrides partially and allows a way for us to prevent overriding from Rails 4.1. Check the complete explanation about Rails database connection behaviour on Heroku article
So, in your case
If you are using Rails 4.1+: you may try to add url key to your database.yml as described in Active Record 4.1+ Escape Valve section of above link.
If you are using Rails under 4.1 version: override database connection by an initializer. See Heroku article ("Otherwise if you are using an older version of Rails you will need to use an initializer" section)
Can someone please help me how can i pull single table from heroku database.
I have tried the following way:
heroku db:pull --tables table_name
with no luck as heroku db:pull is deprecated now.
Thanks in Advance
Not sure if you can pull a single table from heroku but there are certainly some options to pull whole database.You can use Herokus PG Backups add-on to import or export your database. First you'll have to add it as add on in your app by
$ heroku addons:add pgbackups
$ heroku update
After that you can download your database by
$ heroku pgbackups:capture
$ curl -o latest.dump `heroku pgbackups:url`
You can also use pg:transfer Heroku CLI plugin to transfer the data in a single step.
From your app's directory, copy your production database locally (assuming a local PG db), by installing the plugin and execute the pg:transfer command.
$ heroku plugins:install https://github.com/ddollar/heroku-pg-transfer
$ heroku pg:transfer
Not sure if this is still relevant, but you can achieve what you want pulling a CSV dump of a single table (or any other format) and then import it. The steps would be as follows:
# 1. Connect to psql on Heroku
$ heroku pg:psql --app your_app_name DATABASE
Then you can download a table like so:
# 2. Pull table_you_want_to_copy
your_app_name::DATABASE=> \COPY table_you_want_to_copy TO '~/local/path/your_table.csv' WITH (FORMAT csv, DELIMITER ',', HEADER true);
A simple version of an importer could look like this:
# 3. Seed local table from fetched CSV
CSV.foreach('~/local/path/your_table.csv, headers: true) do |row|
YourModel.create(row.to_hash)
end
In this block you can ofc define whatever other logic you might require.
I have facing problems with migrating data to my heroku app which has Postgresql as database for my hosted site(Production). At my development site i have rails 3.2.13 with Sqlite3 as database. I have followed Ruby on rails Tutorial by Michael Hartl
i have used git push heroku to update my site at heroku. i also want to update database along with data. But heroku run rake db:migrate migrates schema not data. I tried db:push to push data to heroku but i get error
dependency.rb in 'to_specs' :Could not find sequel (~) 3.20.0
also i have searched and found that i should first my sqlite data to dump.sql and then run
heroku pg:psql HEROKU_POSTGRESQL_COLOR --app app_name < file.sql as answered in https://stackoverflow.com/questions/15371394/...
but it failed with
the local psql command could not be located
please tell me what i am doing wrong. or what is the right way to update heroku postgresql with my development sqlite3 data.
Thanks in Advance
It is not a good idea to fill the production database with the data that you have now in the developement database. Because, if you have problems with your production database in the future, and you need to refill it again, your development db may changed (e.g dropped), and you are not going to be able to do it again.
For this need, Rails provides seeds in db/seeds.rb file. You should create all the neccessary objects there.
Then when you push your code to Heroku, Heroku is going to prepare the database, create the schema, and seed it. If you need to seed the db manually, you can run bundle exec rake db:seed, if you want to run it in Heroku: heroku run bundle exec rake db:migrate
I am trying to view my Heroku app's schema in Terminal (Mac OS X Lion) and stumbled upon a command that does just that. In Terminal, I run heroku run more db/schema.rb but it seems to display an older schema version. I just migrated the Heroku db and I noticed that none of the new columns are listed.
I can't seem to find anything helpful in Heroku's documentation. Does anyone know a command to view the current database schema for a Heroku app?
By the way, I inherited the code for the app and for some reason all of the migration files are commented out (there are probably 40+ files) so I can't just run rake db:migrate locally to update the schema; hence, I'd like to see the Heroku app's schema directly.
Any suggestions?
You could run heroku pg:psql to fire up a Postgres console, then issue \d to see all tables, and \d tablename to see details for a particular table.
For a rails schema, try:
$ heroku run "bundle exec rake db:schema:dump && cat db/schema.rb"
You can use rateaux:
rake db:view:schema
I have been trying to run $ heroku pg:reset from the command line but I believe I'm not putting in the database correctly. I've tried a number of variations.
I ran $ heroku config | grep POSTGRESQL to get the database name which prints as
HEROKU_POSTGRESQL_PINK_URL: postgres://naknaswvxfvuup:QK2dHYNMZ_va10lDgMDo4S0BIM#ec2-23-21-161-255.compute-1.amazonaws.com:5432/db7eute4gu4mcb
I've tried running everything from
#1
$ heroku pg:reset HEROKU_POSTGRESQL_PINK_URL: postgres://naknaswvxfvuup:QK2dHYNMZ_va10lDgMDo4S0BIM#ec2-23-21-161-255.compute-1.amazonaws.com:5432/db7eute4gu4mcb
#2
$ heroku pg reset postgres://naknaswvxfvuup:QK2dHYNMZ_va10lDgMDo4S0BIM#ec2-23-21-161-255.compute-1.amazonaws.com:5432/db7eute4gu4mcb
#3
$ heroku pg:reset db7eute4gu4mcb
and other variations. Please let me know how to correctly note this as I keep getting either an error or this text from the commmand line " ! Unknown database: db7eute4gu4mcb. Valid options are: DATABASE_URL, HEROKU_POSTGRESQL_PINK_URL"
I'm currently at 10.4 on the Ruby on Rails tutorial. Thanks!
You should specify a DATABASE when run heroku pg:reset. This is the syntax:
heroku pg:reset <DATABASE>
To know the value of , you can run:
heroku pg:info
It will return DATABASE_URL, something like: HEROKU_POSTGRESQL_GRAY_URL
Then you can reset your database:
heroku pg:reset HEROKU_POSTGRESQL_GRAY_URL
In your case, to reset database run:
heroku pg:reset HEROKU_POSTGRESQL_PINK_URL
this is how it worked for me
(replace app-staging with your app's name, do not replace DATABASE_URL, this is how heroku now finds the app's db)
heroku pg:reset DATABASE_URL --confirm app-staging
hope it helps
Assuming you have your authentication information exported to the shell environment, you should be fine with just passing the name of the database. For example:
PGPASSWORD='foobarbaz'
export PGPASSWORD
heroku pg:reset pink
There are certainly other ways to use the reset command, but IMHO this is the easiest.