啟動、連線測試
以下以 PostgreSQL 預設設定為例:
- 主機:`localhost`
- 埠號:`5432`
- 使用者:安裝時建立的使用者,常見為 `postgres`
- 資料庫:通常可使用與使用者同名的資料庫,或 `postgres`
---
## 一、Windows
### 方法 1:透過 Windows 服務啟動
1. 按 `Win + R`,輸入:
```text
services.msc
```
2. 找到名稱類似:
```text
postgresql-x64-16
```
或其他版本號的 PostgreSQL 服務。
3. 右鍵選擇:
- **Start/啟動**
- 若已啟動,會顯示 **Running/執行中**
也可以使用系統管理員身分開啟命令提示字元:
```bat
net start postgresql-x64-16
```
停止服務:
```bat
net stop postgresql-x64-16
```
> `16` 請依照實際安裝版本修改,例如 `postgresql-x64-15`。
---
### 方法 2:使用 `pg_ctl` 啟動
如果 PostgreSQL 沒有註冊為 Windows 服務,可以執行:
```bat
"C:\Program Files\PostgreSQL\16\bin\pg_ctl.exe" ^
-D "C:\Program Files\PostgreSQL\16\data" ^
start
```
停止:
```bat
"C:\Program Files\PostgreSQL\16\bin\pg_ctl.exe" ^
-D "C:\Program Files\PostgreSQL\16\data" ^
stop
```
安裝路徑和版本號可能不同,例如:
```text
C:\Program Files\PostgreSQL\15\
```
---
### Windows 連線方式
#### 方式 A:使用 SQL Shell(psql)
安裝 PostgreSQL 後,可從開始選單開啟:
```text
SQL Shell (psql)
```
依序輸入:
```text
Server [localhost]:
Database [postgres]:
Port [5432]:
Username [postgres]:
Password for user postgres:
```
直接按 Enter 可接受預設值,密碼則輸入安裝 PostgreSQL 時設定的密碼。
---
#### 方式 B:在命令提示字元使用 `psql`
如果 `psql` 已加入 PATH:
```bat
psql -h localhost -p 5432 -U postgres -d postgres
```
輸入密碼後即可連線。
如果系統找不到 `psql`,請使用完整路徑:
```bat
"C:\Program Files\PostgreSQL\16\bin\psql.exe" ^
-h localhost -p 5432 -U postgres -d postgres
```
成功後會看到類似:
```text
postgres=#
```
離開 PostgreSQL:
```sql
\q
```
---
#### 方式 C:使用 pgAdmin
1. 開啟 **pgAdmin**
2. 在左側選擇或新增伺服器
3. 填入:
```text
Host: localhost
Port: 5432
Maintenance database: postgres
Username: postgres
Password: 安裝時設定的密碼
```
4. 連線成功後,即可建立資料庫、資料表及執行 SQL。
---
## 二、macOS
macOS 常見有兩種安裝方式:Homebrew 和 Postgres.app。
---
## 方法 1:使用 Homebrew 安裝與啟動
### 1. 安裝 PostgreSQL
如果尚未安裝 Homebrew,可先參考 Homebrew 官方網站安裝。
安裝 PostgreSQL:
```bash
brew install postgresql
```
查看版本:
```bash
psql --version
```
---
### 2. 啟動 PostgreSQL
讓 PostgreSQL 隨 macOS 登入時自動啟動:
```bash
brew services start postgresql
```
查看服務狀態:
```bash
brew services list
```
若要停止:
```bash
brew services stop postgresql
```
也可以只在目前工作階段啟動:
```bash
pg_ctl -D "$(brew --prefix)/var/postgresql@16" start
```
資料目錄實際名稱可能因版本不同而不同,可使用:
```bash
brew --prefix
```
或查看:
```bash
ls "$(brew --prefix)/var"
```
---
### 3. 初始化資料庫叢集
某些 Homebrew 版本安裝後需要先初始化資料目錄:
```bash
initdb --locale=C -E UTF-8 "$(brew --prefix)/var/postgresql@16"
```
接著啟動:
```bash
brew services start postgresql@16
```
版本號請依實際安裝版本調整。
---
### macOS 連線方式
#### 使用 `psql`
```bash
psql -h localhost -p 5432 -U postgres -d postgres
```
如果 Homebrew 預設建立的是目前 macOS 使用者名稱,而不是 `postgres`,可以嘗試:
```bash
psql postgres
```
或:
```bash
psql -d postgres
```
成功後會進入:
```text
postgres=#
```
離開:
```sql
\q
```
---
## 方法 2:使用 Postgres.app
### 1. 啟動 PostgreSQL
1. 開啟 **Postgres.app**
2. 點選 **Initialize** 或啟動既有的 PostgreSQL 伺服器
3. 確認狀態顯示為 Running
Postgres.app 通常會使用:
```text
Host: localhost
Port: 5432
```
如果要讓終端機可以使用 `psql`,可在 Postgres.app 中點選:
```text
Postgres.app → Open psql
```
或將其指令加入 PATH,例如:
```bash
export PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH"
```
若希望永久生效,可加入 `~/.zshrc`:
```bash
echo 'export PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
```
---
## 三、確認 PostgreSQL 是否正在執行
### Windows
可以檢查服務:
```bat
sc query postgresql-x64-16
```
或使用:
```bat
netstat -ano | findstr :5432
```
### macOS
使用 Homebrew:
```bash
brew services list
```
或檢查 5432 埠號:
```bash
lsof -i :5432
```
也可以直接測試:
```bash
pg_isready -h localhost -p 5432
```
成功時通常會顯示:
```text
localhost:5432 - accepting connections
```
---
## 四、常用連線指令
### 連線到指定資料庫
```bash
psql -h localhost -p 5432 -U 使用者名稱 -d 資料庫名稱
```
例如:
```bash
psql -h localhost -p 5432 -U postgres -d mydb
```
### 使用連線字串
```bash
psql "host=localhost port=5432 dbname=postgres user=postgres"
```
### 以環境變數提供密碼
Linux/macOS:
```bash
export PGPASSWORD='你的密碼'
psql -h localhost -p 5432 -U postgres -d postgres
```
Windows CMD:
```bat
set PGPASSWORD=你的密碼
psql -h localhost -p 5432 -U postgres -d postgres
```
不建議長期以這種方式保存密碼;正式環境可使用 `.pgpass` 或 Windows 認證設定。
---
## 五、常見問題
### 1. `connection refused`
通常代表 PostgreSQL 尚未啟動,或未監聽 `5432`:
```bash
pg_isready -h localhost -p 5432
```
確認服務已啟動。
### 2. `role "postgres" does not exist`
有些 macOS Homebrew 安裝會以目前 macOS 使用者名稱建立角色。可改用:
```bash
psql -d postgres
```
或使用自己的 macOS 使用者名稱:
```bash
psql -U 你的 macOS 使用者名稱 -d postgres
```
### 3. `password authentication failed`
請確認:
- 使用者名稱正確
- 密碼正確
- 連線的是正確的 PostgreSQL 伺服器
- 埠號是否為 `5432`
### 4. `database does not exist`
先連線到預設資料庫:
```bash
psql -U postgres -d postgres
```
再建立新資料庫:
```sql
CREATE DATABASE mydb;
```
之後連線:
```bash
psql -U postgres -d mydb
```
相關學習地圖、教學課程
Python 資料工程