pgAdmin是PostgreSQL的图形化管理工具,需先安装PostgreSQL作为后端数据库。
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-$(rpm -E %{rhel})-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo yum install -y postgresql15-server postgresql15-devel
sudo /usr/pgsql-15/bin/postgresql-15-setup initdb # 初始化数据库集群
sudo systemctl enable --now postgresql-15 # 开机启动并立即启动服务
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版更适合远程管理,以下是安装步骤:
sudo yum install -y pgadmin4-web
sudo /usr/pgadmin4/bin/pgadmin4-web-setup.sh
按提示输入管理员邮箱(如admin@yourdomain.com)和密码(如PgAdminAdmin@123)。sudo systemctl start pgadmin4 # 启动服务
sudo systemctl enable pgadmin4 # 开机自启动
要让pgAdmin远程访问PostgreSQL,需修改PostgreSQL配置文件:
postgresql.conf(允许监听所有IP):/var/lib/pgsql/15/data/postgresql.conf或/usr/pgsql-15/data/postgresql.conf),修改以下行:listen_addresses = '*' # 允许监听所有网络接口
pg_hba.conf(允许远程IP访问):postgresql.conf同目录),在文件末尾添加以下行(允许所有IP通过密码验证访问):host all all 0.0.0.0/0 md5 # 生产环境建议限制为客户端IP段(如192.168.1.0/24)
sudo systemctl restart postgresql-15
若系统启用了firewalld,需开放HTTP(pgAdmin Web界面)和PostgreSQL(数据库连接)端口:
sudo firewall-cmd --permanent --add-service=http # 开放HTTP服务(默认80端口)
sudo firewall-cmd --permanent --add-port=5432/tcp # 开放PostgreSQL端口
sudo firewall-cmd --reload # 重新加载防火墙规则
若使用iptables,可执行以下命令:
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT # 允许HTTP流量
sudo iptables -A INPUT -p tcp --dport 5432 -j ACCEPT # 允许PostgreSQL流量
sudo service iptables save # 保存规则(CentOS 7及以下)
默认情况下,pgAdmin Web界面仅允许本地访问,需修改Apache配置:
sudo cp /etc/httpd/conf.d/pgadmin4.conf.sample /etc/httpd/conf.d/pgadmin4.conf
vi)打开配置文件,确认以下设置:
Listen localhost:80改为Listen 80(允许所有IP访问);ServerName localhost改为服务器公网IP或域名(如ServerName pgadmin.yourdomain.com)。sudo systemctl restart httpd
http://your_server_ip或http://pgadmin.yourdomain.com)。pgadmin/YourStrongPassword123!”),点击“登录”。localhost);若系统已安装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