0

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.

5
  • You can access the database from your application using postgres://database:5432 Check this docs.docker.com/compose/networking Commented Jun 9, 2020 at 15:28
  • @ShankarShastri dosnt work for me, now i get " No suitable driver found for jdbc:postgres://database:5432" (also without the jdbc: leading) Not sure if this is a step backward or forward Commented Jun 9, 2020 at 16:28
  • jdbc:postgresql://database:5432/smartmarkt This should work @Proxy. It's explained from docker documentation itself with postgres as example. Commented Jun 9, 2020 at 16:36
  • I tried jdbc:postgresql://database:5432 WITHOUT /smartmark. I though database:5432 would link me directly to my configured database (they didnt use the environment tag in the documentation) . Now it works. Thanks. Commented Jun 9, 2020 at 16:52
  • I've added a answer, please mark the same. Commented Jun 9, 2020 at 16:57

1 Answer 1

1
jdbc:postgresql://database:5432/smartmarkt

The above connection string will work as within docker network containers can be accessed by their hostnames.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.