Unable to connect to Postgres within docker [duplicate] - postgresql

This question already has answers here:
ECONNREFUSED for Postgres on nodeJS with dockers
(7 answers)
Closed 2 months ago.
So I'm trying to dockerize my Postgres-Express-React-Node Application the docker-compose for the application is
version: '3.8'
services:
postgres:
image: postgres:12.1
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: PGAdminAth
backend:
build: ./backend
container_name: backend_c
volumes:
- /app/node_modules
- ./backend:/app
ports:
- "4000:4000"
frontend:
build: ./frontend
container_name: frontend_c
ports:
- "3000:3000"
stdin_open: true
tty: true
but every time I run docker-compose up, I get
Error: connect ECONNREFUSED 127.0.0.1:5432
backend_c | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1195:16) {
backend_c | errno: -111,
backend_c | code: 'ECONNREFUSED',
backend_c | syscall: 'connect',
backend_c | address: '127.0.0.1',
backend_c | }
as an error.
The piece of code that connects to my postgres database from nodejs app is
const { Client } = require("pg");
const client = new Client({
host: "localhost",
port: 5432,
user: "postgres",
password: "PGAdminAth",
database: "postgres",
});
client.connect();

Docker-compose configures a virtual network where all the containers from compose file live. This:
postgres: # <--- This
image: postgres:12.1
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: PGAdminAth
means that any service within the virtual network will be able to reach your database by hostname postgres.
So your client code have to use service names when perform network communication to the container.
Hence your client setup would be
const client = new Client({
host: "postgres",
port: 5432,
user: "postgres",
password: "PGAdminAth",
database: "postgres",
});

Use depends_on in the backend service. (This started the Database before the backend service.)
depends_on:
- postgres
And change this
host: "localhost",
to this
host: "postgres",
Inside the application, containers refer to their service name. So use the service name that you gave when defining Database. Examples of service names postgres, backend, and frontend.
To store Database data permanently, you must map a volume to it. Read this

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.

How do I connect to host.docker.internal postgres instance?

I'm trying to connect to my local server. I have the following docker-compose.yaml and servers.json files. I think I'm making a mistake with my .pgpass. When the containers are up and running I can login to pgAdmin, and I can see the docker_postgres_group "group", but when I try to login I get the authentication error in the first screenshot below. Further below is how I've got my pgpass set up. I think this is where the problem is, but I could be wrong...
docker-compose.yaml
version: '3.8'
services:
db:
container_name: pg_container
image: postgres
restart: always
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: test_db
ports:
- "5432:5432"
pgadmin:
container_name: pgadmin4_container
image: dpage/pgadmin4
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: admin#admin.com
PGADMIN_DEFAULT_PASSWORD: root
ports:
- "5050:80"
volumes:
- ./servers.json:/pgadmin4/servers.json # preconfigured servers/connections
- ./pgpass:/pgpass # passwords for the connections in this file
servers.json
{
"Servers": {
"1": {
"Name": "docker_postgres",
"Group": "docker_postgres_group",
"Host": "host.docker.internal",
"Port": 15432,
"MaintenanceDB": "postgres",
"Username": "postgres",
"PassFile": "/pgpass",
"SSLMode": "prefer"
}
}
}
.pgpass
This is what's inside my pgpass file: host.docker.internal:15432:postgres:postgres:postgres
Here's its location:
I've tried having the .pgpass file in the pgpass folder and outside.
And - I'm using postgres as the password to try to login to pgadmin.
When spinning up containers through docker-compose, also a network is created where each service can see the other services of the docker-compose definition. They are also resolvable by there servicename. In your case you can replace host.docker.internal with db and use the internal port 5432.
EDIT: host.docker.internal resolves the internal IP address used by your host machine. If you want connect through the host IP, the you have either to change the port in servers.json from 15432 to 5432 or the port mapping from the db service:
ports:
- "15432:5432"

How can I connect to docker Postgres server from PGAdmin4

