英文:
Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections
问题
我在执行 docker-compose up 时遇到以下错误。
springbootapp | org.postgresql.util.PSQLException: 拒绝连接到 localhost:5432。请检查主机名和端口是否正确,并且 postmaster 是否接受 TCP/IP 连接。
springbootapp | at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:280) ~[postgresql-42.2.8.jar!/:42.2.8]
springbootapp | at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.8.jar!/:42.2.8]
springbootapp | at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195) ~[postgresql-42.2.8.jar!/:42.2.8]
springbootapp | at org.postgresql.Driver.makeConnection(Driver.java:458) ~[postgresql-42.2.8.jar!/:42.2.8]
springbootapp | at org.postgresql.Driver.connect(Driver.java:260) ~[postgresql-42.2.8.jar!/:42.2.8]
springbootapp | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.2.jar!/:na]
springbootapp | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:354) ~[HikariCP-3.4.2.jar!/:na]
springbootapp | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:202) ~[HikariCP-3.4.2.jar!/:na]
springbootapp | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:473) [HikariCP-3.4.2.jar!/:na]
springbootapp | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:554) [HikariCP-3.4.2.jar!/:na]
springbootapp | at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) [HikariCP-3.4.2.jar!/:na]
springbootapp | at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) [HikariCP-3.4.2.jar!/:na]
springbootapp | at org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:56) [flyway-core-6.0.8.jar!/:na]
springbootapp | at org.flywaydb.core.internal.jdbc.JdbcConnectionFactory.<init>(JdbcConnectionFactory.java:80) [flyway-core-6.0.8.jar!/:na]
springbootapp | at org.flywaydb.core.Flyway.execute(Flyway.java:438) [flyway-core-6.0.8.jar!/:na]
springbootapp | at org.flywaydb.core.Flyway.migrate(Flyway.java:149) [flyway-core-6.0.8.jar!/:na]
springbootapp | at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:65) [spring-boot-autoconfigure-2.2.6.RELEASE.jar!/:2.2.6.RELEASE]
**Dockerfile:**
FROM openjdk:8-jdk-alpine
VOLUME /tmp
EXPOSE 8082
RUN mkdir -p /app/
RUN mkdir -p /app/logs/
ADD target/household-0.0.1-SNAPSHOT.jar /app/app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Dspring.profiles.active=container", "-jar", "/app/app.jar"]
**docker-compose.yml:**
version: '3.2'
services:
postgres:
image: postgres:latest
network_mode: bridge
container_name: postgres
volumes:
- postgres-data:/var/lib/postgresql/data
expose:
- 5432
ports:
- 5434:5434
environment:
- POSTGRES_PASSWORD=
- POSTGRES_USER=
- POSTGRES_DB=test
restart: unless-stopped
# APP*****
springbootapp:
image: springbootapp:latest
network_mode: bridge
container_name: springbootapp
expose:
- 8080
ports:
- 8080:8080
restart: unless-stopped
depends_on:
- postgres
links:
- postgres
volumes:
postgres-data:
英文:
I'm getting the below error while giving docker-compose up.
springbootapp | org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
springbootapp | at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:280) ~[postgresql-42.2.8.jar!/:42.2.8]
springbootapp | at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.8.jar!/:42.2.8]
springbootapp | at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195) ~[postgresql-42.2.8.jar!/:42.2.8]
springbootapp | at org.postgresql.Driver.makeConnection(Driver.java:458) ~[postgresql-42.2.8.jar!/:42.2.8]
springbootapp | at org.postgresql.Driver.connect(Driver.java:260) ~[postgresql-42.2.8.jar!/:42.2.8]
springbootapp | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.2.jar!/:na]
springbootapp | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:354) ~[HikariCP-3.4.2.jar!/:na]
springbootapp | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:202) ~[HikariCP-3.4.2.jar!/:na]
springbootapp | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:473) [HikariCP-3.4.2.jar!/:na]
springbootapp | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:554) [HikariCP-3.4.2.jar!/:na]
springbootapp | at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) [HikariCP-3.4.2.jar!/:na]
springbootapp | at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) [HikariCP-3.4.2.jar!/:na]
springbootapp | at org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:56) [flyway-core-6.0.8.jar!/:na]
springbootapp | at org.flywaydb.core.internal.jdbc.JdbcConnectionFactory.<init>(JdbcConnectionFactory.java:80) [flyway-core-6.0.8.jar!/:na]
springbootapp | at org.flywaydb.core.Flyway.execute(Flyway.java:438) [flyway-core-6.0.8.jar!/:na]
springbootapp | at org.flywaydb.core.Flyway.migrate(Flyway.java:149) [flyway-core-6.0.8.jar!/:na]
springbootapp | at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:65) [spring-boot-autoconfigure-2.2.6.RELEASE.jar!/:2.2.6.RELEASE]
Dockerfile:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
EXPOSE 8082
RUN mkdir -p /app/
RUN mkdir -p /app/logs/
ADD target/household-0.0.1-SNAPSHOT.jar /app/app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Dspring.profiles.active=container", "-jar", "/app/app.jar"]
docker-compose.yml:
version: '3.2'
services:
postgres:
image: postgres:latest
network_mode: bridge
container_name: postgres
volumes:
- postgres-data:/var/lib/postgresql/data
expose:
- 5432
ports:
- 5434:5434
environment:
- POSTGRES_PASSWORD=
- POSTGRES_USER=
- POSTGRES_DB=test
restart: unless-stopped
# APP*****
springbootapp:
image: springbootapp:latest
network_mode: bridge
container_name: springbootapp
expose:
- 8080
ports:
- 8080:8080
restart: unless-stopped
depends_on:
- postgres
links:
- postgres
volumes:
postgres-data:
答案1
得分: 2
错误绝对是由于 PostgreSQL 未能正确运行或应用程序无法与 PostgreSQL 通信所致。请使用以下 Docker Compose 文件并进行检查:
version: '2'
services:
postgresql:
image: postgres:10.4
volumes:
- ./postgresql/:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=someuser
- POSTGRES_PASSWORD=
ports:
- 5432:5432
您始终可以使用以下命令检查数据库是否接受连接:
psql -h [主机IP] -U [数据库用户] -d [数据库名称]
英文:
The error is definitely due to postgres is not running correctly or the app is not able to communicate to postgres. Use below docker-compose file and check.
version: '2'
services:
postgresql:
image: postgres:10.4
volumes:
- ./postgresql/:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=someuser
- POSTGRES_PASSWORD=
ports:
- 5432:5432
You can always use following command to check if db is accepting connection.
psql -h [HostIP] -U [db_user] -d [db_name]
答案2
得分: 0
我遇到了同样的问题。解决方法很简单。
- 首先打开 Docker 应用,
在终端进程中;
-
然后你需要进入存放
docker-compose.yml
文件的目录。 -
接着输入
docker-compose up
。 -
接下来,打开 pgAdmin 应用并选择与你的 Spring 项目相关的数据库,
-
然后,输入在
docker-compose.yml
中找到的用户名和密码
(用户名:image,密码:POSTGRES_PASSWORD)。 -
最后运行 Spring Boot 项目。
英文:
I had same problem. the solution is simple.
- first open docker app,
in Terminal process;
-
then you have to go to the directory where the
docker-compose.yml
file is located. -
then write
docker-compose up
-
next, open pgAdmin app and choose db to your spring project,
-
next, enter the password and username found in
docker-compose.yml
(username: image, password: POSTGRES_PASSWORD), -
finally run the spring boot project.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论