CentOS系統配置pgAdmin詳細步驟
pgAdmin是PostgreSQL的圖形化管理工具,需先安裝PostgreSQL作為后端數據庫。
安裝PostgreSQL官方Yum存儲庫(適用于CentOS 7/8/9):
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-$(rpm -E %{rhel})-x86_64/pgdg-redhat-repo-latest.noarch.rpm
安裝PostgreSQL服務器(以PostgreSQL 15為例):
sudo yum install -y postgresql15-server postgresql15-devel
初始化數據庫并啟動服務:
sudo /usr/pgsql-15/bin/postgresql-15-setup initdb # 初始化數據庫集群
sudo systemctl enable --now postgresql-15 # 開機啟動并立即啟動服務
創建pgAdmin專用數據庫用戶(用于登錄pgAdmin):
切換至postgres用戶,執行SQL命令創建用戶(如pgadmin)并授權:
sudo -u postgres psql
CREATE USER pgadmin WITH PASSWORD 'YourStrongPassword123!' CREATEDB; # 創建用戶并允許創建數據庫
GRANT ALL PRIVILEGES ON DATABASE postgres TO pgadmin; # 授權訪問默認數據庫
\q # 退出psql
pgAdmin支持桌面版和Web版,Web版更適合遠程管理,以下是安裝步驟:
安裝pgAdmin Web組件:
sudo yum install -y pgadmin4-web
初始化pgAdmin配置:
運行初始化腳本,設置管理員郵箱(用于找回密碼)和初始密碼:
sudo /usr/pgadmin4/bin/pgadmin4-web-setup.sh
按提示輸入管理員郵箱(如admin@yourdomain.com)和密碼(如PgAdminAdmin@123)。
啟動pgAdmin服務:
初始化完成后,pgAdmin會自動啟動。如需手動控制,可使用以下命令:
sudo systemctl start pgadmin4 # 啟動服務
sudo systemctl enable pgadmin4 # 開機自啟動
pgAdmin Web版依賴Apache提供HTTP服務,需調整配置以允許外部訪問:
復制默認配置文件:
sudo cp /etc/httpd/conf.d/pgadmin4.conf.sample /etc/httpd/conf.d/pgadmin4.conf
修改配置文件(可選):
用文本編輯器(如vi)打開配置文件,確認以下設置:
Listen localhost:80改為Listen 80;ServerName localhost改為服務器公網IP或域名(如ServerName pgadmin.yourdomain.com)。重啟Apache服務:
sudo systemctl restart httpd
若系統啟用了firewalld,需開放HTTP(80端口)和PostgreSQL(5432端口):
sudo firewall-cmd --permanent --add-service=http # 開放HTTP服務
sudo firewall-cmd --permanent --add-port=5432/tcp # 開放PostgreSQL端口
sudo firewall-cmd --reload # 重新加載防火墻規則
http://your_server_ip或http://pgadmin.yourdomain.com)。pgadmin/YourStrongPassword123!”),點擊“登錄”。My PostgreSQL Server);localhost(若pgAdmin與PostgreSQL在同一服務器)或PostgreSQL服務器IP;5432(默認端口);pgadmin(之前創建的數據庫用戶);YourStrongPassword123!”(用戶密碼)。若系統已安裝Docker,可通過容器快速部署pgAdmin(無需配置Apache):
sudo yum install -y docker
sudo systemctl start docker
sudo systemctl enable docker
docker pull dpage/pgadmin4
docker run -p 5050:80 \
-e "PGADMIN_DEFAULT_EMAIL=admin@pgadmin.com" \
-e "PGADMIN_DEFAULT_PASSWORD=admin123" \
-d dpage/pgadmin4
http://your_server_ip:5050,使用admin@pgadmin.com/admin123登錄。postgresql.conf中的listen_addresses為localhost),并通過SSH隧道訪問。sudo setsebool -P httpd_can_network_connect_db 1
以上步驟覆蓋了CentOS系統下pgAdmin的主要配置流程,可根據實際需求調整(如修改端口、配置HTTPS等)。