Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to connect to Postgres DB due to the authentication type 10 is not supported

I have recently tried my hands on Postgres. Installed it on local (PostgreSQL 13.0). Created a maven project and used Spring Data JPA, works just fine. Whereas when I tried using Gradle project, I am not able to connect to the DB and keep getting the following error.

org.postgresql.util.PSQLException: The authentication type 10 is not supported. Check that you have configured the pg_hba.conf file to include the client's IP address or subnet, and that it is using an authentication scheme supported by the driver. at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:614) ~[postgresql-42.1.4.jar:42.1.4] at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:222) ~[postgresql-42.1.4.jar:42.1.4] at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.1.4.jar:42.1.4] at org.postgresql.jdbc.PgConnection.(PgConnection.java:194) ~[postgresql-42.1.4.jar:42.1.4] at org.postgresql.Driver.makeConnection(Driver.java:450) ~[postgresql-42.1.4.jar:42.1.4] at org.postgresql.Driver.connect(Driver.java:252) ~[postgresql-42.1.4.jar:42.1.4] at java.sql.DriverManager.getConnection(Unknown Source) [na:1.8.0_261] at java.sql.DriverManager.getConnection(Unknown Source) [na:1.8.0_261] at org.postgresql.ds.common.BaseDataSource.getConnection(BaseDataSource.java:94) [postgresql-42.1.4.jar:42.1.4] at org.postgresql.ds.common.BaseDataSource.getConnection(BaseDataSource.java:79) [postgresql-42.1.4.jar:42.1.4]

I tried using JDBCTemplate as well. Doesn't work

Modified the pg_hba.cfg file referring to this post - Doesn't work

Used the deprecated Lib of - Doesn't Work either.

Please Suggest me a solution for this problem.

My code and Config:

    @Configuration
    public class DataSourceConfig {
    
        
        @Bean
        public DriverManagerDataSource getDataSource() {
            DriverManagerDataSource dataSourceBuilder = new DriverManagerDataSource();
            dataSourceBuilder.setDriverClassName("org.postgresql.Driver");
            dataSourceBuilder.setUrl("jdbc:postgresql://localhost:5432/postgres");
            dataSourceBuilder.setUsername("postgres");
            dataSourceBuilder.setPassword("root");
            return dataSourceBuilder;
        }
        
    }



@Component
public class CustomerOrderJDBCTemplate implements CustomerOrderDao{
    
    private DataSource dataSource;
    
    private JdbcTemplate jdbcTemplateObject;

    @Autowired
    ApplicationContext context;
    
    public void setDataSource() {
        //Getting Bean by Class
        DriverManagerDataSource dataSource = context.getBean(DriverManagerDataSource.class);
        this.dataSource = dataSource;
        this.jdbcTemplateObject = new JdbcTemplate(this.dataSource);
    }

@Override
    public Customer create(Customer customer) {
        setDataSource();
        String sql = "insert into CustomerOrder (customerType, customerPayment) values (?, ?)";
        //jdbcTemplateObject.update(sql, customerOrder.getCustomerOrderType(), customerOrder.getCustomerOrderPayment());
        
        KeyHolder holder = new GeneratedKeyHolder();
        jdbcTemplateObject.update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setString(1, customer.getType());
                ps.setString(2, customer.getPayment());
                return ps;
            }
        }, holder);

        long customerId = holder.getKey().longValue();
        customer.setCustomerID(customerOrderId);
        return customer;
        
    }

}

dependencies

implementation('org.springframework.boot:spring-boot-starter-web')
    compile("org.springframework.boot:spring-boot-devtools")
    compile(group: 'org.postgresql', name: 'postgresql', version: '42.1.4')
    compile("org.springdoc:springdoc-openapi-ui:1.4.1")
    compile("org.springframework:spring-jdbc:5.2.5.RELEASE")

password_encryption is set like this:

postgres=# show password_encryption;
 password_encryption
---------------------
 scram-sha-256
(1 row)
like image 441
Tushar Banne Avatar asked Oct 05 '20 14:10

Tushar Banne


People also ask

Why doesn't PostgreSQL support password_encryption using JDBC?

Now you are using an old JDBC driver version on the client side that does not support that authentication method, even though PostgreSQL introduced it in v10, three years ago. You should upgrade your JDBC driver. An alternative would be to set password_encryption back to md5, but then you'll have to reset all passwords and live with lower security.

Why does PostgreSQL use SCRAM-SHA-256 authentication instead of MD5?

Now if you have a SCRAM-hashed password, PostgreSQL uses scram-sha-256 authentication even if it says md5 in pg_hba.conf. That is a compatibility feature that is supposed to make the transition to SCRAM easier.

Should I downgrade password_encryption in PostgreSQL to MD5?

Downgrading password_encryption in PostgreSQL to md5, changing all the passwords and using the md5 authentication method is a possible, but bad alternative. It is more effort, and you get worse security and old, buggy software. Show activity on this post.

Why can't I connect to Postgres with rust?

This isn't a Rust-specific question; the issue applies to any application connecting to a Postgres DB that doesn't support the scram-sha-256 authentication method. In my case it was a problem with the Perl application connecting to Postgres. These steps are based on a post. You need to have installed the latest Postgres client.


4 Answers

I solved similar issue by applying below steps in PostgreSQL Version 13:

  1. Change password_encryption to md5 in postgresql.conf
Windows: C:\Program Files\PostgreSQL\13\data\postgresql.conf
GNU/Linux:           /etc/postgresql/13/main/postgresql.conf

enter image description here

  1. Change scram-sha-256 to md5 in pg_hba.conf
Windows: C:\Program Files\PostgreSQL\13\data\pg_hba.conf
GNU/Linux:           /etc/postgresql/13/main/pg_hba.conf
host    all             all             0.0.0.0/0               md5

enter image description here

  1. Change Password ( this restore password in md5 format).

    Example: ALTER ROLE postgres WITH PASSWORD 'root';

  2. Make sure you set listen_addresses = '*' in postgresql.conf if you are working non production environment.

like image 78
Kishor K Avatar answered Oct 24 '22 10:10

Kishor K


Get your pg_hba.conf File in the Directory C:\Program Files\PostgreSQL\13\data\pg_hba.conf

And Simply Change scram-sha-256 under Column Method to trust.

enter image description here

It worked For me!

like image 39
Bukhari Syed Avatar answered Oct 24 '22 08:10

Bukhari Syed


According to the wiki, the supported JDBC driver for SCRAM-SHA-256 encryption is 42.2.0 or above. In my case, the driver was 41.1.1. Change it to 42.2.0 or above. That fixed it for me.

(Maven, pom.xml):

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.2.0</version>
</dependency>
like image 29
noamyg Avatar answered Oct 24 '22 08:10

noamyg


By setting password_encryption to scram-sha-256 (which is the default value in v13) you also get scram-sha-256 authentication, even if you have md5 in pg_hba.conf.

Now you are using an old JDBC driver version on the client side that does not support that authentication method, even though PostgreSQL introduced it in v10, three years ago.

You should upgrade your JDBC driver. An alternative would be to set password_encryption back to md5, but then you'll have to reset all passwords and live with lower security.

like image 7
Laurenz Albe Avatar answered Oct 24 '22 09:10

Laurenz Albe