Prerequisites
Before starting, ensure you have:
postgres
or a custom user).Update your package list and install pgAdmin4 using the following commands:
sudo apt update
sudo apt install pgadmin4
During installation, you’ll be prompted to set a master password for pgAdmin (required to access the web interface).
To launch pgAdmin, run:
pgadmin4
This will open the pgAdmin web interface in your default browser. Log in with the master password you set.
postgresql.conf
to Allow Remote ConnectionsEdit the PostgreSQL configuration file to enable listening on all network interfaces:
sudo nano /etc/postgresql/<version>/main/postgresql.conf
Replace <version>
with your PostgreSQL version (e.g., 15
for PostgreSQL 15). Find the line:
#listen_addresses = 'localhost'
Uncomment it and change to:
listen_addresses = '*'
This allows PostgreSQL to accept connections from any IP address.
pg_hba.conf
to Permit Remote AuthenticationEdit the client authentication file:
sudo nano /etc/postgresql/<version>/main/pg_hba.conf
Add the following line at the end of the file to allow password-based connections from any IP (adjust the IP range for security in production):
host all all 0.0.0.0/0 md5
Save and close both files.
sudo systemctl restart postgresql
If the postgres
user (or your target user) doesn’t have a password, set one:
sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD 'your_strong_password';"
Replace your_strong_password
with a secure password.
Allow incoming traffic on PostgreSQL’s default port (5432) using ufw
:
sudo ufw allow 5432/tcp
sudo ufw enable # Enable firewall if not already active
Verify the rule is applied:
sudo ufw status
You should see 5432/tcp ALLOW
in the output.
Remote Production DB
).5432
(default; change if your server uses a different port).postgres
(default system database).postgres
).postgres
) to open it and start querying.pg_hba.conf
, replace 0.0.0.0/0
with your client’s specific IP range (e.g., 192.168.1.0/24
) to limit connections.postgresql.conf
(set ssl = on
) and provide certificates to secure data in transit.By following these steps, you can securely connect pgAdmin on Ubuntu to a remote PostgreSQL server and manage your databases efficiently.