Unable to connect to the database (development). getaddrinfo ENOTFOUND postgres - postgresql

I'm trying to connect to a postgres database with a NestJs app. This is my docker-compose file:
postgres:
image: postgres:alpine
ports:
- '5432:5432'
networks:
- network
# volumes:
# - 'postgres_data:/var/lib/postgresql/data'
environment:
POSTGRES_DB: database
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U postgres']
interval: 10s
timeout: 5s
retries: 5
This is my .env file:
TYPEORM_NAME=development
TYPEORM_TYPE=postgres
TYPEORM_HOST=postgres
TYPEORM_PORT=5432
TYPEORM_CACHE=true
TYPEORM_LOGGING=all
TYPEORM_DATABASE=database
TYPEORM_USERNAME=postgres
TYPEORM_PASSWORD=postgres
TYPEORM_DROP_SCHEMA=false
TYPEORM_SYNCHRONIZE=true
TYPEORM_MIGRATIONS_RUN=true
I run: docker exec printenv and I get these:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=ea6383b9f55f
POSTGRES_PASSWORD=postgres
POSTGRES_DB=database
POSTGRES_USER=postgres
LANG=en_US.utf8
PG_MAJOR=15
PG_VERSION=15.1
When I run npm run start I get this error message:
Unable to connect to the database (development). Retrying (1)...
Error: getaddrinfo ENOTFOUND postgres
at GetAddrInfoReqWrap.onlookup [as oncomplete]
What am I doing wrong?

When you run a program on your host, it can't use the hostnames available on the docker network. It has to use 'localhost' and the mapped port(s) of the container.
So
TYPEORM_HOST=postgres
needs to be
TYPEORM_HOST=localhost
Since you've mapped the container port 5432 to the host port 5432, you don't need to change that.

Related

Error: connect ECONNREFUSED 127.0.0.1:5432 -> error is showing when for docker-compose up for node+postgres application

I am new to docker.
My docker-compose file:
version: '2.2'
services:
db:
image: postgres:10
ports:
- "5430:5431"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
api:
build: .
environment:
DB_USERNAME: postgres
DB_PASSWORD: postgres
DB_NAME: TestDB6
DB_HOSTNAME: db
ports:
- 8081:8081
what changes can be made to resolve the issue?
Error: connect ECONNREFUSED 127.0.0.1:5432
Checked if there was any processes running on port 5432, there were none.
It's best practice that when you encounter an error, you should share the error output along with the configuration that caused it. If I had to guess since there's no error, in the db definition you put
ports:
- "5430:5431"
And usually, the default port for postgres is 5432. So you're exposing a port that postgres isn't actually using. Best solution would be to update the ports mapping to
ports:
- "5430:5432"
You also could try to configure postgres to run on 5431 instead of 5432, but that's probably unnecessary.

Docker and Postgres - Cannot connect to container

I am attempting to start PostgreSQL in a docker container, but cannot connect from my terminal.
After running docker-compose up -d using the following docker-compose:
docker-compose.yaml
version: '3.8'
services:
db:
image: postgres:14.5
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
logging:
options:
max-size: 10m
max-file: "3"
ports:
- '5416:5432'
volumes:
- ./api/db/postgres-test-data:/var/lib/postgresql/data
Running docker ps gives me:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8611fc5a9a73 postgres:14.5 "docker-entrypoint.s…" 21 seconds ago Up 12 seconds 0.0.0.0:5416->5432/tcp, :::5416->5432/tcp api_db_1
But when I try to connect with psql -h localhost -p 5416 -U postgres, I get:
psql: error: connection to server at "localhost" (::1), port 5416 failed: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
How can I fix this?
It was because I had my VPN running. Disabling it fixed the issue.

Docker compose getting connect ECONNREFUSED 127.0.0.1:3333 with AdonisJS, Postgres

i am trying to setup an application with docker/adonis/postgres/mongo,
but I am getting an ECONNREFUSED error
Error: connect ECONNREFUSED 127.0.0.1:3333
here is my dockerfile
FROM node
WORKDIR /usr/app
COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
RUN npm i -g #adonisjs/cli && npm install
COPY . .
EXPOSE 3000
CMD ["node", "ace", "serve"]
dockercompose
version: '3.4'
services:
app:
build: .
ports:
- 3000:3000
volumes:
- .:/usr/app
db:
image: postgres
environment:
POSTGRES_PASSWORD: ${PG_PASSWORD}
POSTGRES_USER: ${PG_USER}
POSTGRES_DB: ${PG_DB_NAME}
ports:
- 5432:5432
mongo:
image: mongo
environment:
MONGO_PASSWORD: 1234
MONGO_USER: MONGO
ports:
- 27017:27017
Your error message mentions port 3333. But nowhere in your docker-compose file is a port 3333 exposed to the Host.
Where do you try to connect from?
If from app container, then services like 'db' and 'mongo' are not on localhost. Looking from inside 'app' service, the db service is at db:5432 and the mongo service is at mongo:27017.
If from the host, then db is on localhost:5432 and mongo is at localhost:27017.
Is becomes more clear is you use other ports on the host than internally.

