![](/img/trans.png)
[英]Spring Boot not able to read data at server startup with Postgres DB connection
[英]Connection with postgres by spring boot
我有一個運行在 127.0.0.1:51282 上的 Postgres 服務器。 當我構建這個項目時,我得到 org.postgresql.util.PSQLException 並告知連接嘗試失敗。 這是我的配置:
application.properties:
spring.output.ansi.enabled=ALWAYS
spring.datasource.url=jdbc:postgresql://172.0.0.1:51282/postgres
spring.datasource.username=postgres
spring.datasource.password=kurwa123
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=create
Postgres 控制台:圖片
Gradle配置:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.18'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
非常感謝您的回答
發起spring申請時,應該在圖片下方。
現在單擊“生成”按鈕下載項目。 然后解壓,用方便的IDEA打開。 (例如:IntelliJ)
在這里,您必須像我一樣添加依賴項。
PostgreSQL Driver SQL
允許 Java 程序使用標准的、獨立於數據庫的 Java 代碼連接到 PostgreSQL 數據庫。 您可以下載並安裝最新的PostgreSQL 。
安裝步驟。
安裝時,您必須輸入密碼。 記住它,因為連接服務器需要該密碼。 此外,您必須提供 PostgreSQL 的端口號。現在,該應用程序已安裝在您的本地計算機上。 然后找到應用程序 [ pgAdmin 4 ],如下圖所示。
現在您可以看到一個界面,輸入密碼以連接服務器,如下所示。
因此,請按照以下步驟操作。
現在,您可以看到顯示服務器連接成功的通知。
目前,如果您還沒有創建數據庫,則可以按照以下步驟操作。
現在,您可以在下面看到數據庫和數據庫的用戶。
然后我們可以將我們的數據庫與創建的 spring 啟動應用程序連接起來。
數據庫配置:
應用程序.properties 文件:
spring.datasource.url=jdbc:postgresql://localhost:5432/EmployeeDB
spring.datasource.username=postgres
spring.datasource.password=1234
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.show-sql=true
spring.jpa.open-in-view=false
在這里,我給的PostgreSQL的端口號是5432,你可能不一樣。 因此,將您的端口號放在那里,數據庫名稱也應該與您在執行上述步驟時創建的數據庫相同。 此外,用戶名和密碼應該是您的。
現在,我們已經完成了。 運行應用程序並查看。
Tomcat started on port(s): 8080 (http) with context path ''
,您可以在終端中看到它。 如果要更改端口號 8080,
server.port=3000
應該添加到同一個文件中。
用終端更改端口IDEA鏡像時:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.