DB connection error Error: connect ECONNREFUSED 127.0.0.1:5432? - postgresql

Running a docker container with postgres and npm produces this error, while running them separetly
(npm and docker) doesn't. What seems to be the error here?
My docker-compose.yml:
version: "3.5"
services:
db:
image: postgres:12.1
ports:
- 5432:5432
environment:
- FLYWAY_URL=jdbc:postgresql://db:5432/
- POSTGRES_USER=myuser
- POSTGRES_PASSWORD=mypass
- POSTGRES_DB=mydb
volumes:
- ./db/data:/var/lib/postgresql/data
migrate:
image: boxfuse/flyway
entrypoint: ["sh", "-c", "/flyway/wait-for.sh db:5432 -- flyway migrate"]
depends_on:
- db
volumes:
- ./common/migrations/:/flyway/sql:rw
- ./common/scripts/wait-for.sh:/flyway/wait-for.sh:rw
environment:
# - FLYWAY_LOCATIONS=classpath:/common/migrations/
- FLYWAY_PASSWORD=mypass
- FLYWAY_USER=myuser
- FLYWAY_URL=jdbc:postgresql://db:5432/mydb?user=myuser&password=mypass
- FLYWAY_CONNECT_RETRIES=30
networks:
default:
name: mydb-local
services:
example-service:
build: .
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- 3000:3000
- 9229:9229
command: npm start
Thing I have checked:
postgresql.conf & pg_hba.conf accepts connections.
DB credentials are correct.
Db is running

Related

Password auth failed for user "postgres"

I am trying to run my postgres server and nestjs project with docker script and it does fire up the server and database. While firing up it does run migrations too but when I open pgAdmin I see no database there and if i try to create new server i get fatal pasword incorrect error. Also my server crashes too with error saying Password authentication failed for user "postgres". It was running fine yesterday but today its not running at all. I tried pruning everything and made fresh build and then compose up but nothing. Here is docker script
version: "3.5"
services:
dev-api:
container_name: xxxxxxxx-api
build:
context: .
dockerfile: Dockerfile
depends_on:
- dev-db
environment:
DATABASE_URL: postgresql://postgres:postgres#dev-db:5432/xxxxxxx_api
APP_ENV: development
PORT: 3030
WAIT_HOSTS: dev-db:5432
ports:
- "3030:3030"
- "9229:9229"
volumes:
- .:/usr/api/
dev-db:
container_name: xxxxxxxx-postgres
image: postgres:13.5-alpine
restart: always
ports:
- "5432:5432"
volumes:
- ./pg-data:/var/lib/postgresql/data
- ./src/db/docker/init.sql:/docker-entrypoint-initdb.d/dbinit.sql
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=xxxxxxx_api
expose:
- "5432"
pgadmin:
container_name: xxxxxxx-pgadmin
image: dpage/pgadmin4:6.2
ports:
- 8080:80
volumes:
- pgadmin-data:/var/lib/pgadmin
environment:
- PGADMIN_DEFAULT_EMAIL=user#postgres.com
- PGADMIN_DEFAULT_PASSWORD=postgres
- PGADMIN_LISTEN_PORT=80
depends_on:
- dev-db
volumes:
pgadmin-data:

How to connect postgresql with PGAdmin in docker-compse.yml file

This is my docker-compose.yml file:
version: "3.9"
services:
db:
image: postgres
volumes:
- ./data/db:/var/lib/postgresql/data
- ./innovators.sql:/docker-entrypoint-initdb.d/innovators.sql
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
pgadmin:
image: dpage/pgadmin4:4.18
restart: unless-stopped
environment:
- PGADMIN_DEFAULT_EMAIL=admin#domain.com
- PGADMIN_DEFAULT_PASSWORD=admin
- PGADMIN_LISTEN_PORT=80
ports:
- "8090:80"
volumes:
- ./pgadmin-data:/var/lib/pgadmin
links:
- "db:pgsql-server"
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
volumes:
pgadmin-data:
In postgreql I import my own table (./innovators.sql:/docker-entrypoint-initdb.d/innovators.sql).
What should I do to connect my postgresql database with my pgAdmin?
I wish the end result would be that I can see my tables which I imported in pgadmin.
Access pgadmin in the browser on your host on localhost:8090. Sign in and then navigate to Servers->Create->Server, in the connection tab use db or pgsql-server as "Host name/address" and 5423 as a port.

Permission denied when docker-compose exec -T database pg_dump -U teslamate teslamate > /backuplocation/teslamate.bck

When I execute this command I receive a PERMISSION DENIED
docker-compose exec -T database pg_dump -U teslamate teslamate > /backuplocation/teslamate.bck
What is going wrong?
below the docker-compose.yml
I am trying to follow the instructions to make a backup of the database
below the docker compose
version: "3"
services:
teslamate:
image: teslamate/teslamate:latest
restart: always
environment:
- DATABASE_USER=teslamate
- DATABASE_PASS=secret
- DATABASE_NAME=teslamate
- DATABASE_HOST=database
- MQTT_HOST=mosquitto
ports:
- 4000:4000
volumes:
- ./import:/opt/app/import
cap_drop:
- all
database:
image: postgres:13
restart: always
environment:
- POSTGRES_USER=teslamate
- POSTGRES_PASSWORD=secret
- POSTGRES_DB=teslamate
volumes:
- teslamate-db:/var/lib/postgresql/data
grafana:
image: teslamate/grafana:latest
restart: always
environment:
- DATABASE_USER=teslamate
- DATABASE_PASS=secret
- DATABASE_NAME=teslamate
- DATABASE_HOST=database
ports:
- 3000:3000
volumes:
- teslamate-grafana-data:/var/lib/grafana
mosquitto:
image: eclipse-mosquitto:1.6
restart: always
ports:
- 1883:1883
volumes:
- mosquitto-conf:/mosquitto/config
- mosquitto-data:/mosquitto/data
volumes:
teslamate-db:
teslamate-grafana-data:
mosquitto-conf:
mosquitto-data:

