How can I get a JDBC URL for fly postgres?

Hello!

I am trying to connect my Spring Boot API to a Fly.io PostgreSQL database. The API is also hosted on Fly.io.

When connecting with Spring Boot, a JDBC DATABASE_URL is needed. However, when I use the URL provided by Fly.io, I encounter connection errors.

The format of a Fly.io PostgreSQL URL is: postgres://{username}:{password}@{hostname}:{port}/{database}?options

The format of a JDBC DATABASE_URL is: jdbc:postgresql://{hostname}:{port}/{database} The username and password are provided separately.

I was able to connect by replacing the hostname with the IPv6 address obtained using the following query inside Flyctl:

SELECT current_setting('server_version') AS version,
       inet_server_addr() AS host,
       inet_server_port() AS port,
       current_database() AS database;

This solution works, but the IPv6 address may change if the DB server is restarted.

Can someone provide insight on how to handle this issue?

Thank you!

Hey @Mr-Robot

Fly Apps are connected by a mesh of WireGuard tunnels using IPv6.

I hope that helps,
Sergio TurpĂ­n

Hi there and thanks for trying out fly.io! :wave:

As mentioned above by @sturpin, all Fly apps are connected via an internal Wireguard mesh network called 6PN. The 6PN IPv6 address and the hostname <app-name>.internal can be used to access other Fly apps under the same organization. If the hostname you used in the “broken” JDBC URL that caused connection errors was already the correct <app-name>.internal, then it could be a DNS configuration issue (try resolving DNS outside of Java / Sprint Boot?), or something Java / Spring Boot-specific (maybe it doesn’t like IPv6-only hostnames?).

You could also obtain a stable internal IP to access your PostgreSQL cluster with a Flycast address. You can read more about the tradeoffs, precautions to take, and private networking in general in our docs.

2 Likes

Hey @PeterCxy!

The solution was to use the <app-name>.internal as the hostname like you mentioned.

Thank you very much for your help :smile:

@sturpin thank you for trying to help :smiling_face:

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.