rails newでアプリを立ち上げるまで postgresql

やりたいこと

rails newしてアプリを作成していきたいが、様々なエラーに出くわしたので改善していきます。DBはpostgresqlを使用します。



エラー内容

エラー1

% rails _6.0.4_ new calender --database=postgresql --skip-test
    2: from /Users/urakamitakuya/.rbenv/versions/2.7.2/bin/rails:23:in `<main>'
  1: from /Users/urakamitakuya/.rbenv/versions/2.7.2/lib/ruby/2.7.0/rubygems.rb:296:in `activate_bin_path'
/Users/.rbenv/versions/2.7.2/lib/ruby/2.7.0/rubygems.rb:277:in `find_spec_for_exe': can't find gem railties (= 6.0.4) with executable rails (Gem::GemNotFoundException)
 % rails -v
Rails 6.0.4

これはエラー文の通り。can't find gem railties (= 6.0.4) gem install railsで解決できました。

gem install rails -v 6.0.4



エラー2

rails _6.0.4_ new . --database=postgresql --skip-test

無事rails newできたのですが、bin/rails db:createを実行するとエラーが発生してしまいます。

% bin/rails db:create
connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
    Is the server running locally and accepting connections on that socket?
Couldn't create 'calender_development' database. Please check your configuration.
rails aborted!
PG::ConnectionBad: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory

以下の記事を参考。 mysql使いがpostgresqlを入れてみた - Qiita

 % postgres -D /usr/local/var/postgres
2021-12-10 11:20:09.396 JST [43403] FATAL:  database files are incompatible with server
2021-12-10 11:20:09.396 JST [43403] DETAIL:  The data directory was initialized by PostgreSQL version 13, which is not compatible with this version 14.0.
% psql -V
psql (PostgreSQL) 14.0

どうやらデータベース本体のバージョンと今のpostgresqlのバージョンが合っていなかったようなので、

urakamitakuya@urakamiuyanoMBP calender % brew services list 
512
Name          Status  User          File
dbus          stopped               
mysql@5.7     started urakamitakuya ~/Library/LaunchAgents/homebrew.mxcl.mysql@5.7.plist
postgresql    error   urakamitakuya 
postgresql@13 stopped 

データベース本体のバージョン13を起動。するとbin/rails db:createが起動し、アプリが立ち上がりました。

% brew services start postgresql@13