網頁

2021/11/14

Mac Homebrew安裝PostgreSQL

Mac使用Homebrew安裝PostgreSQL。


範例環境:

  • macOS Big Sur (version 11.4)
  • Homebrew 3.3.3


安裝PostgreSQL

在終端機(Terminal)輸入brew install postgresql

$ brew install postgresql
...
==> postgresql
To migrate existing data from a previous major version of PostgreSQL run:
  brew postgresql-upgrade-database

This formula has created a default database cluster with:
  initdb --locale=C -E UTF-8 /usr/local/var/postgres
For more details, read:
  https://www.postgresql.org/docs/14/app-initdb.html

To restart postgresql after an upgrade:
  brew services restart postgresql
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgres
...


啟動PostgreSQL

安裝完後輸入brew services start postgresql啟動PostgreSQL服務。

$ brew services start postgresql
==> Tapping homebrew/services
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Enumerating objects: 1522, done.
remote: Counting objects: 100% (401/401), done.
remote: Compressing objects: 100% (290/290), done.
remote: Total 1522 (delta 165), reused 286 (delta 101), pack-reused 1121
Receiving objects: 100% (1522/1522), 446.04 KiB | 1003.00 KiB/s, done.
Resolving deltas: 100% (642/642), done.
Tapped 1 command (38 files, 555.8KB).
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

輸入brew services list可檢視運行中的PostgreSQL服務。

$ brew services list
Name       Status  User File
postgresql started matt ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist


psql登入

輸入psql postgres登入postgres資料庫(預設不用密碼),登入後提示符變成postgres=#

$ psql postgres
psql (14.1)
Type "help" for help.

postgres=#

輸入SHOW server_version顯示PostgreSQL版本資訊。

postgres=# SHOW server_version;
 server_version
----------------
 14.1
(1 row)

輸入\du檢視現有的角色(role),預設使用者角色為系統使用者名稱。

postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 matt      | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

引述官方文件說明如下:

In order to bootstrap the database system, a freshly initialized system always contains one predefined role. This role is always a “superuser”, and by default (unless altered when running initdb) it will have the same name as the operating system user that initialized the database cluster. Customarily, this role will be named postgres. In order to create more roles you first have to connect as this initial role.



GUI管理工具連線

這邊用Sqlectron圖形化資料庫管理工具連線到PostpreSQL。

  • Name: 任意命名
  • Database Type: PostgreSQL
  • Server Address: localhost
  • Port: 5432
  • User: <username>
  • Password: 無

連線設定。



連線進入PostgreSQL。



或使用「DBeaver」。


停止PostgreSQL

輸入brew services stop postgresql終止PostgreSQL服務。

$ brew services stop postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)

輸入brew services list確認PostgreSQL服務已停止。

$ brew services list
Name       Status  User File
postgresql stopped


沒有留言:

張貼留言