Home > other >  R2DBC Postgres connection fails
R2DBC Postgres connection fails

Time:11-17

My connection config:

@Configuration
@EnableR2dbcRepositories
public class DatabaseConfiguration extends AbstractR2dbcConfiguration {

    @Bean
    public PostgresqlConnectionFactory connectionFactory() {
        return new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
            .host("r2dbc:postgresql://mydb.alfa")
            .port(5432)
            .username("admin")
            .database("postgres")
            .password(password)
            .build()
        );
    }
}

My repository:

public interface ReactiveOdometerRepository extends ReactiveCrudRepository<OdometerEntity, String> {
    Flux<OdometerEntity> findTop1By(String name);
}

My entity:

@Setter
public class OdometerEntity {
    @Getter
    @Id
    private String name;
    @Getter
    private int value;
}

When I try querying the DB, it fails:

@PostMapping(value = "/query", produces = MediaType.APPLICATION_JSON_VALUE)
public int get(@RequestBody RequestObject request) {
    return odometerRepository.findTop1ByName(request.getName()).getValue();

Error:

io.r2dbc.postgresql.PostgresqlConnectionFactory$PostgresConnectionException: 
Cannot connect to r2dbc:postgresql://mydb.alfa/<unresolved>:5432

Not sure what the unresolved is doing in there.

How do I fix this error? thanks

CodePudding user response:

You have to specify host name in the host, not the url there


 PostgresqlConnectionFactory(
     PostgresqlConnectionConfiguration.builder()
         .host("localhost")
         .database("blogdb")
         .username("user")
         .password("password")
         .codecRegistrar(
              EnumCodec
                 .builder()
                 .withEnum("post_status", Post.Status.class)
                 .build()
         )
         .build()
)

There are a few r2dbc connectionfactory examples from my Github.

CodePudding user response:

Your host variable is wrong. It should contain only the name or ip-address of the host not the full URL.

So instead of host("r2dbc:postgresql://mydb.alfa") use host("mydb.alfa").

@Bean
public PostgresqlConnectionFactory connectionFactory() {
    return new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
        .host("mydb.alfa")
        .port(5432)
        .username("admin")
        .database("postgres")
        .password(password)
        .build()
    );
}
  • Related