How to connect NodeJS on a droplet to postgres database on another droplet?

Posted October 11, 2018 2.9k views
Node.jsPostgreSQLUbuntu 18.04

I followed the steps in this tutorial to set up nodeJS web server in ubuntu-18-04.

Now what I should take into account while following this tutorial to set up postgres on another droplet in order to be able to connect to it from the other droplet?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

Hi there,

You would need to configure your PostgreSQL service to allow remote connections.

You can do that by following the steps here:

  • Update the postgresql.conf file:

First, find the file with the following command:

sudo find / -name "postgresql.conf"

Once you find the file, open it with your favorite text editor and find this line here:

listen_addresses = 'localhost'

And change it to:

listen_addresses = '*'

With that change, the service will no longer listen only on localhost:5432 but on and will be accessible from the outside world and not only the Droplet itself.

  • Update the pg_hba.conf file

Again in order to find the file you can use this command here:

sudo find / -name "pg_hba.conf"

Then open the file and add the following entry:

all  all  YOUR_IP_ADDRESS/32   trust

That way all IP addresses will be allowed to access the service.

Finally, restart the PostgreSQL service.

Hope that this helps!