概要
- Ruby on Rails速習ガイドを読んでいて、Railsの環境構築を行なっていたらpostgresqlで詰まった内容を記録しました。
- 途中でpsqlコマンドが使えないことが発覚し、そちらの対応内容も記録してあります。
postgresqlがインストールできない
$brew install postgresql
Warning: No available formula with the name "homebrew/core/postgresql".
Please tap it and then try again: brew tap homebrew/core
上記のエラー文が表示されてしまい「?」って状態に脳が陥った。
そこでインストール方法がほかにないか調べてみると、基本的に
上記のコマンド
$brew install postgresql
を他の方も実行してpostgresqlを入れているのが大半だった。
brew のpostgresqlパッケージ一覧を確認する
以前Rails の環境構築でDockerのコンテナ環境で構築した際もこういったインストールしたいパッケージのバージョンを指定せずに行った結果エラーに苛まれました。
その時はNodeでしたが、今回のPostgreSQLもそうなのか。。。?
と思い、以下のコマンドを実行します。
$brew serch postgresql
==> Formulae
postgresql@10 postgresql@12 postgresql@14 postgresql@9.4 qt-postgresql
postgresql@11 postgresql@13 postgresql@15 postgresql@9.5 postgrest
==> Casks
navicat-for-postgresql
If you meant "postgresql" specifically:
postgresql breaks existing databases on upgrade without human intervention.
See a more specific version to install with:
brew formulae | grep postgresql@
バージョン等が表示されているので、バージョン指定してインストールを実行してみます。
今回はpostgresql@13を指定してみました
$brew install postgresql@13
最初とは違い、エラー表記なく進んでいきました。
インストールが完了したか以下のコマンドで確認してみます。
$postgres -V
=> zsh: command not found: postgres
postgresqlをsetしてあげる
調べたところ、以下の内容がインストール後のターミナルに表示されていました。
postgresql@13 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula
このkeg-onlyは「インストールのみでシンボリックは繋がない」というサービスにつく単語だそうです。
詳しくはこちらの記事を拝読
https://zenn.dev/fujishiro/scraps/d311a91daccb1d
つまりPC買ってきたけど、モニターに繋いでいないのと同じ状態
そりゃシンボリックが繋がってないならしゃーない。
ってことで、インストール後にでてきた表記をよく見てみるとset方法のコマンドが書かれてあるのでそれを順次行っていきます
If you need to have postgresql@13 first in your PATH, run:
echo 'export PATH="/usr/local/opt/postgresql@13/bin:$PATH"' >> ~/.zshrc
For compilers to find postgresql@13 you may need to set:
export LDFLAGS="-L/usr/local/opt/postgresql@13/lib"
export CPPFLAGS="-I/usr/local/opt/postgresql@13/include"
For pkg-config to find postgresql@13 you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/postgresql@13/lib/pkgconfig"
To start postgresql@13 now and restart at login:
brew services start postgresql@13
Or, if you don't want/need a background service you can just run:
/usr/local/opt/postgresql@13/bin/postgres -D /usr/local/var/postgresql@13
$brew servies start postgressql@13
==> Tapping homebrew/services
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Enumerating objects: 2448, done.
remote: Counting objects: 100% (2448/2448), done.
remote: Compressing objects: 100% (1167/1167), done.
remote: Total 2448 (delta 1137), reused 2344 (delta 1093), pack-reused 0
Receiving objects: 100% (2448/2448), 671.57 KiB | 3.01 MiB/s, done.
Resolving deltas: 100% (1137/1137), done.
Tapped 1 command (45 files, 840.2KB).
==> Successfully started `postgresql@13` (label: homebrew.mxcl.postgresql@13)
無事postgresqlが起動することが確認できましたが、まだ解決できていない問題がこの後浮上します。
バージョンの確認ができない
ここまでうまくいっていたようなんですが、バージョンの確認ができません
$postgres -V
$psql --version
=> zsh: command not found: psql
上記コマンドを実行しましたが
psqlコマンド自体が使用できないという自体が発覚。
ただインストールができたのはちゃんと確認できていたので、色々やるとなんとかなるはずと思い調べまくりました。
色々ググって検索したところ
こちらの記事が参考になりました。
https://qiita.com/saitok7/items/efc734131c41981c6d06
PATHがちゃんと通っていない?
$brew link postgresql@13
=>Linking /usr/local/Cellar/postgresql@13/13.11_1... 378 symlinks created.
If you need to have this software first in your PATH instead consider running:
echo 'export PATH="/usr/local/opt/postgresql@13/bin:$PATH"' >> ~/.zshrc
これはインストールした時にも書かれていた内容ですね。
コピペして実行していたんですが、うまく行えていなかったようでした。
PATHを通すためにエディタを立ち上げて該当ファイルへ記入し保存します。
vimでPATHを更新する
$vim ~/.zshrc
vimで開いたあと、
~/.zshrcの最後の方へ
echo 'export PATH="/usr/local/opt/postgresql@13/bin:$PATH"'
を入力し、保存。
今度こそ!とpsql —versionを実行すると。。。
$psql --version
=>psql (PostgreSQL) 13.11
きました・・・・!!٩( ᐛ )و!!