I'm trying to connect my local spring boot application to an aws rds. While I can perfectly connect with pgadmin or from inside IntelliJ it just isn't working inside my spring boot application on localhost. This is my spring boot config:
server.error.whitelabel.enabled=false
spring.jpa.database=POSTGRESQL
spring.sql.init.platform=postgres
spring.datasource.url=jdbc:postgresql://odyxdb.c56c5slasovs.eu-west-1.rds.amazonaws.com:5432/odyxdb
spring.datasource.username=TEST
spring.datasource.password=TEST
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
Related
I have a working setup of Spring boot application connecting to Postgres DB in docker. The same Spring boot application throws an exception when I move it to another docker container. The Postgres docker was unchanged. What might be wrong ? Why is the same application working outside docker and not connecting to Postgres when put inside docker.
org.postgresql.util.PSQLException: Connection to MYDOMAIN:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
application.properties
spring.datasource.url=jdbc:postgresql://MYDOMAIN:5432/
UPDATE
When I changed MYDOMAIN to the public IP address of the machine hosting Postgres docker, it worked fine. But why is the domain name not getting resolved ?
Because a Docker Container is an isolated environment which you only have your spring boot application in it. so inside that container, there are no Postgres running on port 5432.
you can follow the instruction in this link to create a docker-compose file in which you can address the PostgreSQL Docker Container in that file to your dockerized spring boot application.
I could find the root cause finally. From /etc/hosts file, when I removed MYDOMAIN against 127.0.0.1, the spring boot application was able to resolve MYDOMAIN via internet and access Postgres.
I have a spring boot app running in a gcp k8s cluster using iam-auth to connect to postgres.
Spring boot settings when the service is deployed on the cluster that work without problem:
spring:
cloud:
gcp:
sql:
database-name: my_database
instance-connection-name: my-project-12345:europe-north1:mydb-instance-56aaf6d6
enable-iam-auth: true
datasource:
username: myiam#my-project-12345.iam
So now for dev reasons I want to run my app locally from my IDE using port forwarding to reach the postgres database:
kubectl port-forward svc/cloudsql-proxy-whatever -n whatevernamespace 5432:5432
Is there any configuration that can be used here? I have tried different combinations of the properties below, commented and uncommented, of the properties below without success.
spring:
cloud:
gcp:
sql:
database-name: my_database
instance-connection-name: my-project-12345:europe-north1:mydb-instance-56aaf6d6
enable-iam-auth: true
datasource:
username: myiam#my-project-12345.iam
url: jdbc:postgresql://localhost:5432/
Somehow, I need to square the instance-connection-name with the fact that port forwarding (localhost) is being used.
I know I can do this with username and password auth...no problems...but now user/password is disabled and iam-auth is the secure way to go. Is it even possible to portforward using iam-auth?
(Names have been changed to protect the innocent.)
when I use maven to build my spring boot app the application needs to have a connection to my postgresql database in the application.properties. It only can connect when I use
spring.datasource.url=jdbc:postgresql://localhost:5432/springbootdb
but when I containerize the buileded jar file, my application can't connect to my postgres database hosted outside the Container.
what is the solution for this problem?
I am still a Beginner
From the 18.03 docs:
I want to connect from a container to a service on the host
The host has a changing IP address (or none if you have no network
access). From 18.03 onwards our recommendation is to connect to the
special DNS name host.docker.internal, which resolves to the internal
IP address used by the host.
The gateway is also reachable as gateway.docker.internal.
example:
spring.datasource.url=jdbc:postgresql://host.docker.internal:5432/springbootdb
Where you hosted your PostgreSQL database?
Replace localhost with actual machine IP address (PostgreSQL database machine address)
spring.datasource.url=jdbc:postgresql:<ip_address>:5432/springbootdb
I'm trying to use the Heroku postgresql add-on in my spring boot application (locally first). In my application properties, I removed the local server information and updated it with the Heroku credentials. I also added the posgresql dependency in my gradle file.
My code:
spring.datasource.url=jdbc:postgres://qfxqpoceljdtfo:f50b14498b7be95f0a1f4cf466b09b54ed8bbefaac9aa28a97b14719d0625e56#ec2-23-21-201-255.compute-1.amazonaws.com:5432/d6eqamh4egp1g0
spring.datasource.username=qfxqpoceljdtfo
spring.datasource.password=***Password***
spring.datasource.driver-class-name=org.postgresql.Driver
Heroku Credentials:
Host: ec2-23-21-201-255.compute-1.amazonaws.com
Database: d6eqamh4egp1g0
User: qfxqpoceljdtfo
Port: 5432
Password: ***Password***
URI postgres://qfxqpoceljdtfo:f50b14498b7be95f0a1f4cf466b09b54ed8bbefaac9aa28a97b14719d0625e56#ec2-23-21-201-255.compute-1.amazonaws.com:5432/d6eqamh4egp1g0
Heroku CLI: heroku pg:psql postgresql-metric-82455 --app battlesh1p
The resulting error is:
java.sql.SQLException: Driver:org.postgresql.Driver#20999517 returned null for URL
I've also tried using:
jdbc:postgresql://ec2-23-21-201-255.compute-1.amazonaws.com:5432/d6eqamh4egp1g0?sslmode=require
Which results in this error
org.postgresql.util.PSQLException: FATAL: no pg_hba.conf entry for host "94.248.76.191", user "qfxqpoceljdtfo", database "d6eqamh4egp1g0", SSL off
In case anyone comes across this. The Heroku documentation worked fine in configuring my application to use the postgres database plug-in in production. But I could never get my spring app to run locally and access the host DB.
I'm getting timeout errors from my Java application (Spring Boot) that uses a MongoDB, the connection works when I run the jar, however when it is Dockerized, the connection times out. I'm not sure if I'm just not configuring something correctly with Docker?
Use the --link docker run option to easily connect to your mongo container with your specified host name
If your dockerized Spring Boot app is using 'localhost' to try to connect to the dockerized Mongo instance, it will fail because 'localhost' in that context refers to the container (the one running the Spring Boot app, which is not running Mongo).
If the name of your Mongo container is 'mongo' you can find the docker network IP address of that container with
$ docker inspect mongo | grep IPAddress
Then you should be able to configure your Spring Boot container to connect to mongo at that address.