Error: connect ECONNREFUSED 127.0.0.1:5432

docker-compose.yml
version: "3.8"

services:
  database:
    image: postgres
    container_name: database-ignite
    restart: always
    ports:
      - 5432:5432
    environment:
      - POSTGRES_USER=SYSDBA
      - POSTGRES_PASSWORD=masterkey
      - POSTGRES_DB=rentx
    volumes:
      - pgdata:/data/postgres

  app:
    build: .
    container_name: rentx
    ports:
      - 3333:3333
    volumes:
      - .:/usr/app
    depends_on:
      - database

volumes:
  pgdata:
    driver: local

error:
when I run “docker logs rentx -f” it returns this error

rentalx@1.0.0 start
ts-node-dev --inspect --transpile-only --ignore-watch node_modules --respawn src/server.ts

[INFO] 14:53:59 ts-node-dev ver. 2.0.0 (using ts-node ver. 10.8.1, typescript ver. 4.7.3)
Debugger listening on ws://127.0.0.1:9229/a142edaa-8170-49f8-a9a6-ad9bc74bb543
For help, see: Debugging - Getting Started | Node.js
database is running!
Server is running!
Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1237:16)
[ERROR] 14:54:24 Error: connect ECONNREFUSED 127.0.0.1:5432

How I can fix it ?

Please next time paste your compose files wrapped in a preformated text (</> symbol) block .

Just configure your database connection to dabase:5432 instead of 127.0.0.1:5432 in your app container.

I’m having the exact same problem, did you solve it?

If it’s the exact same problem, did you read and try Metin’s solution?

I have the same problem
Caused by: 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.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:303) ~[postgresql-42.2.18.jar:42.2.18]
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51) ~[postgresql-42.2.18.jar:42.2.18]
at org.postgresql.jdbc.PgConnection.(PgConnection.java:225) ~[postgresql-42.2.18.jar:42.2.18]
at org.postgresql.Driver.makeConnection(Driver.java:465) ~[postgresql-42.2.18.jar:42.2.18]
at org.postgresql.Driver.connect(Driver.java:264) ~[postgresql-42.2.18.jar:42.2.18]
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.HikariPool.(HikariPool.java:115) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.4.5.jar:na]

The same answer to the same problem:

1 Like

Hi,

I try to put 127.0.0.1:5432 but nothing

See my code:

applications.properties

spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/reactdb
spring.datasource.username=postgres
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true

i run this command:

-Build Local Docker Image using Jib
mvnw jib:dockerBuild -Djib.to.image=fullstack:v1

To run container

  • docker run --name fullstack -p 8080:8080 fullstack:v1

org.postgresql.util.PSQLException: Connection to 127.0.0.1:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

May I ask why you replace localhost with the ip that localhost resolves to instead of using the service name like suggested?

Please, if something in a response is unclear to you, try phrasing in your own words what you understood it says, so we can try to clearify missunderstanding…

Sorry If i undesrtand I replace
spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/reactdb
by this line
spring.datasource.url=jdbc:postgresql://dabase:5432/reactdb

???

Depending on how your service is actualy named in the compose file, it is almost or completly correct.

  • if your service is named “dabase” then its correct.
  • if your service is named “database”, then your environment must be spring.datasource.url=jdbc:postgresql://database:5432/reactdb (of course if reactdb is also correct)

it is fine now Thanks