Java JDBC cannot connec to Fly.io postgres database

Hello! I might be missing something obvious, but I am trying to use the JDBC driver to connect to the Fly.io postgres database. My docker connects to other postgres databases fine (personal one at a different doamin), but trying to connect to postgres://MYUSERNAME:MYPASSWORD@top2.nearest.of.postgres-solutions.internal:5432/database_name results in Exception in thread "main" java.sql.SQLException: No suitable driver found for … It might not like the format of the domain name, since that is the only thing different in my case. I’ve tried both the latest version of the driver (org.postgresql:postgresql:42.5.0) and an older one (org.postgresql:postgresql:42.3.3).

Has anyone else had this issue or know why it would be doing so?

(It appears someone had an issue that might be related, but different error here: Can't connect to fly postgres via JDBC)

Thanks!, Michael
Thank you!

The Fly.io format is different to the JDBC URL format. Here’s a snippet that I wrote. It may work for you.

#!/bin/bash

# Format FLY's database URI. It uses 'postgres' instead of 'postgresql' in driver URL. Rewrite
# > postgres://USER:PASSWORD@HOST:PORT/DATABASE
# to
# > jdbc:postgresql://HOST/DATABASE?user=USER&password=PASSWORD

export DB_URI=$(echo "$DATABASE_URL" | sed -r "s/postgres:\/\/([^:]*):([^@]*)@([^:]*):(.*)/jdbc:postgresql\:\/\/\3:\4\?user=\1\&password=\2/g")
1 Like