diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java index 61b8540038..0678d0633f 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java @@ -198,6 +198,8 @@ public final class DataSourceBuilder { create(classLoader, "oracle.jdbc.datasource.OracleDataSource", OracleDataSourceSettings::new)); addIfAvailable(this.allDataSourceSettings, create(classLoader, "org.h2.jdbcx.JdbcDataSource", (type) -> new DataSourceSettings(type, (aliases) -> aliases.addAliases("username", "user")))); + addIfAvailable(this.allDataSourceSettings, create(classLoader, "org.postgresql.ds.PGSimpleDataSource", + (type) -> new DataSourceSettings(type, (aliases) -> aliases.addAliases("username", "user")))); } private static List resolveAvailableDataSourceSettings(ClassLoader classLoader) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java index 1da5bf3d8f..974bced9bc 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java @@ -33,6 +33,7 @@ import org.h2.Driver; import org.h2.jdbcx.JdbcDataSource; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; +import org.postgresql.ds.PGSimpleDataSource; import org.springframework.jdbc.datasource.SimpleDriverDataSource; @@ -129,6 +130,15 @@ class DataSourceBuilderTests { assertThat(h2DataSource.getUser()).isEqualTo("test"); } + @Test + void dataSourceCanBeCreatedWithPGDataSource() { + this.dataSource = DataSourceBuilder.create().url("jdbc:postgresql://localhost/test") + .type(PGSimpleDataSource.class).username("test").build(); + assertThat(this.dataSource).isInstanceOf(PGSimpleDataSource.class); + PGSimpleDataSource pgDataSource = (PGSimpleDataSource) this.dataSource; + assertThat(pgDataSource.getUser()).isEqualTo("test"); + } + @Test void dataSourceAliasesAreOnlyAppliedToRelevantDataSource() { this.dataSource = DataSourceBuilder.create().url("jdbc:h2:test").type(TestDataSource.class).username("test")