LoginSignup
4
2

More than 3 years have passed since last update.

Railsにpostgresqlを導入する

Last updated at Posted at 2019-08-30

今までRailsのデータベースにmySQLを使用してきましたが
herokuを使うにあたりRailsでpostgreSQLを導入する
手順をまとめました。

$がついてるコマンドはターミナル上で実行してください。

postgreSQLのインストール

$ brew install postgresql

バージョンの確認

$ psql -V
psql (PostgreSQL) 11.5

データベースの初期化

$ 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".

Postgresのバージョンチェック

$ postgres --version
postgres (PostgreSQL) 11.5

PostgresSQLの起動

$ postgres -D /usr/local/var/postgres
2019-08-29 23:50:34.469 JST [22297] LOG:  listening on IPv6 address "::1", port 5432
2019-08-29 23:50:34.469 JST [22297] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2019-08-29 23:50:34.471 JST [22297] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2019-08-29 23:50:34.498 JST [22298] LOG:  database system was shut down at 2019-08-29 23:27:02 JST
2019-08-29 23:50:34.519 JST [22297] LOG:  database system is ready to accept connections

この時のIPv4 address "127.0.0.1"はローカル・ループバック・アドレスと呼ばれる
localhostのアドレスの実態がこれ。

データベースの確認

ターミナル上で
commant + T (新しいタブ)
もしくは
command + N (新しいウィンドウ)
で別のターミナルを開き、下記のコマンドでデータベースを確認する

$ psql -l
                                         List of databases
   Name    |      Owner       | Encoding | Collate | Ctype |           Access privileges           
-----------+------------------+----------+---------+-------+---------------------------------------
 postgres  | username         | UTF8     | C       | C     | 
 template0 | username         | UTF8     | C       | C     | =c/username                  +
           |                  |          |         |       | username=CTc/username
 template1 | username         | UTF8     | C       | C     | =c/username                  +
           |                  |          |         |       | username=CTc/username
(3 rows)

3つのデータベースが作られているのが確認できる。

postgresを終了させる

ターミナル上で control + c

パスを通す

Postgresを簡単に起動できるようにパスを通す

$ vi ~/.bash_profile
上記コマンドでvimに入って、「i」でインサートモードにする
export PGDATA=/usr/local/var/postgres
を追記する
escでインサートモードを終了
:wqで変更を保存してvimを終了

上の流れは

$ echo 'export PGDATA=/usr/local/var/postgres' >> ~/.bash_profile

で同じことをしたことになる。

変更した.bash_profileを読み込む

$ source ~/.bash_profile

パスを通した状態での起動を確認する

$ postgres

接続を確認

ターミナル上で
commant + T (新しいタブ)
もしくは
command + N (新しいウィンドウ)
で新しいターミナルから接続を確認する

$ psql -d postgres

ここからRails

$ cd projects
$ rails new <アプリの名前> -d postgresql
$ cd <アプリの名前>
$ rails db:create
$ rails s

http://localhost:3000/
にアクセスするとYay!がみれ・・・ない。

PG::ConnectionBad (could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

エラーが発生している様子。

解消するために、brew経由でインストールしたpostgreを再起動させる

$ brew services start postgresql
$ brew services stop postgresql
$ brew services restart postgresql

Railsを起動させる

$ rails s

データベースがないとのエラーが発生。

ActiveRecord::NoDatabaseError (FATAL:  database “アプリ名” does not exist

なので、データベースを作り押して再度Rails起動

$ rails db:create
$ rails s

http://localhost:3000/
これでYay!が見れるようになった!

参考

https://qiita.com/longtime1116/items/9045717ff8607bed07fe
https://make-from-scratch.com/error-connections-on-unix-domain-socket-tmp-s-pgsql-5432/

4
2
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
4
2