switching from Sqlite3 to PostgreSQL on an existing rails application - ruby-on-rails

When I tried to switching from sqlite3 to postgresql in my existing application I faced this problem with rake db:migrate, I did the following
1 - rake db:create
2- rake db:migrate I got this error:
== AddColumn1: migrating ===================================================== -- add_column(:users, :first_name, :string) rake aborted! An error has occurred, this and all later migrations canceled: PG::Error: ERROR: relation "users" does not exist
3- rake db:reset
4- rake db:migrate , now migration is done with no errors
I have lost my data specially my admin user because of rake db:reset and my questions is :
1- why I forced to use rake db:reset ?
2- is there ways to transfer my data from a database engine to another without losing it in the next time ?
3- and for PostgreSQL I couldn’t use a blank password , it said fe_sendauth: no password supplied , after adding password this error is gone , using password in another database engine instead of Sqlite3 is a must ?
4- will heroku require a reset also or its like Github will accept the data if I use another db engine in development ?

So, what you are saying is you lost data in your sqlite3 database? This should not have happened. Did you change your database.yml in config folder to point your app to the new Postgres DB? If you did point it to the new DB, you should not have lost any data in sqlite3 DB since the app is not hitting it any more.
If you have indeed changed your db config to use the new db, you may not have lost any data. I hope this is the case. :)
1- why I forced to use rake db:reset ?
I don't think you had to run rake db:reset on your new DB, unless your migration files are messed up.
2- is there ways to transfer my data from a database engine to another without losing it in the next time ?
You can create a rake task or a script that points to two db instances (the old one and the new one you are migrating to). You can then pull data from the old db and then massage data as needed (each RDBM has their own minor syntactical differences) and the load them to the new db.
3- and for PostgreSQL I couldn’t use a blank password , it said fe_sendauth: no password supplied , after adding password this error is gone , using password in another database engine instead of Sqlite3 is a must ?
It depends how you set up your Postgres DB instance you created...
4- will heroku require a reset also or its like Github will accept the data if I use another db engine in development ?
Setting up your DB on Heroku would be very simple. All you have to do is the following:
heroku crate your-app-name
git push heroku master
heroku run db:migrate
But, I think you have some issues in your migration files. Nevertheless, you can actually get a username/password for your DB on Heroku and then use your script to push the data over to it from your local DB.
I hope this helps!

If you are using Devise, you can reload your user account information using:
rake db:seed

Take a look on this guide https://www.digitalocean.com/community/tutorials/how-to-set-up-ruby-on-rails-with-postgres so you can recreate the process manually by changing your database.yml and Gemfile.
If you need to migrate your database information run
pgloader ./production.sqlite3 postgres://username:password#localhost/database
Check https://migara.li/2017/07/24/migrating-from-sqlite-to-postgres/
Other alternatives like taps aren't working right now
http://railscasts.com/episodes/342-migrating-to-postgresql

Related

Heroku Schema load: Permission denied for database "postgres" User

I'm trying to deploy my Rails app to Heroku, and I now want to load my schema. I get the following error:
Running `bundle exec rake db:schema:load` attached to terminal... up, run.1533
rake aborted!
PG::ConnectionBad: FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
Which is weird because postgres is not the database it should connect to. The database URL, which is available to the app via ENV, has the right values, and the long apparently random name d3kwlkwlwjwljetc....
Searching for this message gives me people who are trying to drop or reset the DB, which is not what I'm doing.
I have 0 rows
I tried pg:reset, even removed and put back the database. No avail.
I'm not doing db:drop, just db:schema:load
I'm dumbfounded. Why can't my app connect?
Edit: Apparently, I can't schema:load, but I can migrate all my migrations over. Why is that?
It appears that this is an issue in Rails itself - there is a discussion regarding it at https://github.com/rails/rails/issues/17945 - it seems that there's a commit (https://github.com/rails/rails/blob/master/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb#L77) that is connecting to the posgres database which isn't permitted on Heroku so fails.
In short, for the moment use db:migrate until the issue is resolved.

How to reset synch with a database on rails?

I am having a lot of issues with rails migration mechanism.
I think I have run a migration file and has been executed partially.
So when I am trying to run
rake db:migrate
again it gives me an error the column name already exist.
I am trying to reset it with
rake db:reset
and gives me an error
Unknown database 'databasename'
Is there a way to reset the whole mechanism ?
Is it a good idea to manually drop all tables and try to run rake db:migrate again ?
You should change the database name in your database.yml.

Migrate Data and schema from development to production rails heroku

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

Can't recreate postgres db in rails app

So, had a working postgres db instance with my rails app on my development Mac, but the migrations were getting unmanageable as I'm building the model up from scratch (i.e. adding, removing, changing columns), so I decided to start clean. After running rake db:drop, I can not recreate via rake db:create. I simply get the following:
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"xyz_development", "pool"=>5, "username"=>"xyz", "password"=>nil}
I am thinking I need to create the user in postgres, but shouldn't it be there from initial setup?
Thanks.
You should see the SQL statement to create the database in the log output, and probably also the error. Check that.
Also check that the database doesn't already exist. Try creating the database manually using psql(1) with the username/password you use in rails and see what happens.
$ createdb xyz_development
Use the postgres bin createdb

Rails Heroku Database Population

After deploying an app of Heroku I am having trouble getting the database populated with some of the data I need there. I ran heroku rake db:populate and it did create the initial admin user, but failed to put the rest of the data in.
I am populating the database with files from my local disk. I suspect the problem is caused because it sees nothing in the directory listed in the sample_data.rake file, as it in on my hdd and not the server. How can I get around this?
I figure that I have to either host all the files and change the directory to the servers directory, or find a way around this. I would guess there is an easy way to move the database from my computer to heroku? I'm obviously pretty new to this.
Thanks!
heroku commands have changed. now it should be:
heroku run rake db:push
heroku pg:reset DATABASE
heroku run rake db:seed
also data from local database can be uploaded to heroku by installing taps gem and pushing data from local PostgreSQL db.
gem install taps
heroku db:push
If you have a clean local database (i.e. just the good stuff, no silly testing data), then you could try heroku db:push:
db:push [<database_url>] # push a local database into the app's remote database
And please remember to develop on PostgreSQL if you're going to use the Heroku shared or dedicated databases, using the same database and version (version 8.3 for shared, 9.0 for dedicated) in both your development and production environments will save you much pain, suffering, and confusion.
References:
How can I import my existing data to Heroku?
Import: Push to Heroku
I setup the development db, and push it to Heroku.
rake db:reset
rake db:seed
heroku rake db:push
If you are using the default PostgreSQL, another option is heroku pg:reset
heroku rake db:seed
I use the following in the seed.rb file:
require 'pathname'
RailsRoot = Pathname.new(RAILS_ROOT).expand_path
print "Loading data..."
fileData = File.read (RailsRoot + "db/data-file.csv")

Resources