Generally, most servers only allow PostgreSQL connections to be made locally, that is, from the same Cloud server and not from an external server or machine.
In setups where it is necessary to configure PostgreSQL on a separate server, for example to avoid overloading the web frontend, some configurations must be carried out to enable remote connections to the databases.
Below we explain the steps to configure this access:
- Access your server via SSH as the “root” user. Access Linux via SSH
# ssh root@public_ip
- Once connected, edit the following file:
# vi /etc/postgresql/12/main/postgresql.conf
Look for the following line:
Replace 'localhost' with '*' and uncomment the line by removing the #:#listen_addresses = 'localhost' # what IP address(es) to listen on;
listen_addresses = '*' # what IP address(es) to listen on;
- The next step is to configure the "pg_hba.conf" file to allow remote access from any address. To do this, run the following:
# vi /etc/postgresql/12/main/pg_hba.conf
Add the following lines at the end of the file:
host all all 0.0.0.0/0 md5
host all all ::/0 md5 - To finish, restart the PostgreSQL service:
# systemctl restart postgresql.service
- Finally, test the access from a remote machine:
Open Firewall Port
In the firewall section of the client panel, you need to open port 5432 TCP to allow connections. For security reasons, it is recommended to open the port only to the public IP of your internet connection. How to create a firewall rule
With these simple steps, you will have configured PostgreSQL to allow remote connections.