Programming

새로운 Rails 프로젝트에서 SQLite에서 PostgreSQL로 변경

procodes 2020. 7. 11. 23:28
반응형

새로운 Rails 프로젝트에서 SQLite에서 PostgreSQL로 변경


데이터베이스가 SQLite (개발자 및 프로덕션) 인 Rails 앱이 있습니다. heroku로 이동하고 있으므로 데이터베이스를 PostgreSQL로 변환하고 싶습니다.

어쨌든 로컬, 개발 및 데이터베이스를 SQLite에서 변경할 필요가 없다고 들었습니다. 따라서 변경할 필요는 없지만 프로덕션 환경을 SQLite에서 PostgreSQL로 변경하는 방법은 무엇입니까?

아무도 전에 이것을 해본 적이 있고 도울 수 있습니까?

추신 :이 프로세스가 정확히 무엇인지 확실하지 않지만 SQLite에서 PostgreSQL로 데이터베이스를 마이그레이션하는 것에 대해 들었습니다. 무엇을해야합니까?


즉시 사용 가능한 sqlite를 사용하는 대신 database.yml을 이것으로 변경할 수 있습니다.

development:
  adapter: postgresql
  encoding: utf8
  database: project_development
  pool: 5
  username: 
  password:

test: &TEST
  adapter: postgresql
  encoding: utf8
  database: project_test
  pool: 5
  username: 
  password:

production:
  adapter: postgresql
  encoding: utf8
  database: project_production
  pool: 5
  username: 
  password:

cucumber:
  <<: *TEST

아래 단계는 나를 위해 일했습니다. Heroku가 제작하고 Ryan Bates의 Railscast # 342에서 언급 한 보석을 사용합니다 . 몇 가지 단계가 있지만 완벽하게 작동했으며 (날짜가 올바르게 마이그레이션 되었음) 과거에 수행 한 Oracle-> DB2 또는 SQL Server-> Oracle 마이그레이션보다 훨씬 쉽습니다.

SQLite에는 사용자 ID 또는 비밀번호가 없지만 탭 보석에는 무언가가 필요합니다. 방금 문자 "user"와 "password"를 사용했습니다.

새 데이터베이스에 대한 Postgres 데이터베이스 사용자 생성

$ createuser f3
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) y

편집-아래의 업데이트 된 명령-대신 사용하십시오

$ createuser f3 -d -s

필요한 데이터베이스 생성

$ createdb -Of3 -Eutf8 f3_development
$ createdb -Of3 -Eutf8 f3_test

Gemfile 업데이트

gem 'sqlite3'
gem 'pg'
gem 'taps'
$ bundle

database.yml 업데이트

#development:
#  adapter: sqlite3
#  database: db/development.sqlite3
#  pool: 5
#  timeout: 5000

development:
  adapter: postgresql
  encoding: unicode
  database: f3_development
  pool: 5
  username: f3
  password:

#test:
#  adapter: sqlite3
#  database: db/test.sqlite3
#  pool: 5
#  timeout: 5000

test:
  adapter: postgresql
  encoding: unicode
  database: f3_test
  pool: 5
  username: f3
  password:

sqlite 데이터베이스에서 탭 서버를 시작하십시오.

$ taps server sqlite://db/development.sqlite3 user password

데이터 이전

$ taps pull postgres://f3@localhost/f3_development http://user:password@localhost:5000

Rails 웹 서버를 다시 시작하십시오

$ rails s

젬 파일 정리

#gem 'sqlite3'
gem 'pg'
#gem 'taps'
$ bundle

heroku로 이동 중이므로 탭을 사용하여 다음을 수행 할 수 있습니다.

heroku db:push

This will push your local development sqlite data to production, and heroku will automagically convert to postgres for you.

This should also work to push a production sqlite db to heroku, but it's not tested.

RAILS_ENV=production heroku db:push

you will also need to add the line "gem 'pg'" to your gemfile, 'pg' being the current postgres gem for Rails.


Simply update the config/database.yml file:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5

development:
  <<: *default
  database: projectname_development

test:
  <<: *default
  database: projectname_test

production:
  <<: *default
  database: projectname_production
  username: 
  password: 

The above is what's generated when you run:

$ rails new projectname --database=postgresql --skip-test-unit

Also add this to your Gemfile:

gem 'pg'

After replacing gem 'sqlite3 with gem pg in the gemfile, I kept getting the sqlite3 error when pushing to Heroku master because I forgot to commit the updated gemfile. Simply doing the following solved this:

git add .
git commit -m 'heroku push'
heroku create 
git push heroku master

Just Update you datatbase.yml

development: &development
  adapter: postgresql
  database: Your_database_name
  username: user_name
  password: password
  host:     localhost
  schema_search_path: public
  min_messages: warning

test:
  <<: *development
  database: test_database_name

production:
  <<: *development
  database: production_db_name

We are using rails and the basic standards should be follow like DRY, Convention over Configuration etc.. so in above code we are not repeating same code again and again.


It's been mentioned above me, but I don't have enough reputation as a lurker to be able to upvote it. In the hopes of drawing a little more attention for Rails newbies reading this answer:

you will also need to add the line "gem 'pg'" to your gemfile, 'pg' being the current postgres gem for Rails.

^^^ This is a key piece in addition to the database.yml file described in the selected answer to migrate your Rails app to Postgres.


Now its become easy with the command

bin/rails db:system:change --to=postgresql

if you have any doubts you can check here

https://github.com/rails/rails/pull/34832

This is how I have mine setup. If you are only using MRI and not Jruby you can skip the logic in the adapter settings.

defaults: &defaults
  adapter: <%= RUBY_ENGINE == 'ruby' ? 'postgresql' : 'jdbcpostgresql' %>
  encoding: unicode
  pool: 5
  timeout: 5000

development:
  database: project_development
  <<: *defaults

test:
  database: project_test
  <<: *defaults

production:
  database: project_production
  <<: *defaults

You can try following: sqlite3 development.db .dump | psql dbname username

or try with sqlitetopgscript: http://trac-hacks.org/browser/sqlitetopgscript/0.10/sqlite2pg


A possible solution (not for heroku) it's to use yaml.db from:

http://www.railslodge.com/plugins/830-yaml-db


Today I had the same issue. I'm working on Rails 4.2.8. The solution was specify the pg gem version, in my case, 0.18.4.

참고URL : https://stackoverflow.com/questions/6710654/change-from-sqlite-to-postgresql-in-a-fresh-rails-project

반응형