PostgreSQL(常簡稱 Postgres)是開源的關聯式資料庫管理系統(RDBMS),支援標準 SQL,並具備:
適用於 Web 後端、數據分析、地理資訊、向量檢索等場景。
# macOS(Homebrew)
brew install postgresql@16
brew services start postgresql@16
# 進入互動式終端
psql postgres
常用連線參數:
| 參數 | 說明 | 預設 |
|---|---|---|
-h |
主機 | localhost |
-p |
埠號 | 5432 |
-U |
使用者 | 系統使用者 |
-d |
資料庫 | postgres |
psql -h localhost -p 5432 -U harry -d mydb
| 概念 | 說明 |
|---|---|
| Database | 資料庫實例中的一個庫 |
| Schema | 物件命名空間(預設 public) |
| Table | 資料表 |
| Row / Column | 列 / 欄 |
| Primary Key | 主鍵,唯一識別一列 |
| Foreign Key | 外鍵,關聯其他表 |
-- 建立資料庫
CREATE DATABASE shop;
-- 切換資料庫(在 psql 中)
\c shop
-- 建立使用者並授權
CREATE USER app_user WITH PASSWORD 'secret';
GRANT ALL PRIVILEGES ON DATABASE shop TO app_user;
-- 列出資料庫
\l
-- 列出目前連線的資料庫中的表
\dt