I am new to Docker containers but not quite new to Postgres (I delved a bit in the past).
The thing is I am trying to connect to a Postgres server started from the following Docker container dpage/pgadmin4
The issue is that I cannot connect to the server using PGAdmin4.
I am getting the following error:
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Address not available
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
I have the following yaml file:
version: '3'
services:
db:
image: postgres
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: parola
PGDATA: /var/lib/postgresql/data/pgdata
pgadmin4:
image: dpage/pgadmin4
ports:
- "5050:80"
environment:
PGADMIN_DEFAULT_EMAIL: user#domain.com
PGADMIN_DEFAULT_PASSWORD: parola
Using the psql command line I can connect to it only if I set -h 0.0.0.0, as follows:
psql -h 0.0.0.0 -U postgres
Can someone tell me what I need to set up in the yaml file or how I can configure so it allow connection from localhost/127.0.0.1?
I am running on Ubuntu 20.04.2, AMD64 with Docker version 20.10.7, build f0df350.
service pgadmin4 is not being able reach service db because inside pgadmin4 at port 5432 there wont be any service running
With this configuration, you are only mapping the db to host machine's 5432 port. That is why you were able to run psql -h 0.0.0.0 -U postgres in your machine (host machine)
According to docker-compose pgadmin4 can access DB using host name db
you can link the configuration with some additional configuration as below
version: '3'
services:
db:
image: postgres
restart: always
ports:
- "5432"
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: parola
PGDATA: /var/lib/postgresql/data/pgdata
pgadmin4:
image: dpage/pgadmin4
ports:
- "5050:80"
environment:
PGADMIN_DEFAULT_EMAIL: user#domain.com
PGADMIN_DEFAULT_PASSWORD: parola
volumes:
- ./servers.json:/pgadmin4/servers.json
add a file called servers.json in the same directory as docker-compose with the below contents. take note that I have added the Host as db
{
"Servers": {
"1": {
"Name": "local db",
"Group": "Server Group 1",
"Port": 5432,
"Username": "postgres",
"Host": "db",
"SSLMode": "prefer",
"MaintenanceDB": "postgres"
}
}
}

How configure docker / pgadmin with nest

I'm trying to configure my docker to pgadmin4 in my nest js project, to do the backend, but when i try connect, show this message "Unable to connect to server:
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting"
I already tried configure the docker-compose.yml with pgadmin configurations, network configurations and
my docker-compose.yml
version: '3'
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: "12345678"
ports:
- "5432:5432"
volumes:
- ./data/pgadmin/
networks:
- pg-network
pgadmin:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: "monnerat#monnerat.com.br"
PGADMIN_DEFAULT_PASSWORD: "abc"
ports:
- "5050:80"
depends_on:
- db
networks:
- pg-network
networks:
pg-network:
driver: bridge
My actual result is connection refused is server running on host "localhost (127.0.0.1) and accepting"
but i expect he open the pgadmin to create the database
Your docker-compose.yml is good. Please see the attached host name should be 'db'

Connection Error accessing postgres docker container

I setup a prisma project recently and here is my docker-compose.yml file
version: '3'
services:
prisma:
image: prismagraphql/prisma:1.31
restart: always
ports:
- '4030:4466'
environment:
TZ: ${PRISMA_DB_TIME_ZONE}
PRISMA_CONFIG: |
port: 4466
# managementApiSecret: my-secret
databases:
default:
connector: postgres
host: postgres
port: 5432
user: prisma
password: ${PRISMA_DB_PASSWORD}
migrations: true
rawAccess: true
postgres:
image: postgres:10.3
restart: always
ports:
- "3306:3306"
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: ${PRISMA_DB_PASSWORD}
TZ: ${PRISMA_DB_TIME_ZONE}
volumes:
- postgres:/var/lib/postgresql/data
volumes:
prisma:
postgres:
I can open my prisma playground and it functions without any issue. but I can't create a direct connection to the postgre container with dbeaver.
dbeaver Error message
Connection reset
Why my connection to the database fails?
This photo will be helpful.
postgres by default listens on port 5432.
In your postgres container specification, you should expose the port 5432 not 3306.
version: '3'
services:
prisma:
image: prismagraphql/prisma:1.31
restart: always
ports:
- '4030:4466'
environment:
TZ: ${PRISMA_DB_TIME_ZONE}
PRISMA_CONFIG: |
port: 4466
# managementApiSecret: my-secret
databases:
default:
connector: postgres
host: postgres
port: 5432
user: prisma
password: ${PRISMA_DB_PASSWORD}
migrations: true
rawAccess: true
postgres:
image: postgres:10.3
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: ${PRISMA_DB_PASSWORD}
TZ: ${PRISMA_DB_TIME_ZONE}
volumes:
- postgres:/var/lib/postgresql/data
volumes:
prisma:
postgres:
If 5432 port in your host is already in use and if you want to use 3306 instead , then you can do port forwarding like below:
postgres:
image: postgres:10.3
restart: always
ports:
- "3306:5432"
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: ${PRISMA_DB_PASSWORD}
TZ: ${PRISMA_DB_TIME_ZONE}
volumes:
- postgres:/var/lib/postgresql/data
UPDATE - 1
Reason why prisma can access postgres
ports section is meant only to make our services accessible in host level. But in the container level, If a port is open in a container, any other running container can access that port with the help of networks or links section.
By default, docker-compose will create a network per docker-compose.yml file and joins all the services in that file into that network.
That's the reason we could use the <service name> as the hostname and compose will resolve that name to the ip-address of the respective (in your case postgres) container.