Mac?OS上安裝PostgreSQL完整圖文教程
macOS 上安裝 PostgreSQL(完整圖文教程)
適用于 macOS 12+(Monterey / Ventura / Sonoma / Sequoia),支持 Apple Silicon(M1/M2/M3) 和 Intel
推薦使用 Homebrew(最簡單、最常用)
一、推薦方式:使用 Homebrew 安裝(99% 用戶選這個(gè))
1. 安裝 Homebrew(如果還沒裝)
打開 終端(Terminal),粘貼運(yùn)行:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
安裝過程會提示輸入密碼并按回車。
2. 安裝 PostgreSQL
brew install postgresql
自動(dòng)安裝最新穩(wěn)定版(如
PostgreSQL 17),并包含:
psql命令行工具pg_dump、createdb等pgAdmin可選(后面教你裝)
3. 啟動(dòng) PostgreSQL 服務(wù)
# 啟動(dòng)服務(wù)(當(dāng)前會話) brew services start postgresql # 或手動(dòng)啟動(dòng)(推薦) pg_ctl -D /opt/homebrew/var/postgres start # Apple Silicon # pg_ctl -D /usr/local/var/postgres start # Intel 舊版
4. 設(shè)置開機(jī)自啟(可選)
brew services start postgresql
以后系統(tǒng)啟動(dòng)時(shí)自動(dòng)運(yùn)行 PostgreSQL。
5. 驗(yàn)證安裝成功
psql --version # 輸出類似:psql (PostgreSQL) 17.0 # 進(jìn)入數(shù)據(jù)庫 psql postgres
在 psql 中運(yùn)行:
SELECT version(); \l \q
二、其他安裝方式(了解即可)
| 方式 | 說明 | 推薦度 |
|---|---|---|
| Postgres.app | 拖拽式 GUI 應(yīng)用,一鍵啟動(dòng) | ★★★★ |
| 官方安裝包 | .dmg 安裝,帶 pgAdmin | ★★★ |
| Docker | 容器化,隔離環(huán)境 | ★★★★ |
三、Postgres.app 方式(適合新手 / 想圖形化啟動(dòng))
- 下載:https://postgresapp.com
- 拖到
應(yīng)用程序文件夾 - 雙擊打開 → 自動(dòng)初始化并啟動(dòng)
- 菜單欄有小象圖標(biāo),可一鍵啟停
默認(rèn)數(shù)據(jù)目錄:
~/Library/Application Support/Postgres/var-17
四、圖形化管理工具(推薦安裝一個(gè))
方式 1:安裝 pgAdmin 4(官方 GUI)
brew install --cask pgadmin4
安裝后從 Launchpad 打開,添加本地服務(wù)器:
- Host:
localhost- Port:
5432- User:
你的 mac 用戶名(Homebrew 版默認(rèn))
方式 2:DBeaver(免費(fèi)多數(shù)據(jù)庫 GUI)
brew install --cask dbeaver-community
五、Homebrew 版 PostgreSQL 重要路徑
| 路徑 | 說明 |
|---|---|
/opt/homebrew/var/postgres | 數(shù)據(jù)目錄(M1/M2/M3) |
/usr/local/var/postgres | 數(shù)據(jù)目錄(Intel 舊版) |
/opt/homebrew/bin/psql | 命令行工具 |
/opt/homebrew/etc/postgresql@17 | 配置文件 |
六、設(shè)置密碼 & 創(chuàng)建用戶(推薦)
Homebrew 安裝的 PostgreSQL 默認(rèn)無密碼,通過本地 socket 認(rèn)證。
設(shè)置postgres角色密碼:
psql postgres
ALTER USER postgres WITH PASSWORD 'your_secure_password'; \q
創(chuàng)建應(yīng)用專用用戶和數(shù)據(jù)庫
psql postgres
CREATE DATABASE myapp; CREATE USER appuser WITH ENCRYPTED PASSWORD 'apppass123'; GRANT ALL PRIVILEGES ON DATABASE myapp TO appuser; \q
測試連接:
psql -h localhost -U appuser -d myapp
七、允許遠(yuǎn)程連接(開發(fā)/測試用)
1. 修改監(jiān)聽地址
nano /opt/homebrew/var/postgres/postgresql.conf
找到并修改:
# listen_addresses = 'localhost' listen_addresses = '*'
2. 修改認(rèn)證方式
nano /opt/homebrew/var/postgres/pg_hba.conf
末尾添加:
# 允許局域網(wǎng) host all all 192.168.1.0/24 md5 # 允許所有(僅測試?。? # host all all 0.0.0.0/0 md5
3. 重啟服務(wù)
brew services restart postgresql
4. 開放 macOS 防火墻(系統(tǒng)設(shè)置 → 網(wǎng)絡(luò) → 防火墻)
或命令行:
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --add /opt/homebrew/bin/postgres
八、常用命令總結(jié)(終端)
| 命令 | 說明 |
|---|---|
brew services start postgresql | 啟動(dòng)服務(wù) |
brew services stop postgresql | 停止服務(wù) |
brew services restart postgresql | 重啟 |
psql postgres | 進(jìn)入數(shù)據(jù)庫 |
pg_dump mydb > backup.sql | 備份 |
psql mydb < backup.sql | 恢復(fù) |
createdb mydb | 創(chuàng)建數(shù)據(jù)庫 |
dropdb mydb | 刪除數(shù)據(jù)庫 |
pg_ctl status | 查看狀態(tài) |
九、備份與恢復(fù)
# 備份 pg_dump -U postgres myapp > myapp_backup.sql # 恢復(fù) psql -U postgres myapp < myapp_backup.sql # 或 createdb myapp_restore psql -U postgres myapp_restore < myapp_backup.sql
十、卸載 PostgreSQL(徹底清理)
# 停止服務(wù) brew services stop postgresql # 卸載 brew uninstall postgresql # 刪除數(shù)據(jù)(謹(jǐn)慎?。? rm -rf /opt/homebrew/var/postgres rm -rf /usr/local/var/postgres # 刪除配置文件 rm -rf /opt/homebrew/etc/postgresql@*
十一、常見問題(FAQ)
| 問題 | 解決方法 |
|---|---|
psql: error: could not connect to server | brew services start postgresql |
FATAL: role "postgres" does not exist | 重新初始化:initdb /opt/homebrew/var/postgres |
permission denied for database postgres | 檢查數(shù)據(jù)目錄權(quán)限:chmod 700 /opt/homebrew/var/postgres |
| 想換端口 | 修改 postgresql.conf 中的 port = 5433 |
十二、學(xué)習(xí)資源
- 官方文檔:https://www.postgresql.org/docs/
- Postgres.app:https://postgresapp.com
- Homebrew 公式:https://formulae.brew.sh/formula/postgresql
恭喜!你已成功在 macOS 上安裝 PostgreSQL!
到此這篇關(guān)于Mac OS上安裝PostgreSQL的文章就介紹到這了,更多相關(guān)Mac OS安裝PostgreSQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
postgresql中時(shí)間轉(zhuǎn)換和加減操作
這篇文章主要介紹了postgresql中時(shí)間轉(zhuǎn)換和加減操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
Ruoyi從mysql切換到postgresql的幾個(gè)踩坑實(shí)戰(zhàn)
最近由于工作的原因,需要將Ruoyi從mysql切換到postgresql,所以這篇文章主要給大家介紹了關(guān)于Ruoyi從mysql切換到postgresql的幾個(gè)踩坑實(shí)戰(zhàn),需要的朋友可以參考下2023-02-02
解決PostgreSQL服務(wù)啟動(dòng)后占用100% CPU卡死的問題
前文書說到,今天耗費(fèi)了九牛二虎之力,終于馴服了NTFS權(quán)限安裝好了PostgreSQL,卻不曾想,服務(wù)啟動(dòng)后,新的狀況又出現(xiàn)了。2009-08-08
postgresql 實(shí)現(xiàn)取出分組中最大的幾條數(shù)據(jù)
這篇文章主要介紹了postgresql 實(shí)現(xiàn)取出分組中最大的幾條數(shù)據(jù),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
postgresql 刪除重復(fù)數(shù)據(jù)案例詳解
這篇文章主要介紹了postgresql 刪除重復(fù)數(shù)據(jù)案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
postgresql 實(shí)現(xiàn)將字段為空的值替換為指定值
這篇文章主要介紹了postgresql 實(shí)現(xiàn)將字段為空的值替換為指定值,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01

