用Rails7+PostgresSQL+Tailwind创建新的Rails应用
首先
我开始开发个人应用程序。由于要边查资料边实施,所以我将它作为备忘录。
※注意※
我是一位正在学习编程的初学者。
如果我有任何写作错误,请您指正,将不胜感激。
环境
苹果电脑 M1
Rails 7.0.7.2
Ruby 3.2.0
Node.js 20.2.0
Yarn 1.22.19
做过的事情 de
创建一个新的Rails项目
通过最初进行以下步骤`rails new`,框架已经建立。
$ rails new self_growth_diary --database=postgresql --css=tailwind
由于要提交的文件数量已超过5000个,并且为了避免向GitHub推送大量文件,我将在.gitignore文件中添加vendor/bundle的内容。
# Ignore vendor/bundle directory
vendor/bundle
总共有83件。
请启动服务器。
$ bin/dev
创建数据库
创建数据库并完成了Rails新建。
$ 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未运行,因此需要启动。
$ brew services start postgresql@14
==> Successfully started `postgresql@14` (label: homebrew.mxcl.postgresql@14)
由于成功的消息传来,我以为完成了,于是确认了一下,结果出现了错误……咦?
$ 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已成功启动。
$ 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'

请提供以下信息供参考。