How to make connection between 2 VPS (1 VPS for Phoenix App & 1 VPS for Postgresql

Dear all,
I have purchase 2 VPS with different VPS Provider, 1 for Digital Ocean & 1 for Vultr, Please help me how to make connection between 2 VPS, 1 VPS for Phoenix App & 1 VPS for Postgresql ?

I think this will be way easier for you to have both Phoenix and Postgresql on the same VPS, and maybe more secure and faster.

Anyway whatever is your reason to have them on different server, your question is more about Postgresql and not Elixir/Phoenix.

You want to make some search about how to enable remote access to a PostgreSQL server. Maybe they have some guides at Vultr?

From the Phoenix side it won’t make a big difference whether you are connecting to a database service remotely or locally. You will have just to add the right configs (localhost/remote_host_ip, user, pass…).

2 Likes

Hi @ariandanim this is not really an Elixir or Phoenix question. Some people here might be able to help, but this has a lot more to do with cloud networking.

2 Likes

You have two main steps:

  1. how to access your PostgreSql from outside the VPN;
  2. How to put those credentials/“coordinates” in Phoenix so that it points and has the right to access it.
    You’re lucky as Phoenix as long as you pass to it the correct (2) will create the database(s) you need and will work without any issue.
    So, in practice, you need to:
  • read the Phoenix guides/docs about creating/manipulating a database with Ecto and where to put the credentials/“coordinates”;
  • understand with your VPN provider (where you host the PostgreSql) how to access it through IP/password, …
    But you’ll have to read and understand the problem first.
    Hope this helps!
1 Like

So there are two ways to do this

  1. Make your postgresql run on a public ip and setup ssl, etc. to ensure it is communicated securely.
  2. Setup wireguard as a private vpn between two hosts.

I would recommend that you just run them both on the same machine to be honest. I do believe that running wireguard is probably the better option if you don’t want to use the same machine.

5 Likes

Thanks Sir, my reason to separate VPS between Phoenix App and Postgresql is to avoid some disaster in the future that I don’t exactly what it is,

If we can make app connecting database like firebase DAAS (Database As A Service) it perhaps also we can our VPS become standalone database, another reason for me is to reduce cost

Pardon me if my misunderstood about question, hopefully getting insight from seniors here :blush:

Conventional approach is to have both the master db and app servers at the same Provider and same datacenter. (I.e., super-fast & reliable networking) Then, for extra redundancy, have follower db’s at other providers.

So that’s two VM’s, one provider, one datacenter as the first start. But TBH, if your app is an initial proof of concept and/or not bringing in real traffic and money, you can easily run both on one VM, and get pretty far with that.

At Linode, my db is a managed cluster of 3 VM’s, but it presents itself as a single always-available db. Then, my apps are on the second VM that’s a dedicated-CPU with containers that I manage with Dokku. All the network traffic between the apps and the db is on Linode’s private network, and it’s unmetered. Linode has a nice free Firewall app that I’ve configured. Both VM’s get auto-backups by Linode. Total cost = $100/month.

I haven’t yet found the need for more redundancy.

Finally, though, I’m always trying out new hosting ideas in the background. I’m extremely impressed with fly.io and I’m working on that atm.

7 Likes

Vultr already has managed db assuming you’re using Postgres.

2 Likes

:sweat_smile: Oh no, in fact, I’m far from a qualified person to give lessons here. To tell you the truth, I wouldn’t have chosen your strategy, perhaps for the wrong reasons. I feel like it requires more experience (which I don’t have and I’m intimidated to approach) and that if I do a little wrong everything could go to hell.

For example, I’m thinking how do I make my Phoenix application’s response acceptable to clients if my database server is not in the same region as the Phoenix application server. Is it just a matter of region or bandwidth also matters here?

Are there ways to tighten security at the database server level so that it only accepts connections coming from the Phoenix application server? How to do that exactly ?

Personally all this seems complex to me and I would prefer to be really sure of what I am doing.

So yes I can only finally encourage you to continue your quest because that’s how you can improve yourself.

Anyway there are more experienced and qualified people here who already gave you good directions.

1 Like

I’m not sure that separating the app from the db will help you avoid a disaster. I think it will make it worse. With this approach you have more possible points of failure (if either DO or Vultr go down, or the connectivity between them goes down, your application will be broken). In addition, it is more complicated to manage and you will have a larger attack surface for hackers as you need to expose Postgres to the internet (or correctly implement a VPN without leaking details).

As @dogweather says, you are better off regularly copying/replicating the db off your main provider onto a secondary provider. If you have the code for the application, having the ability to restore the database onto new infrastructure is the most important thing for basic disaster recovery. You will also need to understand how to deploy the application onto that new infrastructure, update DNS records etc, but there’s no way to reconstruct the system if you lose the database.

5 Likes