PostgreSQL 是功能最完整的開源物件關聯式資料庫,支援 SQL92/99、ACID 交易、可擴展型別、MVCC 並發控制。本文記錄在 Red Hat 7.0 (GNOME、linuxconf) 環境下,使用官方 RPM 套件安裝 PostgreSQL 7.0.2、配置 Postmaster 參數、建立資料庫使用者、開啟遠端連線與 phpPgAdmin 管理介面的完整流程。
linuxconf 系統配置工具postgresql-server-7.0.2-17.i386.rpm # 伺服器核心 postgresql-7.0.2-17.i386.rpm # 客戶端工具、庫 php-pgsql-4.0.1pl2-9.i386.rpm # PHP 模組 (若需 PHP 連線)
postgresql-devel-7.0.2-17.i386.rpm # 開發標頭/庫 postgresql-jdbc-7.0.2-17.i386.rpm # JDBC 驅動 postgresql-odbc-7.0.2-17.i386.rpm # ODBC 驅動 postgresql-perl-7.0.2-17.i386.rpm # Perl DBI/DBD::Pg postgresql-python-7.0.2-17.i386.rpm # Python DB-API postgresql-tcl-7.0.2-17.i386.rpm # Tcl 介面 (推薦) postgresql-tk-7.0.2-17.i386.rpm # Tk 圖形介面 phpPgAdmin_2-2-1-pl1.tar.gz # Web 管理介面 (https://www.greatbridge.org/)
# 方法 1:GnoRPM 圖形安裝 (GNOME 選單 → System → GnoRPM) # 方法 2:命令列批次安裝 rpm -ivh postgresql-*.rpm php-pgsql-*.rpm # 部署 phpPgAdmin mkdir -p /var/www/html/phpPgAdmin tar -xzf phpPgAdmin_2-2-1-pl1.tar.gz -C /var/www/html/phpPgAdmin --strip-components=1
Postmaster 參數可透過 /var/lib/pgsql/data/postmaster.opts.default 設定,啟動時自動生成 postmaster.opts。
| 參數 | 說明 | 建議生產值 |
|---|---|---|
| -B | 共享緩衝區塊數 (每塊 8 KB),至少 2× max connections | 512 (4 MB) 起,視 RAM 調大至 8192+ (64 MB) |
| -D | 資料目錄路徑 | /var/lib/pgsql/data (固定) |
| -N | 最大後端進程數 (含 superuser 保留) | 128-256 (視連線池) |
| -i | 啟用 TCP/IP 監聽 (預設只走 Unix socket) | 必須啟用以允許遠端連線 |
| -p | TCP 監聽埠 (預設 5432) | 5432 或自訂 |
| -l | 啟用 SSL 加密連線 | 生產環境建議開啟 |
| -S | 靜默模式 (無除錯輸出) | 生產環境建議 |
| -d | 除錯等級 (1-5) | 0 (關閉) |
# 編輯 /var/lib/pgsql/data/postmaster.opts.default -B 512 -i -N 128 # 重啟服務生效 service postgresql restart # 驗證生效 cat /var/lib/pgsql/data/postmaster.opts # 應顯示 -B 512 -i -N 128
# 使用 linuxconf (圖形/文字) linuxconf → Control → Control Service Activity → postgresql → Start # 或命令列 service postgresql start chkconfig postgresql on
# 切換至 postgres 超級使用者 su - postgres # 建立一般使用者 (可建立資料庫) createuser -P -d -a newuser # -P 提示密碼, -d 允許建立資料庫, -a 允許建立使用者 (超級用戶) # 建立資料庫 createdb -O newuser -E UNICODE mydb # 驗證連線 psql -U newuser -d mydb -c "SELECT version();"
編輯 /var/lib/pgsql/data/pg_hba.conf,在檔案末尾新增:
# TYPE DATABASE USER CIDR-ADDRESS METHOD host all all 192.168.0.0/24 md5 host all all 10.0.0.0/8 md5 # 本地 Unix socket 連線 local all all peer local all postgres ident
local (Unix socket)、host (TCP/IP, SSL)、hostssl、hostnossl。all 表所有,或指定名稱/群組 (+group)。255.255.255.0 = /24。trust (無密碼)、md5 (密碼雜湊)、password (明文)、ident (OS 帳號映射)、peer (本地同用戶)。安全建議:生產環境禁止使用 trust,一律 md5 或 scram-sha-256 (PG 10+),並配合防火牆限制 5432 埠來源 IP。
# iptables 範例 (僅允許內網 192.168.0.0/24) iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 5432 -j ACCEPT iptables -A INPUT -p tcp --dport 5432 -j DROP service iptables save
# 編輯 /var/www/html/phpPgAdmin/conf/config.inc.php
$conf['servers'][0]['host'] = 'localhost';
$conf['servers'][0]['port'] = 5432;
$conf['servers'][0]['defaultdb'] = 'template1';
$conf['owned_only'] = true; # 僅顯示擁有者資料庫
# Apache 虛擬主機加入
Alias /phpPgAdmin /var/www/html/phpPgAdmin
<Directory /var/www/html/phpPgAdmin>
Order deny,allow
Deny from all
Allow from 192.168.0.0/24
</Directory>
service httpd reload
在 Red Hat 7.0 部署 PostgreSQL 7.0.2 關鍵步驟:RPM 批次安裝 → postmaster.opts.default 調優 (-B -N -i) → pg_hba.conf 設定 md5 遠端存取 → 防火牆限制來源 → phpPgAdmin 部署。PostgreSQL 功能強大、標準相容度高,掌握上述流程即可建立穩定可靠的資料庫服務。