Connect to a PostgreSQL database in a private network

WebRelay
2 min readAug 20, 2023

--

Imagine this, you want to connect to a PostgreSQL database on a remote server, but the database is in a private network, or behind a firewall, or maybe even running behind a NAT, maybe in a server in your basement.

At this point, you have a few options.

  • Set up a VPN service, or use a service like Tailscale, You can make your remote server and your Machine a part of the same Tailnet. This requires some configuration on your part but once set up its works well.
  • Set up ssh port forwarding.
  • Use a tunneling solution like Ngrok, You can expose the Postgres connect port to the internet and connect from your laptop
  • Open ports in your firewall or change security configurations.

All of these methods work but require some sort of networking configuration, or you will need to expose the service on the public internet, which even though may provide security by obscurity, is not really a secure method to access your database.

WebRelay provides an alternative method of achieving this with zero configuration and no software installs. You will not need to make any change in your networking configuration and you will be accessing the remote database as if it was a local service running on your machine in 4 simple steps.

  1. Download the WebRelay binary on both systems.
  2. Login on both systems using your email id
./webrelay login john.doe@email.com

3. Start the postgres service from your remote server

./webrelay service start -p 5432 -n postgres -t tcp

You can add the ‘-u <IP> flag if you are on a bastion host

./webrelay service start -p 5432 -n postgres -t tcp -u <ip-addr>

4. On your local machine, list the service and then connect to it

./webrelay service list 
 ✔  10031  21:46:02
+-----+----------+------+-------+
| NO | SERVICE | TYPE | OWNER |
+-----+----------+------+-------+
| 1 | postgres | tcp | true |
+-----+----------+------+-------+
./webrelay client -n postgres -p 9000

And that's it, The remote Postgres service is now available at your localhost at port 9000, you can connect to it. When you are done, simply CTRL + C on the remote machine and all connections will be terminated.

What's next

In this setup, you still had to access your remote server to start the service on WebRelay. You can avoid that by using remote relays which act like a remote control for you to bring up and stop services on demand. In the next post, we will also discuss how you can share services with other users without them needing to get access to your infrastructure.

--

--

WebRelay
WebRelay

Written by WebRelay

Simple networking toolkit for developers

No responses yet