UnknownHostException when trying to connect spring to postgres in fly.io

I’m trying to deploy a Spring Boot application with a PostgreSQL database on Fly.io, but I keep encountering the UnknownHostException: db-app.flycast error. Here’s the process I’ve followed:

  1. I used the Fly CLI to create the PostgreSQL database named db-app:

    flyctl postgres create -n db-app --autostart true --region cdg --vm-size shared-cpu-1x --initial-cluster-size 1 --volume-size 2
    
  2. Next, I created a Spring Boot application named example-app on Fly.io:

    flyctl apps create example-app -o org
    
  3. I attached the PostgreSQL database db-app to the Spring Boot application:

    flyctl attach db-app -a example-app --database-name db --database-user user
    
  4. then I deployed it using a configuration file fly.toml:

    flyctl deploy --config fly.toml -a example-app -ha=false --volume-initial-size 2
    

    fly.toml configuration:

    kill_signal = "SIGINT"
    kill_timeout = 5
    swap_size_mb = 1024
    
    [build]
      dockerfile = "../docker/Dockerfile.app"
    
    [http_service]
      internal_port = 8080
      force_https = true
      auto_stop_machines = true
      auto_start_machines = true
      min_machines_running = 0
      processes = ['app']
    

I consistently receive the following error:

java.net.UnknownHostException: db-app.flycast

I have:

  • Confirmed that the PostgreSQL database db-app is created successfully on Fly.io.

  • Verified DNS resolution for db-app.flycast using ping and nslookup, ensuring it resolves to the correct IP address.

  • Ensured that both the Spring Boot application and PostgreSQL database (db-app) are within the same Fly.io organization and region, and reviewed network policies.

  • Verified that application.properties or application.yml in the Spring Boot application correctly points to db-app.flycast with the appropriate port and credentials.

  • Checked Fly.io logs (flyctl logs -a example-app) for any connectivity issues or errors related to database access.

How can I resolve the UnknownHostException issue and ensuring successful deployment of my Spring Boot application with PostgreSQL on Fly.io?

Maybe parts of fly are (temporarily) down.

my machine just got totally unreachable , that’s why I am in that forum now

Hi @PookyFox :wave:

Sometimes database drivers may prefer IPv4 over IPv6 by default, and in that case, they may complain that the .flycast domain does not exist because it does not resolve as an IPv4 address.

If you can manually resolve the .flycast address, then this could be what is happening to you. One workaround you could try is to replace the hostname in your database URL with the resolved IPv6 address of the .flycast domain. The “proper” solution would be to find out whether there is an option in your database connector to always try IPv6, but since Flycast addresses are stable, using it directly in your app should be okay as well.

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