I’m a .NET dev trying to learn java. I have a postgres instance running at localhost:5432 using docker, with database “knowledgemap”, username “knowledgemap” and password “test123”. I’m able to connect to this postgres instance using a .NET application and do all operations correctly. But when I’m try to connect to it using my java application, it’s breaking with the error message FATAL: password authentication failed for user "knowledgemap"
. I’m using a windows 11 laptop. I checked the firewall settings and allowed java and IntellijIDEA to use public and private networks, but still getting the error. Please give any advice where to start debugging this issue.
Java code:
package org.example;
import java.sql.*;
public class Main {
public static void main(String[] args) throws SQLException {
String url = "jdbc:postgresql://localhost:5432/knowledgemap";
String username = "knowledgemap";
String password = "test123";
Connection db = DriverManager.getConnection(url, username, password);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Java_PostgresConnectionTest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.4</version>
</dependency>
</dependencies>
</project>
EDIT:
Postgres version: PostgreSQL 16.3 (Debian 16.3-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
Connection string used in .NET application:
UserID=knowledgemap;Password=test123;Host=localhost;Port=5432;Database=knowledgemap;
Driver and version used in .NET app:
Npgsql.EntityFrameworkCore.PostgreSQL v8.0.4
Relevant portion of pg_hba.conf:
TYPE DATABASE USER ADDRESS METHOD
local all all trust
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
8
There was another postgres instance running in WSL and I was totally unaware of it. apparently java was always going to the WSL one and .NET was always going to the docker one.
1