Monday, November 1, 2021

[SOLVED] How to set up my web app database to use a postgres db in another machine?

Issue

So I'm using prisma with next.js in my web app and I need to provide a database url for my postgres db. However the db that I want to reach is in another machine where I have to use ssh tunnel. I'm sorry if I'm a bit all over the place I'm still new with this and I'm still learning. Below is what I need to modify.

DATABASE_URL='postgresql://<user>:<pass>@<db-host>:<db-port>/<db-name>'

Anyone willing to help/explain? Thank you.


Solution

First, you'll need to create the tunnel. You can find lots of guides that will show you how to do that, but as an example, I'll paste one way:

ssh -L 5432:DBHOST:5432 USER@PROXY -N

Replace DBHOST with the DB machine name/address, USER with the ssh user and PROXY with the name/address of the machine that tunnels the connections.

Tools such as AutoSSH could be used in order to handle the tunnel in a more robust way.

Once the tunnel is up, replace <db-host> with localhost and <db-port> with 5432 in the DB url that you posted.



Answered By - Jonathan Jacobson