PostgreSQL 是一個功能強大、開源、穩定的對象關系數據庫系統,廣泛用于后端開發、數據處理與分布式架構中。本指南將手把手教你如何在 Windows、macOS 以及主流 Linux 發行版 上安裝 PostgreSQL,并附上安裝驗證命令與基礎配置方法。
一、Windows 安裝與配置 PostgreSQL
1. 安裝步驟
-
打開官網下載頁面:https://www.postgresql.org/download/windows/
-
點擊 “Download the installer”,跳轉至 EnterpriseDB。
-
下載適用于你的版本(例如 PostgreSQL 16)的安裝程序。
-
雙擊運行,按提示操作:
- 選擇安裝目錄(默認即可)
- 設置數據庫超級用戶密碼(務必記住)
- 設置端口號(默認 5432)
- 默認選擇 UTF-8 編碼和英文區域設置
2. 驗證安裝是否成功
打開命令提示符(CMD)或 PowerShell,輸入以下命令:
"C:\Program Files\PostgreSQL\16\bin\psql" --version
如果顯示如下類似輸出,則表示安裝成功:
psql (PostgreSQL) 16.1
可選:將
C:\Program Files\PostgreSQL\16\bin
添加到系統環境變量PATH
,便可直接使用psql
命令。
二、macOS 安裝與配置 PostgreSQL
方法一:使用 Homebrew 安裝(推薦)
- 確保安裝了 Homebrew:https://brew.sh/
- 執行以下命令安裝 PostgreSQL:
brew update
brew install postgresql
驗證安裝是否成功:
psql --version
輸出示例:
psql (PostgreSQL) 16.1
啟動服務:
brew services start postgresql
方法二:使用 Postgres.app(圖形界面安裝)
- 前往:https://postgresapp.com/
- 下載并安裝后,打開應用即可自動初始化服務。
驗證安裝是否成功:
/Applications/Postgres.app/Contents/Versions/latest/bin/psql --version
可將其加入 PATH:
echo 'export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin' >> ~/.zshrc
source ~/.zshrc
之后可使用:
psql --version
三、Linux 安裝與配置 PostgreSQL(涵蓋主流發行版)
以下為不同發行版的詳細安裝步驟,均包含啟動服務及驗證命令。
1. Ubuntu / Debian / Linux Mint / Kali 等
sudo apt update
sudo apt install postgresql postgresql-contrib
驗證安裝:
psql --version
啟動 PostgreSQL:
sudo systemctl start postgresql
sudo systemctl enable postgresql
2. CentOS / Rocky / AlmaLinux / RHEL 系列
添加官方倉庫并安裝 PostgreSQL 16:
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo dnf -qy module disable postgresql
sudo dnf install -y postgresql16-server postgresql16
初始化數據庫并啟動服務:
sudo /usr/pgsql-16/bin/postgresql-16-setup initdb
sudo systemctl enable --now postgresql-16
驗證安裝:
/usr/pgsql-16/bin/psql --version
可加入 PATH:
echo 'export PATH=$PATH:/usr/pgsql-16/bin' >> ~/.bashrc
source ~/.bashrc
3. Fedora
sudo dnf install -y postgresql-server postgresql-contrib
初始化數據庫:
sudo postgresql-setup --initdb
啟動服務:
sudo systemctl enable --now postgresql
驗證安裝:
psql --version
4. Arch Linux / Manjaro
sudo pacman -Syu postgresql
初始化數據庫:
sudo -iu postgres
initdb --locale $LANG -D /var/lib/postgres/data
exit
啟動服務:
sudo systemctl enable --now postgresql
驗證安裝:
psql --version
5. openSUSE / SUSE Linux Enterprise
sudo zypper refresh
sudo zypper install postgresql16-server
初始化數據庫(如未自動執行):
sudo -u postgres initdb
啟動服務:
sudo systemctl enable --now postgresql
驗證安裝:
psql --version
四、PostgreSQL 安裝后通用配置
1. 登錄數據庫
切換至 postgres
用戶:
sudo -i -u postgres
psql
退出:
\q
2. 創建用戶和數據庫
CREATE USER myuser WITH PASSWORD 'mypassword';
CREATE DATABASE mydb OWNER myuser;
五、遠程連接配置(可選)
適用于服務器部署環境。
1. 修改 postgresql.conf
路徑依據系統不同:
- Ubuntu/Debian:
/etc/postgresql/16/main/postgresql.conf
- CentOS/Fedora/RHEL:
/var/lib/pgsql/16/data/postgresql.conf
將:
# listen_addresses = 'localhost'
修改為:
listen_addresses = '*'
2. 修改 pg_hba.conf
添加以下內容以允許遠程訪問:
host all all 0.0.0.0/0 md5
3. 重啟服務:
sudo systemctl restart postgresql
六、常見問題排查
問題 | 解決方法 |
---|---|
psql: command not found | 檢查是否將 PostgreSQL bin 路徑添加至 PATH |
連接被拒絕 | 確保 PostgreSQL 已啟動并監聽地址正確 |
密碼驗證失敗 | 檢查 pg_hba.conf 中是否啟用了 md5 認證模式 |
七、圖形化管理工具推薦
-
pgAdmin(官方推薦 GUI 工具)
https://www.pgadmin.org/download/ -
DBeaver(支持多數據庫)
https://dbeaver.io/