LoginSignup
0
0

More than 3 years have passed since last update.

RailsチュートリアルでPostgreSQLを導入する

Last updated at Posted at 2021-03-02

はじめに

RailsチュートリアルでPostgreSQLを導入する方法について説明されている記事が見当たらなかったので書いてみました。エンジニアの方など、間違いを見つけられましたら、私を含むRailsチュートリアルからステップアップしたいエンジニア志望者にとって大変ありがたいので指摘していただけると嬉しいです。

参考

Railsにpostgresqlを導入する
初心者のMac + PostgreSQL インストール
PostgreSQLの環境構築 (macOS/Postgres 11)
PostgreSQLの基本的なコマンド

PostgreSQLをインストールする

Homebrewがインストールされていることを前提とする。

ターミナル
$ brew install postgresql

最新のPostgreSQLがインストールできていることを確認してみよう。

ターミナル
$ postgres --version
>> postgres (PostgreSQL) 13.2

問題なさそう。

データベースの初期化

ターミナル
$ initdb /usr/local/var/postgres -E utf8
The files belonging to this database system will be owned by user "username".
This user must also own the server process.

The database cluster will be initialized with locale "ja_JP.UTF-8".
initdb: could not find suitable text search configuration for locale "ja_JP.UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

initdb: directory "/usr/local/var/postgres" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/var/postgres" or run initdb
with an argument other than "/usr/local/var/postgres".

DBが無事に使えるかの確認

※筆者は不安なので確認していますが、今回の目的とは直接関係ありません

postgresサーバが無事に起動するかを確認してみる。いろいろ出てきた後に「server started」が表示されればOK。停止するには「pg_ctl stop」をターミナルに入力する。ただし、以降の操作は起動したまま行うこと。
(startを付けることでバックグラウンド起動してくれる。付けなくても起動してくれるけど起動中はそのターミナルは使えないから新しいウィンドウで操作する。)

ターミナル
#バックグラウンド起動
$ postgres -D /usr/local/var/postgres start

PostgreSQLが起動している状態で、psqlコマンドが使えるかを確認する。-lはデータベースを一覧表示してくれるオプションなので、データベースが3つぐらい出てきたら問題ない。

ターミナル
$ psql -l

環境変数にPATHを通す

アプリケーションがDBを探せるようにpostgresの場所を指定する。シェルがbashの人は、zshrcをbash_profileに変更して実行。

ターミナル
$ echo 'export PGDATA=/usr/local/var/postgres' >> ~/.zshrc
$ source ~/.zshrc

Railsで作るアプリケーションにPostgreSQLを導入する

ターミナル
$ cd (アプリの場所)
$ rails new sample_app(アプリ名) -d postgresql # データベースにPostgreSQLを指定
$ cd sample_app(アプリ名)
$ rails db:create #データベース構築
$ rails s

http://localhost:3000/ にアクセスしておなじみの画面が出ればOK

postgresサーバを停止してセッティングは終了

ターミナル
$ pg_ctl stop

RailsチュートリアルをPostgreSQLで進めるにあたっての注意点

6.1. ローカル環境

ローカルで確認しながら開発するときは、「Rails server」と同様にpostgresサーバも起動しておく。

6.2. Gemfile(3章)

3章のGemfileをインストールする際には、開発環境とテスト環境に「sqlite3」ではなく「pg」を記述する。

Gemfile
# 変更前
group :development, :test do
  gem 'sqlite3', '1.4.1'
  gem 'byebug',  '11.0.1', platforms: [:mri, :mingw, :x64_mingw]
end

# 変更後
group :development, :test do
  gem 'pg' #バージョンは本番環境と同じにすることを推奨
  gem 'byebug',  '11.0.1', platforms: [:mri, :mingw, :x64_mingw]
end

6.3. Userモデル(6章)

6章でUserモデルを作った後は、データベースを確認したくなると思うのでその方法を残しておく。

PostgreSQLを起動した状態で、アプリケーションがある場所まで移動する。

ターミナル
$ postgres -D /usr/local/var/postgres start
$ cd sample_app(アプリケーション名)

「rails console」を使ってデータの追加、削除はSQLiteと同じように可能。

ターミナル
$ rails console
irb> user = User.new(name: "EXAMPLE")
irb> user.save

また、「rails dbconsole」からデータベースの一覧を表示したり、実際のテーブルを確認することができる。

ターミナル
$ rails dbconsole
sample_app_development=> \l #データベース一覧を表示
sample_app_development=> \dt; #テーブル一覧を表示
sample_app_development=> \d users; #Userテーブルの構造を表示
sample_app_development=> select * from users; #Userテーブルのすべてのデータを表示

お疲れ様でした。

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