萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Red Hat 7.0 上安裝配置 PostgreSQL 7.0.2 完整指南:從 RPM 安裝到遠端連線

PostgreSQL 是功能最完整的開源物件關聯式資料庫,支援 SQL92/99、ACID 交易、可擴展型別、MVCC 並發控制。本文記錄在 Red Hat 7.0 (GNOME、linuxconf) 環境下,使用官方 RPM 套件安裝 PostgreSQL 7.0.2、配置 Postmaster 參數、建立資料庫使用者、開啟遠端連線與 phpPgAdmin 管理介面的完整流程。

一、安裝前置需求

  • Red Hat Linux 7.0 已安裝
  • 未安裝舊版 PostgreSQL
  • GNOME 桌面環境 (提供 GnoRPM 圖形安裝)
  • linuxconf 系統配置工具
  • root 權限

二、取得安裝套件

必裝 RPM (Red Hat 7.0 光碟或官方 FTP)

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 核心參數配置

Postmaster 參數可透過 /var/lib/pgsql/data/postmaster.opts.default 設定,啟動時自動生成 postmaster.opts。

關鍵參數說明

參數說明建議生產值
-B共享緩衝區塊數 (每塊 8 KB),至少 2× max connections512 (4 MB) 起,視 RAM 調大至 8192+ (64 MB)
-D資料目錄路徑/var/lib/pgsql/data (固定)
-N最大後端進程數 (含 superuser 保留)128-256 (視連線池)
-i啟用 TCP/IP 監聽 (預設只走 Unix socket)必須啟用以允許遠端連線
-pTCP 監聽埠 (預設 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();"

七、開啟遠端連線 (pg_hba.conf)

編輯 /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

參數說明

  • TYPE:local (Unix socket)、host (TCP/IP, SSL)、hostssl、hostnossl。
  • DATABASE/USER:all 表所有,或指定名稱/群組 (+group)。
  • CIDR-ADDRESS:允許連線的網段,255.255.255.0 = /24。
  • METHOD: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

九、phpPgAdmin 配置與驗證

# 編輯 /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 功能強大、標準相容度高,掌握上述流程即可建立穩定可靠的資料庫服務。

copyright © 萬盛學電腦網 all rights reserved