LoginSignup
55
67

More than 3 years have passed since last update.

Ruby on Railsの開発環境でPostgreSQLを利用する

Last updated at Posted at 2017-07-15

開発環境でもPostgerSQLを利用したかったので導入した際の防備録です。

環境

Linux(Ubuntu)
Ruby 2.5.3
Rails 5.2.1.1

補足

下記、コード記載部分の<>は、実際に入力する時は不要です。自分で決めるところを<>で現しています。

PostgreSQL Account を作る

元々、PostgreSQLはInstall済みだったのでAccountの設定から始めました。

$ sudo -u postgres psql
postgres=# create role <username> with createdb login password '<password>';
postgres=# \q

Rails new

rails new <appname> -d postgresql
cd <appname>

config/database.ymlの編集

config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  # 以下、3行追加
  username: <username> # 設定したPostgreSQL Accountと同一のもの
  password: <password> # 設定したPostgreSQL Accountと同一のもの
  host: localhost
development:
  <<: *default
  database: appname_development # appnameのところは、rails new時のappnameになっているはずです。
test:
  <<: *default
  database: appname_test # appnameのところは、rails new時のappnameになっているはずです。

  # 以下、省略

rails db:createでデータベースを作成

下記のrailsコマンドでconfig/database.ymlの内容でデータベースを作成してくれます。

rails db:create

以降、localhost:3000にBrowserでAccessしてrailsの画面が表示されたので問題ないかと思います。

参考

インストールからRails-PostgreSQL環境を整える

55
67
1

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
55
67