Connect Postgres on Fly to a SaaS

I want to connect my postgres on fly app to a SaaS (https://www.getlago.com) which basically require postgres credentials and ip white list.

Obviously, it won’t work out of the box since postgres on fly requires a wireguard tunnel.

Before I start hacking, how would you do this in the simplest way possible?

So we can do this in a few steps:

  1. Pull down your fly.toml file.

You can do this by running:

 flyctl config save --app <app-name>
  1. Allocate an IP address to your instance, if you haven’t already.
# See current list of ips.
flyctl ips list
# Allocate an IPv6 address 
flyctl ips allocate-v6
  1. Add the following to your fly.toml file.
[[services]]
  internal_port = 5432
  protocol = "tcp"

# For secure connections
[[services.ports]]
 handlers = ["tls"]
 port = 443

# For insecure connections. 
[[services.ports]]
 handlers = []
 port = 10000
  1. Deploy your configuration changes.
# Note: Use flyio/postgres:12 if running PG 12.
flyctl deploy . --app <app-name> --image flyio/postgres:13

Using the insecure connection as an example, you should now be able to connect by running:

psql postgres://postgres:<password>@<app-name>.fly.dev:10000

Note: It may take a few minutes before the allocated ip address is recognized.

Let me know if you have any other questions!

2 Likes

I just added some formal documentation around this: Multi-region PostgreSQL · Fly

2 Likes