LoginSignup
4

More than 3 years have passed since last update.

【Windows(WSL)】PostgreSQLでRailsアプリを作成しよう

Posted at

WSL環境でPostgreSQLをインストールしてRails6アプリを起動するところまでの手順を記録しておきます。
なお、Rails標準のデータベース(以下DB)であるSQLite3での環境構築はできる前提で書いていきますね。
※WSL環境におけるRails6の環境構築はこちらでまとめております。

PostgreSQLのインストール

$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list
$ sudo apt-get install curl ca-certificates gnupg
$ curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
$ sudo apt install postgresql-11

インストールが完了できているか、バージョンを表示して確認しましょう。

$ psql -V
psql (PostgreSQL) 11.6 (Ubuntu 11.6-1.pgdg18.04+1)

パッケージもインストールしておきます。

$ sudo apt install libpq-dev

postgreSQLのユーザー名を作成

$ sudo su postgres -c 'createuser -s Ubuntuのログインユーザー名'

次のコマンドで以下のようになれば正常に動作しています。

$ psql postgres
psql (11.6 (Ubuntu 11.6-1.pgdg18.04+1))
Type "help" for help.

postgres=#

Railsアプリの作成

DBにPostgreSQLを指定してアプリを作成します。

$ rails new sample_app -d postgresql

作成したアプリのディレクトリに移動して、DBを作成します。

$ bin/rails db:create

Railsサーバーを起動します。

$ bin/rails s

http://localhost:3000/ にアクセスしてみましょう。

image.png

できましたー:sunny:

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
What you can do with signing up
4