跳至主要内容

PostgreSQL + TimescaleDB 安裝指南(Windows)

適用環境:Windows 10/11 開發機

版本建議

軟體建議版本說明
PostgreSQL17(最新穩定版)PG 18 為 Beta,不建議
TimescaleDB2.24+支援 PG 15/16/17

TimescaleDB 將於 2026/6 移除 PG 15 支援,新專案建議使用 PG 16+。


安裝步驟

1. 安裝 PostgreSQL

  1. 前往 https://www.postgresql.org/download/windows/
  2. 點選 Download the installer(EDB 提供)
  3. 下載 PostgreSQL 17 Windows x86-64 版本
  4. 執行安裝程式,設定如下:
步驟設定
Installation Directory預設 C:\Program Files\PostgreSQL\17
Components全勾(PostgreSQL Server, pgAdmin 4, Stack Builder, Command Line Tools)
Data Directory預設 C:\Program Files\PostgreSQL\17\data
Password設定 superuser(postgres)密碼,務必記住
Port5432(預設)
LocaleDefault locale
  1. 安裝完成後不要勾 Launch Stack Builder,直接 Finish

2. 加入 PATH

系統管理員身分開啟 PowerShell,執行:

[Environment]::SetEnvironmentVariable("Path", [Environment]::GetEnvironmentVariable("Path", "Machine") + ";C:\Program Files\PostgreSQL\17\bin", "Machine")

重新開啟 PowerShell 視窗後驗證:

psql --version

應顯示 psql (PostgreSQL) 17.x

3. 安裝 TimescaleDB

  1. 前往 https://docs.timescale.com/self-hosted/latest/install/installation-windows/
  2. 下載對應 PostgreSQL 17 的 Windows installer(.exe
  3. 右鍵 → 以系統管理員身分執行(重要,否則會因權限無法寫入 lib/ 目錄)
  4. 安裝程式會自動偵測 PostgreSQL 路徑
  5. 詢問是否執行 timescaledb-tune 時,選 y
  6. 詢問 postgresql.conf 路徑時,輸入:
    C:\Program Files\PostgreSQL\17\data\postgresql.conf
  7. 後續優化建議全部按 y 同意

注意:如果未以系統管理員身分執行,會出現 Access is denied 錯誤,無法複製 DLL。

4. 重啟 PostgreSQL 服務

系統管理員身分執行:

net stop postgresql-x64-17
net start postgresql-x64-17

5. 驗證安裝

psql -U postgres -c "SELECT version();"

輸入密碼後應顯示 PostgreSQL 17.x 版本資訊。


建立專案資料庫

每個專案使用獨立的 Database,互不影響。

Jope-PLC-Monitor

psql -U postgres

CREATE DATABASE jope_plc_monitor;
\c jope_plc_monitor
CREATE EXTENSION IF NOT EXISTS timescaledb;
\dx

\dx 應顯示 timescaledb extension 已啟用。輸入 \q 離開。

驗證:

psql -U postgres -d jope_plc_monitor -c "SELECT extname, extversion FROM pg_extension WHERE extname = 'timescaledb';"

未來新專案

CREATE DATABASE {project_name};
\c {project_name}
CREATE EXTENSION IF NOT EXISTS timescaledb; -- 僅需要時序功能的專案

連線資訊

項目
Hostlocalhost
Port5432
Usernamepostgres
Password安裝時設定的密碼
管理工具pgAdmin 4(開始選單可找到)

Connection String 範例(.NET)

Host=localhost;Port=5432;Database=jope_plc_monitor;Username=postgres;Password={your_password}

多專案隔離說明

PostgreSQL Server(localhost:5432)
├── DB: jope_plc_monitor ← PLC-Monitor 專用
├── DB: project_b ← 未來專案 B
└── DB: project_c ← 未來專案 C
  • 不同 Database 之間的 Table、Schema、Extension 完全隔離
  • TimescaleDB Extension 需在每個需要的 DB 內個別啟用
  • 所有專案共用同一個 PostgreSQL Server,只佔一個 Port