Rails 3.0ã®ã¤ã³ã¹ãã¼ã«ã¯ãã¡ããåç §ã®ãã¨ã
PostgreSQLã®ã¤ã³ã¹ãã¼ã«ã¨ãã¼ã¿ãã¼ã¹ä½æ
% sudo aptitude install postgresql
PostgreSQLã®ã¹ã¼ãã¼ã¦ã¼ã¶ã¼ï¼ãµã¼ãã¼ã®ã¹ã¼ãã¼ã¦ã¼ã¶ã¼ã§ã¯ãªãï¼ã¯ãpostgresãªã®ã§ãæ°ããªã¦ã¼ã¶ã¼ããã¼ã¿ãã¼ã¹ãä½ãã¨ãã«ã¯ãpostgresã«ãªãã
% sudo su % su postgres % exit ï¼rootã«æ»ãã¨ãï¼ % exit ï¼å ã®ã¦ã¼ã¶ã¼ã«æ»ãã¨ãï¼
èªåã®ãã°ã¤ã³ã¢ã«ã¦ã³ãï¼ä»®ã«hogehogeã¨ããï¼ãPostgreSQLã®ã¦ã¼ã¶ã¼ã¨ãã¦å ãããå ´åã¯ä»¥ä¸ã®ããã«ããã
% sudo su % su postgres % createuser hogehoge ï¼ã¹ã¼ãã¼ã¦ã¼ã¶ã¼æ¨©éããã¼ã¿ãã¼ã¹ä½æ権éããã¼ã«ä½æ権éã«ã¤ãã¦èããããï¼ % exit ï¼rootã«æ»ãã¨ãï¼ % exit ï¼å ã®ã¦ã¼ã¶ã¼ã«æ»ãã¨ãï¼
Railsããã¸ã§ã¯ãããã¢ã¯ã»ã¹ããããã®ã¦ã¼ã¶ã¼ã¨ãã¼ã¿ãã¼ã¹ãä½æããã
- ã¦ã¼ã¶ã¼åï¼ hogehoge_rails
- ãã°ã¤ã³ãã¹ã¯ã¼ãï¼ 12345
- ãã¼ã¿ãã¼ã¹åï¼ hogehoge_rails_db
ã³ãã³ãã¯ä»¥ä¸ã®ã¨ããã
% sudo su % su postgres % createdb --encoding="utf-8" -T template0 hogehoge_rails_db % psql hogehoge_rails_db > \l ï¼ãã¼ã¿ãã¼ã¹ã®ä¸è¦§ã確èªï¼ > CREATE ROLE hogehoge_rails WITH LOGIN PASSWORD '12345'; > GRANT ALL ON DATABASE hogehoge_rails_db to "hogehoge_rails"; > \q ï¼psqlã®çµäºï¼ % exit % exit
Railsããã¸ã§ã¯ãã®è¨å®
app_30ã¨ããããã¸ã§ã¯ããæ¢ã«ããã¨ããã
ã¾ãã¯ãRubyããPostgreSQLã使ãããã®ã©ã¤ãã©ãªã¼ã®ã¤ã³ã¹ãã¼ã«ã
% sudo gem1.8 install pg
app_30/Gemfileã«pgã追å ãæ¬çªç°å¢ã¯PostgreSQLãéçºç°å¢ã¯SQLite3ã使ãã¨ãã¯ä»¥ä¸ã®ããã«ããã
group :development do gem 'sqlite3-ruby', :require => 'sqlite3' end group :production do gem 'pg' end
Gemfile.lockãçæããã
% bundle install --local --without development
app_30/config/database.ymlãç·¨éãããproductionã®é¨åã以ä¸ã®ããã«ããã
production: adapter: postgresql database: hogehoge_rails_db username: hogehoge_rails password: 12345 host: localhost encording: utf8
ç°å¢å¤æ°ã®RackEnvãproductionã«ãªã£ã¦ãããã¨ã確ãããã使ç¨ãã¦ããã·ã§ã«ãcshãtcshãªãã°.cshrcã«ä»¥ä¸ã®ããã«æ¸ãå ããã
setenv RackENV production
bashã®å ´åã¯ã.bashrcã«ä»¥ä¸ãæ¸ãå ããã
RAILS_ENV=production export RAILS_ENV
è¨å®ãã¡ã¤ã«ãèªã¿è¾¼ãã
% source ~/.cshrc ï¼csh, tcshã®å ´åï¼ % source ~/.bashrc ï¼bashã®å ´åï¼
ãã¼ã¿ãã¼ã¹ã®ä½æãè¡ãã
% cd app_30 % rake db:migrate
ãã¾ããã¼ãã«ãçæããã¦ããã°æåã
% sudo su % su postgres % psql hogehoge_rails_db > \dt ï¼ãã¼ãã«ã®ä¸è¦§è¡¨ç¤ºï¼ > \q ï¼psqlã®çµäºï¼ % exit % exit
追è¨ï¼2010å¹´10æ5æ¥ï¼: db:migrate:resetããã¾ããããªã
ãã®ããæ¹ã§ä½ã£ããã¼ã¿ãã¼ã¹ã¨ã¦ã¼ã¶ã¼ã§ã¯ãdb:migrate:resetãè¡ããã¨ããã¨å¤±æãããdb:migrate:resetã¯ä»¥ä¸ã®æé ã§é²ãã
- ãã¼ã¿ãã¼ã¹ã®åé¤
- ãã¼ã¿ãã¼ã¹ã®ä½æ
- ãã¼ãã«ã®ä½æ
ãã®ã¨ãããã¼ã¿ãã¼ã¹ã®ä½æãã§ããªãã
# rake db:migrate:reset (in /home/hogehoge/railstop/trunk) PGError: ERROR: new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII) HINT: Use the same encoding as in the template database, or use template0 as template. : CREATE DATABASE "hogehoge_rails_db" ENCODING = 'utf8' /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:202:in `log' /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:496:in `execute' /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:575:in `create_database' /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0/lib/active_record/railties/databases.rake:92:in `create_database' /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0/lib/active_record/railties/databases.rake:35 /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `each' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `execute' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain' /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:607:in `invoke_prerequisites' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:604:in `each' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:604:in `invoke_prerequisites' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:596:in `invoke_with_call_chain' /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in `invoke' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `each' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in `run' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/bin/rake:31 /usr/bin/rake:19:in `load' /usr/bin/rake:19 Couldn't create database for {"username"=>"hogehoge_rails", "adapter"=>"postgresql", "encording"=>"utf8", "database"=>"hogehoge_rails_db", "host"=>"localhost", "password"=>"12345"} rake aborted! FATAL: database "hogehoge_rails_db" does not exist (See full trace by running task with --trace)
ã¨ã©ã¼ã¡ãã»ã¼ã¸ãè¦ããããPostgreSQLã§ã¯ããã¼ã¿ãã¼ã¹ä½ææã«ãã³ãã¬ã¼ããåç §ãããã¨ã«ãªã£ã¦ããããããã§ãã¦ããªãã®ãåå ãããã
/usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0/lib/active_record/connection_adapters/postgresql_adapter.rbã®create_databaseã¨ããã¡ã½ãããè¦ãããããtemplateã¨ãããªãã·ã§ã³ãæå®ãã¦ããã°ã¡ããã¨ããSQLãåºåã§ããæ§åãããã§ã/usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0/lib/active_record/railties/databases.rakeãã¡ãã£ã¨ç·¨éãããã¨ã«ããã
% cp /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0/lib/active_record/railties/databases.rake lib/tasks/mydb.rake
以ä¸ã®ããã«å¤æ´ããã
% diff databases.rake mydb.rake 1c1 < namespace :db do --- > namespace :mydb do 92c92 < ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => @encoding)) --- > ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => @encoding, 'template' => 'template0'))
以ä¸ã®ããã«ã³ãã³ããæã¤ã¨PostgreSQLã§ãã¼ã¿ãã¼ã¹ãä½æã§ããã
% rake mydb:migrate:reset