在现有 Rails 应用程序上从 Sqlite3 切换到 PostgreSQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18149293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
switching from Sqlite3 to PostgreSQL on an existing rails application
提问by Mostafa Hussein
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
当我尝试在现有应用程序中从 sqlite3 切换到 postgresql 时,我遇到了这个问题rake db:migrate
,我执行了以下操作
1 - rake db:create
1 - 耙分贝:创建
2- rake db:migrate
I got this error:
2-rake db:migrate
我收到此错误:
== 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
== 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
3- rake db:reset
4- rake db:migrate
, now migration is done with no errors
4- rake db:migrate
,现在迁移完成,没有错误
I have lost my data specially my admin user because of rake db:reset and my questions is :
由于 rake db:reset 我丢失了我的数据,特别是我的管理员用户,我的问题是:
1- why I forced to use rake db:reset
?
1-为什么我被迫使用rake db:reset
?
2- is there ways to transfer my data from a database engine to another without losing it in the next time ?
2- 有没有办法将我的数据从一个数据库引擎传输到另一个数据库引擎而不会在下次丢失它?
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 ?
3-对于 PostgreSQL 我不能使用空白密码,它说fe_sendauth: no password supplied
,添加密码后这个错误消失了,必须在另一个数据库引擎中使用密码而不是 Sqlite3 吗?
4- will heroku require a reset also or its like Github will accept the data if I use another db engine in development ?
4- 如果我在开发中使用另一个数据库引擎,heroku 是否也需要重置或类似 Github 会接受数据?
回答by Drew
This Railscast is a great resource on migrating, and should answer most of your questions:
这个 Railscast 是一个很好的迁移资源,应该可以回答你的大部分问题:
回答by Yosep Kim
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.
那么,您的意思是您在 sqlite3 数据库中丢失了数据?这不应该发生。您是否更改了 config 文件夹中的 database.yml 以将您的应用程序指向新的 Postgres DB?如果您确实将其指向新的数据库,那么您应该不会丢失 sqlite3 数据库中的任何数据,因为应用程序不再访问它。
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 ?
1- 为什么我被迫使用 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.
我认为您不必在新数据库上运行 rake db:reset ,除非您的迁移文件搞砸了。
2- is there ways to transfer my data from a database engine to another without losing it in the next time ?
2- 有没有办法将我的数据从一个数据库引擎传输到另一个数据库引擎而不会在下次丢失它?
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.
您可以创建一个 rake 任务或一个脚本,指向两个 db 实例(旧的和您要迁移到的新实例)。然后,您可以从旧数据库中提取数据,然后根据需要处理数据(每个 RDBM 都有自己的细微语法差异)并将它们加载到新数据库中。
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 ?
3-对于PostgreSQL,我不能使用空白密码,它说fe_sendauth:没有提供密码,添加密码后这个错误消失了,必须在另一个数据库引擎中使用密码而不是Sqlite3?
It depends how you set up your Postgres DB instance you created...
这取决于您如何设置您创建的 Postgres 数据库实例...
4- will heroku require a reset also or its like Github will accept the data if I use another db engine in development ?
4- 如果我在开发中使用另一个数据库引擎,heroku 是否也需要重置或类似 Github 会接受数据?
Setting up your DB on Heroku would be very simple. All you have to do is the following:
在 Heroku 上设置数据库非常简单。您所要做的就是:
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.
但是,我认为您的迁移文件中存在一些问题。尽管如此,您实际上可以在 Heroku 上为您的数据库获取用户名/密码,然后使用您的脚本将数据从本地数据库推送到它。
I hope this helps!
我希望这有帮助!
回答by ardochhigh
If you are using Devise, you can reload your user account information using:
如果您使用的是 Devise,您可以使用以下方法重新加载您的用户帐户信息:
rake db:seed
回答by John David Barbosa
Take a look on this guide https://www.digitalocean.com/community/tutorials/how-to-set-up-ruby-on-rails-with-postgresso you can recreate the process manually by changing your database.yml and Gemfile.
查看本指南https://www.digitalocean.com/community/tutorials/how-to-set-up-ruby-on-rails-with-postgres,以便您可以通过更改 database.yml 手动重新创建该过程和 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/
检查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
Taps 等其他替代方法现在无法正常工作 http://railscasts.com/episodes/342-migrating-to-postgresql