简体   繁体   中英

Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections

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:

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]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM