# PostgreSQL ###### tags: `Database` ## MacOS ### Install **Mac OS** ```bash # 查看版本 $ brew search postgresql ``` ```bash # 15 version $ brew install postgresql@15 # 可能cpu不同 路徑不同 $ echo 'export PATH="/opt/homebrew/opt/postgresql@15/bin:$PATH"' >> ~/.zshrc $ source ~/.zshrc postgres --version # 重新啟動postgresql brew services restart postgresql@15 ``` **查看services列表** ```bash $ brew services list ``` :::info ```bash postgresql@15 started user ~/Library/LaunchAgents/homebrew.mxcl.postgresql@15.plist ``` ::: ### DB List ```bash $ psql --list $ psql -l ``` ### CreateDB **兩種方法** ```bash $ createdb mydb ``` ```bash # 需先修改 database.yml 的內容 $ rails db:create ``` ### DropDB ```bash $ dropdb mydb ``` ### Connect ```bash # 進入mydb $ psql mydb ``` :::success ```sql psql (15.2 (Homebrew)) Type "help" for help. mydb=# ``` ::: ### port ```bash # 改port路徑 $ cd /opt/homebrew/var/postgresql@15 ``` :::info ```bash # port = 5432 # (change requires restart) ``` ::: **lsof** ```bash $ lsof -n -i | grep LISTEN ``` :::info ```bash # postgresql顯示應該是port吧? postgres 15071 user 7u IPv6 0xaaaaaaa 0t0 TCP [::1]:postgresql (LISTEN) postgres 15071 user 8u IPv4 0xaaaaaab 0t0 TCP 127.0.0.1:postgresql (LISTEN) ``` ::: **檢查port** ```bash netstat -l | grep 543 ``` :::info ```bash aaaaaa stream 0 0 aaaaaaab 0 0 0 /tmp/.s.PGSQL.5432 ``` ::: ## references * https://www.youtube.com/watch?v=EK6V5PHG3qo * https://www.stevenchang.tw/blog/2019/06/27/Install-PostgreSQL-in-Rails-Project ## Usage **查看全部table** ```sql \dt ```