I try to connect my Play Backend with my Postgresql Database, but get:
play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[PSQLException: Connection to 127.0.0.1:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.]]
Database is running on Port 5432 (checked it)
I´m using sbt and Play 2.8.2 Already tried 127.0.0.1 and 0.0.0.0 instead of localhost, doesn't matter (would be strange if, but you never know) Also im using Docker-Compose to create the database.
Code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
....
val connection= DriverManager.getConnection ("jdbc:postgresql://127.0.0.1:5432/smartmarkt", "postgres", "postgres")
println("Connected to PostgreSQL database!");
var statement = connection.createStatement();
var resultSet = statement.executeQuery("SELECT * FROM Article");
while (resultSet.next()) {
println(resultSet, resultSet.getString("price"));
}
Dockerfile
services:
web:
build: frontend/
ports:
- "80:80"
links:
- api
database:
image: "postgres" # use latest official postgres version
ports:
- "5432:5432"
environment:
- POSTGRES_DB=smartmarkt
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
#- ./postgres-data:/var/lib/postgresql/data # persist data even if container shuts downvolumes:
api:
build: backend/
ports:
- "8080:8080"
links:
- database
depends_on:
- database
I add jdbc in my build.sbt
libraryDependencies += jdbc
Also i have the postgresql-42.2.13.jar in my /lib dic, but to be honest, i dont actually know if its getting used.
postgres://database:5432Check this docs.docker.com/compose/networkingjdbc:postgresql://database:5432/smartmarktThis should work @Proxy. It's explained from docker documentation itself with postgres as example.jdbc:postgresql://database:5432WITHOUT /smartmark. I thoughdatabase:5432would link me directly to my configured database (they didnt use the environment tag in the documentation) . Now it works. Thanks.