if you worked with java spring boot application, you would know that connecting database in you application is very simeple.
you need these dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
Then, in your configuration file (application.properties
), you add:
spring.datasource.url=jdbc:postgresql://localhost:5432/<YOUR_DATABASE_NAME>
spring.datasource.username=<YOUR_USERNAME>
spring.datasource.password=<YOUR_PASSWORD>
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
This connects your Spring Boot app to a PostgreSQL database.
The issue I’m facing now is when migrating to Fly.io.
I can create the PostgreSQL database on Fly without problems.
But I can’t connect my Spring Boot app to it.
here is what i see when i create database in terminal
Postgres cluster developemntdatabase created
Username: postgres
Password: 3GqY0VzYr6U6Bc3
Hostname: developemntdatabase.internal
Flycast: fdaa:xxxxxxxxxx
Proxy port: 5432
Postgres port: 5433
Connection string: postgres://postgres:3GqY0VzYr6U6Bc3@developemntdatabase.flycast:5432
Save your credentials in a secure place -- you won't be able to see them again!
Connect to postgres
Any app within the xxxx organization can connect to this Postgres using the above connection string
Now that you've set up Postgres, here's what you need to understand: https://fly.io/docs/postgres/getting-started/what-you-should-know/
the problem in here is there is one thing missing which is the database name.
spring boot expects
spring.datasource.url=jdbc:postgresql://localhost:5432/<YOUR_DATABASE_NAME>
spring.datasource.username=<YOUR_USERNAME>
spring.datasource.password=<YOUR_PASSWORD>
which can also be via ENV
fly gave me only username and pasword. and after port 5432 there must be a /databasename.
and if there is no name after port, spring wont connect it and it will return error
i need help with this.
here is also my dockerfile
FROM maven:3-amazoncorretto-17 AS build
WORKDIR /app/myapp
COPY pom.xml .
COPY src ./src
RUN mvn package
# Stage 2: Package the application in a runtime image
FROM amazoncorretto:17-alpine
WORKDIR /app/myapp
COPY --from=build /app/myapp/target/ROOT.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
and no worries the credential above is internal network and i destroyed the app before posting this