How do I configure HikariCP for postgresql?
Asked Answered
P

3

11

I'm trying to use HikariCP in postgresql and I can't find anywhere the configuration for postgresql.

Please point me to any example for postgresql with HikariCP or any configurations tutorial for the same.

I tried to use it like below but it didn't work and then I realized it was meant for MySQL

public static DataSource getDataSource()

    {

            if(datasource == null)

            {

                    HikariConfig config = new HikariConfig();


            config.setJdbcUrl("jdbc:mysql://localhost/test");

            config.setUsername("root");

            config.setPassword("password");



            config.setMaximumPoolSize(10);

            config.setAutoCommit(false);

            config.addDataSourceProperty("cachePrepStmts", "true");

            config.addDataSourceProperty("prepStmtCacheSize", "250");
            config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");



            datasource = new HikariDataSource(config);

            }

           return datasource;

    }
Playacting answered 7/5, 2018 at 11:40 Comment(0)
D
10

You have example in HikariCP configuration wiki page

 Properties props = new Properties();

props.setProperty("dataSourceClassName", "org.postgresql.ds.PGSimpleDataSource");
props.setProperty("dataSource.user", "test");
props.setProperty("dataSource.password", "test");
props.setProperty("dataSource.databaseName", "mydb");
props.put("dataSource.logWriter", new PrintWriter(System.out));

HikariConfig config = new HikariConfig(props);
HikariDataSource ds = new HikariDataSource(config);
Despond answered 7/5, 2018 at 12:1 Comment(3)
where is the hostname specified?Clipboard
what about the schema, please? neither property datasource.hikari.schema nor DataSource::setSchema has any effect. PostgreSQL seems to use "current schema". Is there a way to set it in HikariDataSource?Runoff
@Runoff see #4169189Despond
C
1

This worked for me:

    HikariConfig config = new HikariConfig();

    config.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");
    config.addDataSourceProperty("serverName", "xxxxxxxxxxxxxxxxxx");
    config.addDataSourceProperty("portNumber", "xxxx");
    config.addDataSourceProperty("databaseName", "xxxxxxxxxx");
    config.addDataSourceProperty("user", "xxxxxxxxxxxxxxxxxxxxxxxxxx");
    config.addDataSourceProperty("password", "xxxxxxxxxxxxx");

    // postgress configuration for Hikari
    HikariDataSource ds = new HikariDataSource(config);

    return ds;

Also make sure you have the Maven dependency

<!-- Postgress JDBC Driver -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.10</version>
        </dependency>
Credulous answered 23/5, 2022 at 19:37 Comment(0)
E
-1

It worked for me with these setting in the application.yaml:

javax:
  sql:
    DataSource:
      postgresDataSource:
        dataSourceClassName: org.postgresql.ds.PGSimpleDataSource
        dataSource:
          #url: jdbc:postgresql://localhost:5432/test
          serverName: localhost
          portNumber: 5432
          databaseName: test
          user: ...
          password: ...
Elative answered 14/4, 2021 at 15:43 Comment(1)
And what mechanism reads this password? Why there is url commented out ?Niblick

© 2022 - 2024 — McMap. All rights reserved.