Prerequisites
Before connecting to a PostgreSQL database via pgAdmin on CentOS, ensure the following:
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://<your_centos_server_ip>:80.
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/<version>/data/postgresql.conf or /usr/local/pgsql/data/postgresql.conf) and set:
listen_addresses = '*' # Allows connections from all IP addresses
Save and exit.
Edit pg_hba.conf
Open the file (usually in the same directory as postgresql.conf) and add the following line to permit MD5-password authentication from all IPs:
host all all 0.0.0.0/0 md5
Save and exit.
Restart PostgreSQL
Apply changes by restarting the service:
sudo systemctl restart postgresql-<version> # Replace <version> with your PostgreSQL version (e.g., 15)
Secure the Connection (Optional but Recommended)
For production environments, replace md5 with scram-sha-256 in pg_hba.conf and use SSL encryption. Update postgresql.conf with:
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 to http://<your_centos_server_ip>:80. Log in with your pgAdmin credentials (set during installation).
Create a New Server Connection
postgres).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
sudo systemctl status postgresql-<version>) and the firewall allows port 5432.pg_hba.conf is configured to allow the client IP.sudo systemctl status pgadmin4) and the port (80) is open.