PSQLException: The server requested password-based authentication, but no password was provided

Viewed 7742

I have a Spring Boot project where I'm trying to connect to a local PostgreSQL database. The data source is being instantiated from DataSourceBuilder. For the purposes of this example, I have the database username, password, and database URL hard-coded (they will be passed in through properties files), and this is what the implementation looks like:

String url = "jdbc:postgresql://localhost:5433/<TABLENAME>";

String username = <USERNAME>;
String password = <PASSWORD>;

return DataSourceBuilder
            .create()
            .driverClassName("org.postgresql.Driver")
            .url(url)
            .username(username)
            .password(password)
            .build();

When I run the Spring Application main method, the following exception is thrown:

Caused by: org.postgresql.util.PSQLException: 
The server requested password-based authentication, but no password was provided.
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:534)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:141)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192)

I tried changing the postgres user password a few times using sudo passwd postgres but that didn't seem to work. Any ideas?

2 Answers

My guess is that you're not injecting your bean in the Spring Container, so use something like this:

@Bean
public DataSource dataSource() {
    String url = "jdbc:postgresql://localhost:5433/<DATABASE_NAME>";

    String username = <USERNAME>;
    String password = <PASSWORD>;

    return DataSourceBuilder
            .create()
            .driverClassName("org.postgresql.Driver")
            .url(url)
            .username(username)
            .password(password)
            .build();
}

Btw in your jdbcUrl you need to use the DatabaseName, not the TableName.

Note: I think is better to leverage how Spring Boot already has everything auto-configured, so I suggest to you to do something like this:

@Configuration
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public class DatabaseConfig extends HikariConfig {
    @Bean
    public DataSource dataSource() {
        return new HikariDataSource(this);
    }
}

And use the following properties:

spring.datasource.hikari.jdbcUrl=jdbc:postgresql://localhost:5433/blog
spring.datasource.hikari.username=user
spring.datasource.hikari.password=user
spring.datasource.hikari.driverClassName=org.postgresql.Driver

You can check one of my working examples here: https://github.com/vladucuvoican/simple-blog/blob/master/src/main/resources/application.properties

The problem may be occured by beans scanning

I have the same problem but logs did not indicate it.

Try:

@Configuration
public class DataSourceConfig {
    @Value("${spring.datasource.driver-class-name}")
    private String driverClass;
    @Value("${spring.datasource.data-username}")
    private String url;
    @Value("${spring.datasource.data-username}")
    private String userName;
    @Value("${spring.datasource.data-password}")
    private String password;

    @Bean
    public DataSource getDataSource() {
        return DataSourceBuilder.create()
                .driverClassName(driverClass)
                .url(url)
                .username(userName)
                .password(password)
                .build();
    }
}

If you use SpringBoot, move to package with SpringBootApplication runner or define scanning manually

Related