psql: error: could not connect to server: FATAL: role "postgres" does not exist

I have "dockerized" a Django/PostgreSQL app and try to connect to my database
I have 2 containers: web et db
It works but I can't connect to my postgresql database
I used to ran docker exec -it coverage_africa_db_1 psql -U postgres but I got an error
psql: error: could not connect to server: FATAL: role "postgres" does not exist
I try to 'jump' into my container by running the command docker exec -it aab213f730cd bash and try to connect using psql command...
psql -d db_dev
psql: error: could not connect to server: FATAL: role "root" does not exist
or
psql -U postgres
error: could not connect to server: FATAL: role "postgres" does not exist
in fact, none of psql options works...
.env.dev
SECRET_KEY=*************************************
DEBUG=1
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=db_dev
SQL_USER=user
SQL_PASSWORD=user
SQL_HOST=db
SQL_PORT=5432
DATABASE=postgres
DJANGO_SETTINGS_MODULE=core.settings.dev
docker-compose.yml
version: '3.7'
services:
web:
build: ./app
restart: always
command: python manage.py runserver 0.0.0.0:8000
volumes:
- ./app/:/usr/src/app
ports:
- 8000:8000
env_file:
- ./.env.dev
depends_on:
- db
db:
image: postgres:12.0-alpine
restart: always
volumes:
- postgres_data:/var/lib/postgres/data/
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=user
- POSTGRES_DB=db_dev
volumes:
postgres_data:
With postgres container, this:
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=user
- POSTGRES_DB=db_dev
defines how the database is initialized. If you didn't change it, you should be able to connect as user 'user' with password 'user'.
If you did change it, then the actual values are those which were present at the first launch. After first launch those credentials are written into the database, which data is on postgres_data volume. If you want to delete the data and reinitialize database with new credentials, use docker-compose down -v.

How to initialize postgres db with flyway in docker?

I have django app that I am attempting to host in docker. I have been unsuccessful in launching my postgres server before standing up the django app. Here is my docker-compose.yaml
version: '3'
services:
flyway:
image: boxfuse/flyway
command: -url=jdbc:postgresql://db/dbname -schemas=schemaName -user=user -password=pwd migrate
volumes:
- ./flyway:/flyway/sql
depends_on:
- db
db:
image: postgres:9.6
restart: always
ports:
- 5432:5432
environment:
- POSTGRES_PASSWORD=pwd
healthcheck:
test: "pg_isready -q -U postgres"
app:
image: myimage
ports:
- 8000:8000
Services db and app both seem to stand up fine but I am unable to spin up the postgres defaults with flyway. Here are the errors that I'm getting:
flyway_1 | SEVERE: Connection error:
flyway_1 | org.postgresql.util.PSQLException: Connection to db:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
ERROR:
flyway_1 | Unable to obtain connection from database (jdbc:postgresql://db/dbname) for user 'user': Connection to db:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
I couldn't find a good example on how to use flyway with Postgres. How do I go about getting this to work? TIA
Version '3+' of the docker-compose file doesn't support parameter condition in the depends_on block, but version '2.1+' does. So you can create compose file like the following, that uses healthcheck from the postgres section, for example:
version: '2.1'
services:
my-app:
# ...
# ...
depends_on:
- flyway
flyway:
image: boxfuse/flyway:5-alpine
command: -url=jdbc:postgresql://postgres:5432/mydb -schemas=public -user=postgres -password=postgres migrate
volumes:
- ./migration:/flyway/sql
depends_on:
postgres:
condition: service_healthy
postgres:
domainname: postgres
build: ./migration
ports:
- "5432:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
healthcheck:
test: ["CMD", "pg_isready", "-q", "-U", "postgres"]
interval: 5s
timeout: 1s
retries: 2
depends_on of the flyway service does not actually check that the database within db-container is up and running, but instead only checks that the container is up. This is quite different. The container could be up and running at the moment the database within it is starting but not yet accepting connections.
For such a case, you should specify a health check to make sure your database is accepting connections. You can even find an example how to do it with PostgreSQL in the official docker-compose docs.
Please use -connectRetries to wait for postgres, example (wait for 60s): -connectRetries=60
More details here
https://github.com/flyway/flyway-docker