Docker file wait for postgres container

Hello I have the following error in my node project:
(node:51) UnhandledPromiseRejectionWarning: Error: getaddrinfo
ENOTFOUND ${DB_HOST}
I'm thinking the problem is that my postgress is not yet started when my project starts
and so I'm not able to think of a solution on how to start my container after my postgres is ready, I read something about dockerize, but I'm not able to imagine how to apply
my docker file:
FROM node:lts-alpine
RUN mkdir -p /home/node/api/node_modules && chown -R node:node /home/node/api
WORKDIR /home/node/api
COPY ormconfig.json .env package.json yarn.* ./
USER node
RUN yarn
COPY --chown=node:node . .
EXPOSE 4000
CMD ["yarn", "dev"]
my docker compose:
version: '3.7'
services:
ci-api:
build: .
container_name: ci-api
volumes:
- .:/home/node/api
- /home/node/api/node_modules
ports:
- '${SERVER_PORT}:${SERVER_PORT}'
depends_on:
- ci-postgres
networks:
- ci-network
ci-postgres:
image: postgres:12
container_name: ci-postgres
ports:
- '${DB_PORT}:5432'
environment:
- ALLOW_EMPTY_PASSWORD=no
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASS}
- POSTGRES_DB=${DB_NAME}
volumes:
- ci-postgres-data:/data
networks:
- ci-network
volumes:
ci-postgres-data:
networks:
ci-network:
driver: bridge
and this is my .env
SERVER_PORT=4000
DB_HOST=ci-postgres
DB_PORT=5432
DB_USER=spirit
DB_PASS=api
DB_NAME=emasa_ci
You can reference the below docker-compose.yml in which depends_on, healthcheck and links are added as web service depends on db service.
Reference:
Postgresql Container is not running in docker-compose file - Why is this?
version: "3"
services:
webapp:
build: .
container_name: webapp
ports:
- "5000:5000"
links:
- postgres
depends_on:
postgres:
condition: service_healthy
postgres:
image: postgres:11-alpine
container_name: postgres
ports:
- "5432:5432"
environment:
- POSTGRES_DB=tmp
- POSTGRES_USER=tmp
- POSTGRES_PASSWORD=tmp_password
volumes: # Persist the db data
- database-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5
volumes:
database-data:

Docker connection refused between containers

I'm using Postgres, Redis and Node.js (adding dependencies with yarn), and trying to integrate it all with a docker-compose.yml file.
I have the following docker-compose.yml:
version: '3'
services:
postgres:
image: postgres:latest
restart: unless-stopped
environment:
- POSTGRES_DB=mybase
- POSTGRES_USER=myuser
- POSTGRES_PASSWORD=mypass
redis:
image: redis:latest
restart: unless-stopped
migrate:
build: .
entrypoint: node_modules/.bin/sequelize db:migrate --config src/config/database.js --migrations-path src/database/migrations/
volumes:
- ./:/app
- /app/node_modules
depends_on:
- postgres
wavetech-be:
build:
dockerfile: Dockerfile
context: .
restart: on-failure
volumes:
- /app/node_modules
- ./:/app
environment:
- REDIS_HOST=redis
- REDIS_PORT=6379
- DB_HOST=postgres
- DB_USER=myuser
- DB_PASS=mypass
- DB_PORT=5432
- DB_NAME=mybase
depends_on:
- redis
- migrate
And the following Dockerfile:
FROM node:alpine
WORKDIR "/app"
COPY ./package.json ./
RUN apk add yarn
RUN yarn
COPY . .
CMD [ "yarn", "dev" ]
However, when I docker-compose up, I keep getting connection problems with both databases:
migrate_1 |
migrate_1 | ERROR: connect ECONNREFUSED 127.0.0.1:5432
migrate_1 |
...
wavetech-be_1 | (node:85) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6379
The answer has two parts:
First, as pointed out by #jonrsharpe, the description of the migrate service was lacking the environment variables. So, as it is with the volumes, each service needs its own environment variables configured.
migrate:
build: .
entrypoint: node_modules/.bin/sequelize db:migrate --config src/config/database.js --migrations-path src/database/migrations/
volumes:
- ./:/app
- /app/node_modules
environment:
- DB_HOST=postgres
- DB_USER=myuser
- DB_PASS=mypass
- DB_PORT=5432
- DB_NAME=mybase
- APP_PORT=3000
depends_on:
- postgres
Second, I am using Bull to manage my Redis server. I was importing a config and passing it directly to Redis, so:
import redisConfig from '../../config/redis';
...
init() {
this.queues = Object.values(jobs).map(job => ({
bull: new BullQueue(job.key, redisConfig),
name: job.key,
handle: job.handle,
}));
}
And it turns out that Bull was trying to just use the default Redis configuration. When I passed the environment variables directly into the Bull config, it worked properly:
init() {
this.queues = Object.values(jobs).map(job => ({
bull: new BullQueue(job.key, {
redis: {
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT,
},
}),
name: job.key,
handle: job.handle,
}));
}