switching from Sqlite3 to PostgreSQL on an existing rails application

淺唱寂寞╮ 提交于 2020-01-07 02:01:50

问题


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 ?


回答1:


This Railscast is a great resource on migrating, and should answer most of your questions:

http://railscasts.com/episodes/342-migrating-to-postgresql




回答2:


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!




回答3:


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

rake db:seed



回答4:


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



来源:https://stackoverflow.com/questions/18149293/switching-from-sqlite3-to-postgresql-on-an-existing-rails-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!