LoginSignup
0
0

More than 5 years have passed since last update.

[学習用]Ubuntu16+Rails5+Postgresql でアプリ作成までの流れ

Last updated at Posted at 2018-05-24

UbuntuにPostgresql をインストールしたら
ユーザーを作っておく。

Postgres

Postgresのユーザーを作成

$ psql --version
$ sudo -u postgres createuser -s pguser
$ sudo -u postgres psql

pguser というユーザーを作成しました。

Postgresのパスワードを作成

postgres=# \password pguser
postgres=# \du
postgres=# \q

2回パスワードを入力。
例えば pasword って2回入力すると パスワードが password になります。
\du でユーザーの一覧を表示できますので、pguserがあるか確認しておきます。
\q で脱出。

Railsでアプリを作成

Postgresを指定しておく

$ rails new myapp --database=postgresql
$ cd myapp

いつも通りアプリを作成します。

ジェムファイルを変更

# 開発時はSQLiteで
# gem 'sqlite3', group: :development
# 本番ではpostgresqlを使用する場合は
# gem 'pg', group: :production

#変えない場合は
gem 'pg'

Gemfileに上記を追加・変更します。

コントローラー作成

$ bundle install
$ rails g controller welcome

テスト用のファイルを作ります。

indexファイル編集

app/views/welcome/index.html.erb
<h2>Hello World</h2>
<p>
  The time is now: <%= Time.now %>
</p>

ただ現在時間が表示されるアプリというかコード。

routes編集

config/routes.rb
  root 'welcome#index'

トップページを今作ったファイルへ変更しておきます。

database編集

config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  #以下を追加する3つ
  host: localhost
  username: pguser
  password: password

ホストとユーザー名とパスワードを追記します。

データベース作成?してサーバー起動

$ rake db:create
$ rails s

これで動きました。
rake db:crete の後に rake db:migrate もしたような気も。

参考サイト

解決するのに参考にした:英語サイト
解決するのに参考にした:Qiita
参考:Getting Started with Rails 5.x on Heroku

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0