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 ?

1 Like

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:

2 Likes

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

I am facing the same issue
here is my docker compose

version: "3.8"

networks:
  weather-network:
    driver: bridge
    external: true

volumes:
  weather-data:
    name: weather-data

services:
  postgres:
    image: postgres:14-alpine
    container_name: postgres-container
    ports:
      - "5432:5432"
    networks:
      - weather-network
    environment:
      POSTGRES_USER: postgres
      POSTGRES_DB: weather_db
      POSTGRES_PASSWORD: password
    expose:
      - 5432
  
  redis:
    image: redis
    container_name: redis-container
    ports:
      - "6379:6379"
    networks:
      - weather-network
  
  web:
    image: weather-app
    ports:
      - '9200:9200'
    networks:
      - weather-network
    depends_on:
      - redis
      - postgres
  

I have tried using the proposed solution but it is not working
here is my app env file:

# Database details
DB_HOST=postgres
DB_PORT=5432
DB_NAME="weather_db"
DB_USER="postgres"
DB_PASSWORD="password"

ERROR:

web-1 |     raise OSError(err, f'Connect call failed {address}')
web-1 | ConnectionRefusedError: [Errno 111] Connect call failed ('172.21.0.2', 5432)
web-1 | ERROR:    [Errno 111] Connect call failed ('172.21.0.2', 5432)

Got the issue,
issue was that postgres was not ready to listen before that app was trying to connect to the DB.

updated docker-compose:

version: "3.8"

networks:
  weather-network:
    driver: bridge
    external: true

volumes:
  weather-data:
    name: weather-data

services:
  postgres:
    image: postgres:14-alpine
    container_name: postgres-container
    ports:
      - "5432:5432"
    networks:
      - weather-network
    environment:
      POSTGRES_USER: postgres
      POSTGRES_DB: weather_db
      POSTGRES_PASSWORD: password
    expose:
      - 5432
  
  redis:
    image: redis
    container_name: redis-container
    ports:
      - "6379:6379"
    networks:
      - weather-network
  
  web:
    image: weather-app
    ports:
      - '9200:9200'
    networks:
      - weather-network
    depends_on:
      - redis
      - postgres
    restart: always

always restart your app, this way it will try to connect to the DB again.

Hello everyone, please I am having the same issue and have also tried all the proposed solution. I am trying to connect a Nestjs app running in a docker container to a postgre database running in another docker container. Thanks in advance. Below is my docker-compose.yml

version: '3.8'

services:
  nginx:
    image: nginx:latest
    ports:
      - '8080:80'
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro

  db:
    container_name: lagbaja-db
    image: postgres:14.7 # match with the latest PostgreSQL version supported by AWS Aurora
    restart: always
    ports:
      - '5433:5432' # specify port forwarding
    environment:
      - POSTGRES_USER=lagbaja
      - POSTGRES_PASSWORD=la2024
      - POSTGRES_DB=lagbaja
    networks:
      - docker-network
    # volumes:
    #   - pgdata:/data/postgres

  dev:
    image: lagbaja-api-dev:1.0.0
    build:
      context: .
      target: development
      dockerfile: ./Dockerfile
    command: npm run start:debug
    ports:
      - '3000'
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    restart: always
    environment:            # Add environment variables for the app
      - DATABASE_HOST=db 
    depends_on:
      - db
      - redis
    networks:
      - docker-network

  redis:
    image: 'redis:latest'
    ports:
      - '6379'
    restart: unless-stopped
    networks:
      - docker-network

networks:
  docker-network:
    driver: bridge

Instead of saying, you tried all proposed solutions, please, repeat what you actually did and how you try to connect from the nodejs app to get the error. Also what error message exactly. It would be hard to tell what you do wrong when we donā€™t know what you do and you donā€™t share the connection string.

Sorry for the mis-communication, I have tried all the solutions that were offered in the thread.

I first tried connecting to the database using localhost as my host in the connection string like so

TypeOrmModule.forRoot({
      type: 'postgres',
      host: 'localhost',
      username: 'lagbaja',
      password: 'la2024',
      database: 'lagbaja',
      port: 5433,
      synchronize: process.env.SYNCHRONIZE_DB === 'true',
      autoLoadEntities: true,
    }),

Iā€™m using TypeOrm to connect to the database, and I got this error

Error: connect ECONNREFUSED 127.0.0.1:5433
api-lagbaja-dev-1    |     at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16)

After much Googling, I found a solution that suggested I create a network between the docker containers which should then allow me use the database service name as the host name, as shown below

TypeOrmModule.forRoot({
      type: 'postgres',
      host: 'db',
      username: 'lagbaja',
      password: 'la2024',
      database: 'lagbaja',
      port: 5433,
      synchronize: process.env.SYNCHRONIZE_DB === 'true',
      autoLoadEntities: true,
    }),

this is the error message

Error: connect ECONNREFUSED 172.22.0.3:5433
api-lagbaja-dev-1    |     at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16)     
api-lagbaja-dev-1    | [Nest] 36  - 03/23/2024, 11:52:59 AM   ERROR [TypeOrmModule] Unable to connect to the database. Retrying (3)...

I noticed that in both cases the IP address changed

But why are you trying to use the published host port, when you use the service name through the container network?

Use the service name and container port 5432, and it will work if the database is started and ready for connections.

This worked for me. Thanks for your help!

Hi @gabsantana

After looking at your docker-compose.yml I would say it looks correct but the problem is that youā€™re using localhost as the database host for referencing the connection from your app container but actually, it should be the database as both the containers are isolated the dependency only be used by the service name

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
      - POSTGRES_HOST=database
      - POSTGRES_PORT=5432
    volumes:
      - pgdata:/data/postgres

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

volumes:
  pgdata:
    driver: local

Here Iā€™ve updated your provided docker-compose.yml and added

      - POSTGRES_HOST=database
      - POSTGRES_PORT=5432

You can directly use them inside your appā€™s database configuration.

OlĆ”, tentem definir o DB_HOST conforme o nome do service, que estĆ” ligado na network docker. Recriem a imagem e tentem executar.

services:
db:
image: mysql:8.0
container_name: db_logs
restart: always
volumes:
- db_logs-db-data:/var/lib/mysql
environment:
DB_HOST: db # Nome do Service
MYSQL_ROOT_PASSWORD: ${DB_USER}
MYSQL_USER: ${DB_USER}
MYSQL_PASSWORD: ${DB_PASS}
MYSQL_DATABASE: ${DB_NAME}