RailsとPostgreSQLのインストールが前提
参考:http://qiita.com/torini/items/9952d91c4a7087b23481
必要なライブラリをインストール
$ sudo apt-get install libpg-dev
プロジェクト作成
$ rails new hello --database=postgresql
config/database.ymlの編集
$ vim hellp/config/database.yml default: &default adapter: postgresql encoding: unicode pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> development: <<: *default database: hello_development # 以下の2行を追加 username: username host: localhost test: <<: *default database: hello_test production: <<: *default database: hello_production username: username password: <%= ENV['HELLO_DATABASE_PASSWORD'] %>
必要なgemをインストール
※path解説:http://qiita.com/osamtimizer/items/3a241f365c85356394a8
$ cd hello $ bundle install --path vendor/bundle
データベース作成
$ rails db:setup Created database 'hello_development' Created database 'hello_test' -- enable_extension("plpgsql") -> 0.0116s -- initialize_schema_migrations_table() -> 0.0079s -- enable_extension("plpgsql") -> 0.0221s -- initialize_schema_migrations_table() -> 0.0380s
動作確認
$ rails g scaffold Task title:string body:text $ rails db:migrate == 20170112181531 CreateTasks: migrating ====================================== -- create_table(:tasks) -> 0.0172s == 20170112181531 CreateTasks: migrated (0.0179s) ============================= $ rails s
ブラウザでlocalhost:3000/tasksを確認