LoginSignup
17
8

Rails7+PostgresSQL+Tailwindでrails new

Last updated at Posted at 2023-09-10

はじめに

個人開発のアプリ制作を始めました。
調べながらの実装になるので、メモとして記載しています。

※注意※
プログラミング学習中の初心者です。
記述に間違いがあれば、ご指摘いただけますと幸いです。

環境

Mac M1
rails 7.0.7.2
ruby 3.2.0
Node.js 20.2.0
Yarn 1.22.19

やったこと

①rails new

最初に下記の通りrails newをすることで枠組みはできました。

$ rails new self_growth_diary --database=postgresql --css=tailwind

このままだとコミット対象のファイル数が5000件を超えており、githubにたくさんのファイルをpushしてしまうため、.gitignoreファイルにvendor/bundleを追記します。

# Ignore vendor/bundle directory
vendor/bundle

83件に収まりました。

②サーバーを起動

$ bin/dev

③データベース作成

データベースを作成して、rails new完了。

$ 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 'self_growth_diary_development' database. Please check your configuration.
rails aborted!
ActiveRecord::ConnectionNotEstablished: 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?

調べたところ、PostgreSQLサーバーを起動する必要がありました。

結論 

PostgreSQLをインストールし直して、
$ brew services start postgresqlでサーバー起動をした後、
$ bin/rails db:create で解決しました。

解決した手順

上記の③データベース作成でのエラーの後、本当に動いていないか状態について確認

$ brew services list

==> Tapping homebrew/services
Cloning into '/opt/homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Enumerating objects: 2492, done.
remote: Counting objects: 100% (313/313), done.
remote: Compressing objects: 100% (131/131), done.
remote: Total 2492 (delta 213), reused 206 (delta 181), pack-reused 2179
Receiving objects: 100% (2492/2492), 689.37 KiB | 5.03 MiB/s, done.
Resolving deltas: 100% (1152/1152), done.
Tapped 1 command (45 files, 859.2KB).
==> Downloading https://formulae.brew.sh/api/formula.jws.json
########################################################################################################################################## 100.0%
Name          Status User File
mysql         none        
postgresql@14 none        

postgresql@14 noneということで動いていなかったので、起動

$ brew services start postgresql@14

==> Successfully started `postgresql@14` (label: homebrew.mxcl.postgresql@14)

Successfullyって出たので、出来たと思い確認したところ、エラー・・・あれ??

 $ brew services list               
Name          Status   User         File
mysql         none                  
postgresql@14 error  6 username ~/Library/LaunchAgents/homebrew.mxcl.postgresql@14.plist

もう一度起動コマンドを打ってみました。

$ brew services start postgresql@14

Bootstrap failed: 5: Input/output error
Try re-running the command as root for richer errors.
Error: Failure while executing; `/bin/launchctl bootstrap gui/501 /Users/username/Library/LaunchAgents/homebrew.mxcl.postgresql@14.plist` exited with 5.

bootstrapは入れていないのに何故・・・!???
その後止めたり再起動させたり色々しましたが、結果PostgreSQLを入れ直したら解決しました。
結局原因が分かりませんでした。。。
下記アンインストール手順。

$ brew uninstall postgresql@14
Uninstalling /opt/homebrew/Cellar/postgresql@14/14.8... (3,315 files, 45.3MB)
Error: Could not remove postgresql@14 keg! Do so manually:
  sudo rm -rf /opt/homebrew/Cellar/postgresql@14/14.8

エラーが出たので、提示されたコードを打ったところ、正常終了
$ sudo rm -rf /opt/homebrew/Cellar/postgresql@14/14.8

再度インストール

$ brew install postgresql

==> Downloading https://formulae.brew.sh/api/formula.jws.json
########################################################################################################################################## 100.0%
Warning: Formula postgresql was renamed to postgresql@14.
Warning: Formula postgresql was renamed to postgresql@14.
==> Downloading https://formulae.brew.sh/api/cask.jws.json
########################################################################################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/postgresql/14/manifests/14.9
Already downloaded: /Users/ユーザー名/Library/Caches/Homebrew/downloads/4b57da1f9c51857d70615ba4a417e293db01f6d2309a29ca617dead69a8da01f--postgresql@14-14.9.bottle_manifest.json
==> Fetching postgresql@14
==> Downloading https://ghcr.io/v2/homebrew/core/postgresql/14/blobs/sha256:8b299896c59b81d488f837f20cda59dfc5fd6bba2e3140eb116d9771d9c53334
Already downloaded: /Users/ユーザー名/Library/Caches/Homebrew/downloads/b97d71a72b378d03e5e8c2fdf30a510384f163c45c43f1bfdd0888eb9889fe40--postgresql@14--14.9.arm64_ventura.bottle.tar.gz
==> Pouring postgresql@14--14.9.arm64_ventura.bottle.tar.gz
==> Caveats
This formula has created a default database cluster with:
  initdb --locale=C -E UTF-8 /opt/homebrew/var/postgresql@14
For more details, read:
  https://www.postgresql.org/docs/14/app-initdb.html

To start postgresql@14 now and restart at login:
  brew services start postgresql@14
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/postgresql@14/bin/postgres -D /opt/homebrew/var/postgresql@14
==> Summary
🍺  /opt/homebrew/Cellar/postgresql@14/14.9: 3,316 files, 45.3MB
==> Running `brew cleanup postgresql@14`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

再度PostgreSQLを起動。

$ brew services start postgresql
Warning: Formula postgresql was renamed to postgresql@14.
==> Successfully started `postgresql@14` (label: homebrew.mxcl.postgresql@14)

確認したところ、PostgreSQLが無事startedに変わりました。

$ brew services list              
Name          Status  User File
mysql         none         
postgresql@14 started ユーザー名 ~/Library/LaunchAgents/homebrew.mxcl.postgresql@14.plist

再度データベースを作成。やっとうまく行きました☺️

$ bin/rails db:create
Created database 'self_growth_diary_development'
Created database 'self_growth_diary_test'

image.png

参考

17
8
1

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
17
8