Prerequisites
Before connecting to a PostgreSQL database via pgAdmin on CentOS, ensure the following:
- PostgreSQL is installed and running on the target server.
- You have valid database credentials (username/password).
- Network access between your client machine and the CentOS server is permitted.
Step 1: Install pgAdmin on CentOS
pgAdmin can be installed via the official YUM repository. Run the following commands:
# Add the pgAdmin repository
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-$(rpm -E %{rhel})-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# Disable the default PostgreSQL module (if installed) to avoid conflicts
sudo dnf -qy module disable postgresql
# Install pgAdmin4
sudo dnf install -y pgadmin4
# Enable and start the pgAdmin web interface
sudo systemctl enable --now pgadmin4
After installation, access pgAdmin in your browser at http://.
Step 2: Configure PostgreSQL for Remote Connections
To allow pgAdmin to connect to PostgreSQL from a remote machine, modify the PostgreSQL configuration files:
-
Edit
postgresql.conf
Locate the file (typically at/var/lib/pgsql/or/data/postgresql.conf /usr/local/pgsql/data/postgresql.conf) and set:listen_addresses = '*' # Allows connections from all IP addressesSave and exit.
-
Edit
pg_hba.conf
Open the file (usually in the same directory aspostgresql.conf) and add the following line to permit MD5-password authentication from all IPs:host all all 0.0.0.0/0 md5Save and exit.
-
Restart PostgreSQL
Apply changes by restarting the service:sudo systemctl restart postgresql-# Replace with your PostgreSQL version (e.g., 15) -
Secure the Connection (Optional but Recommended)
For production environments, replacemd5withscram-sha-256inpg_hba.confand use SSL encryption. Updatepostgresql.confwith:ssl = on ssl_cert_file = '/path/to/server.crt' ssl_key_file = '/path/to/server.key'
Step 3: Configure Firewall Rules
Allow incoming traffic on PostgreSQL’s default port (5432) using firewalld:
sudo firewall-cmd --permanent --zone=public --add-port=5432/tcp
sudo firewall-cmd --reload
If using iptables, run:
sudo iptables -A INPUT -p tcp --dport 5432 -j ACCEPT
sudo service iptables save
Step 4: Add a Database Connection in pgAdmin
-
Open pgAdmin
Launch your browser and navigate tohttp://. Log in with your pgAdmin credentials (set during installation).:80 -
Create a New Server Connection
- Right-click the “Servers” node in the left sidebar and select Create > Server.
- In the “General” tab, enter a descriptive name (e.g., “Production DB”).
- Go to the “Connection” tab and fill in the details:
- Host: IP address or hostname of the CentOS server running PostgreSQL.
- Port: 5432 (default PostgreSQL port).
- Username: Your PostgreSQL username (e.g.,
postgres). - Password: The password for the specified user.
- Click Save to store the connection.
-
Test the Connection
Double-click the newly created server to initiate the connection. Enter the user’s password when prompted. If successful, you’ll see the server’s databases in the left sidebar.
Troubleshooting Tips
- Connection Refused: Verify PostgreSQL is running (
sudo systemctl status postgresql-) and the firewall allows port 5432. - Authentication Failed: Ensure the username/password is correct and
pg_hba.confis configured to allow the client IP. - pgAdmin Not Loading: Check if the pgAdmin service is active (
sudo systemctl status pgadmin4) and the port (80) is open.
以上就是关于“centos pgadmin数据库连接方法”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm