I’m trying to connect to Postgres using JDBC.
The database is running in the Docker container.
I am using Windows Docker Desktop (Windows 10 pro) with WSL2.
I launch the Docker container using docker-compose.
Here is the content of docker-compose:
version: "3.8"
services:
db:
image: postgres:13.2
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: yoi
ports:
- "5432:5432"
volumes:
- "./postgres:/var/lib/postgresql/data"
web:
image: openjdk:15
depends_on:
- db
ports:
- "4567:4567"
volumes:
- ".:/share/yoi"
working_dir: /share/yoi
command: ./gradlew run
As you can see, it’s supposed to run the database first, and then run the java application second.
Here is the content of the java application:
package app;
import java.sql.DriverManager;
import java.sql.SQLException;
public final class Main {
public static void main(final String... args) {
System.out.println("START");
try {
DriverManager.getConnection(
"jdbc:postgresql://db:5432/yoi",
"postgres",
"postgres"
);
} catch (final SQLException ex) {
ex.printStackTrace();
}
System.out.println("END");
}
}
I ran the command docker-compose up
and got this result:
$ docker-compose up
Creating network "javatest_default" with the default driver
Creating javatest_db_1 ... done
Creating javatest_web_1 ... done
Attaching to javatest_db_1, javatest_web_1
web_1 | Downloading https://services.gradle.org/distributions/gradle-6.7-bin.zip
db_1 |
db_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1 |
db_1 | 2021-03-29 06:09:21.766 UTC [1] LOG: starting PostgreSQL 13.2 (Debian 13.2-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1 | 2021-03-29 06:09:21.766 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2021-03-29 06:09:21.766 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2021-03-29 06:09:21.776 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2021-03-29 06:09:21.809 UTC [27] LOG: database system was shut down at 2021-03-29 06:08:02 UTC
db_1 | 2021-03-29 06:09:21.835 UTC [1] LOG: database system is ready to accept connections
web_1 | .........10%..........20%..........30%..........40%..........50%.........60%..........70%..........80%..........90%..........100%
web_1 |
web_1 | Welcome to Gradle 6.7!
web_1 |
web_1 | Here are the highlights of this release:
web_1 | - File system watching is ready for production use
web_1 | - Declare the version of Java your build requires
web_1 | - Java 15 support
web_1 |
web_1 | For more details see https://docs.gradle.org/6.7/release-notes.html
web_1 |
web_1 | Starting a Gradle Daemon (subsequent builds will be faster)
web_1 | > Task :compileJava
web_1 | > Task :processResources UP-TO-DATE
web_1 | > Task :classes
web_1 |
web_1 | > Task :run
web_1 | START
web_1 | java.sql.SQLException: No suitable driver found for jdbc:postgresql://db:5432/yoi
web_1 | at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
web_1 | at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
web_1 | at app.Main.main(Main.java:11)
web_1 | END
web_1 |
web_1 | Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
web_1 | Use '--warning-mode all' to show the individual deprecation warnings.
web_1 | See https://docs.gradle.org/6.7/userguide/command_line_interface.html#sec:command_line_warnings
web_1 |
web_1 | BUILD SUCCESSFUL in 27s
web_1 | 3 actionable tasks: 2 executed, 1 up-to-date
javatest_web_1 exited with code 0
I also tried jdbc:postgresql://localhost:5432/yoi
, but the result was the same.
Does anyone have any idea about the cause and resolution?
2
Answers
You need to add the jdbc driver to your java application. If you have a maven project you can add it by adding the following depencency to you pom.xml:
For Gradle:
Can you try
Class.forName("org.postgresql.Driver");