Quantcast
Channel: 初心者タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 21764

PostgreSQLをMacへインストールしてRails newする

$
0
0

概要

ポートフォリオ作成時、今までずっとMySQLに慣れ親しんでいたので、
PostgreSQLとやらに挑戦しようと思い、とりあえずインストール方法〜新規Railsアプリ作成までをまとめてみました!

条件

  • macOS Mojave(ver 10.14.5)

手順

  • Homebrewを使用してPostgreSQLをインストール
  • PostgreSQLの起動 / 起動確認 / 停止方法
  • データベース一覧を表示
  • Ruby on Railsのバージョンを変更
  • アプリケーションを新しく作成
  • GitHubに登録し、ローカルリポジトリを作成
  • リモートリポジトリを作成

Homebrewを使用してPostgreSQLをインストール

postgresqlをインストール

brew install postgresql

バージョン確認

psql -V

インストール場所を確認

which psql

PostgreSQLの起動 / 起動確認 / 停止方法

起動

brew services start postgresql

起動状況の確認

brew services list

停止

brew services stop postgresql

データベース一覧を表示

データベース一覧を表示

                                            List of databases
         Name          |     Owner      | Encoding | Collate | Ctype |         Access privileges         
-----------------------+----------------+----------+---------+-------+-----------------------------------
 postgres              | ************** | UTF8     | C       | C     | 
 test-app_development  | ************** | UTF8     | C       | C     | 
 test-app_test         | ************** | UTF8     | C       | C     | 
 template0             | ************** | UTF8     | C       | C     | =c/**************                +
                       |                |          |         |       | **************=CTc/**************
 template1             | ************** | UTF8     | C       | C     | =c/**************                +
                       |                |          |         |       | **************=CTc/**************

Ruby on Railsのバージョンを変更

cd ~ #ホームディレクトリに移動
gem install rails --version="5.2.4.1" #今回はversion="5.2.4.1"を使用
rbenv rehash #gemの変更を反映

アプリケーションを新しく作成

cd
rails new test-app -d postgresql #rails newコマンドを実行
cd test-app #test-appディレクトリに移動
rails db:create #データベースを作成

GitHubに登録し、ローカルリポジトリを作成

GItHub Desktop から test-app のローカルリポジトリを作成。
左上の「Current Repository」→「Add」→「Add Existing Repository」を選択。

※「Add Repository」がクリックできない場合、事前に該当するアプリケーションのディレクトリで以下の作業を行う。

cd ~/git-app
git init #rails のバージョンによっては rails new 時に git init がされないケースがあるため

その後コミット。

リモートリポジトリを作成

[Publish repository]ボタンをクリックすると GitHub のリモートリポジトリが作成できる。
GitHub の Your repositories の欄に作成したアプリがあれば上手くいっている。

サーバー起動

rails s

できた!制作スタート!

Image from Gyazo


Viewing all articles
Browse latest Browse all 21764

Trending Articles