1

I'm reading about it and trying to solve it by myself:

ECONNREFUSED for Postgres on nodeJS with dockers

Unable to connect to Postgres DB due to the authentication type 10 is not supported

Unable to connect to Docker Postgres from outside

I did a lot of steps and I don't achieve a connection from my python script to Postgres Docker:

My docker-compose.yml

version: "3.8"

services:

  postgres:
    image: postgres:14
    restart: always
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=root
      - POSTGRES_PASSWORD=root
      - POSTGRES_DB=project1

  pgadmin:
    image: dpage/pgadmin4
    environment:
      - PGADMIN_DEFAULT_EMAIL=admin@admin.com
      - PGADMIN_DEFAULT_PASSWORD=adminadmin
    ports:
      - "5050:80"
    restart: always
    depends_on:
      - postgres

Then I run

docker-compose up

My script.py:

import psycopg2

conn = psycopg2.connect(
    host="host.docker.internal",
    database="project1",
    user="root",
    password="root",
    port=5432
)

print(conn)

# create a cursor
cur = conn.cursor()

# execute a statement
print('PostgreSQL database version:')
result = cur.execute('SELECT version()')
print(result)

When I try to connect to DB, the console triggers multiple errors if I change host:

host.docker.internal (I think I have to do something in postgresql.conf or pg_hba.conf, but I'm not sure):

conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "host.docker.internal" (192.168.1.145), port 5432 failed: FATAL:  no pg_hba.conf entry for host "192.168.1.145", user "root", database "project1", SSL off

localhost

 conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "localhost" (::1), port 5432 failed: FATAL:  password authentication failed for user "root"

0.0.0.0

conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "0.0.0.0", port 5432 failed: Cannot assign requested address (0x00002741/10049)
        Is the server running on that host and accepting TCP/IP connections?

BTW: Docker runs services correctly and I can connect to PGAdmin without problems.

enter image description here

Any ideas?

Sommer
  • 1,328
  • 2
  • 10
  • 20
  • Every computer is its own localhost, so saying "from localhost" doesn't tell us anything. Where is your python script running? – jjanes Dec 26 '22 at 02:37
  • Hi @jjanes, I'm running the code on my own laptop to test, it's a Windows 11, I'm using Docker Desktop. – Sommer Dec 26 '22 at 16:55
  • So your own laptop here is also the docker host? This works for me on Windows 11 (but not on Linux, because there the host can't refer to itself as "host.docker.internal", only containers can do that). It looks like you have a different database already running or mapped from the docker host, and are trying to connect to that one. – jjanes Dec 26 '22 at 17:32
  • Yes, I'm trying to test my script on my own computer. – Sommer Dec 26 '22 at 18:21

1 Answers1

0

this is docker will be access by localhost as host, not host.docker.internal

conn = psycopg2.connect(
    host="localhost",
    database="project1",
    user="root",
    password="root",
    port=5432
)

In future when you try to host container for your script in same docker-compose file then host will be postgres

conn = psycopg2.connect(
    host="postgres",
    database="project1",
    user="root",
    password="root",
    port=5432
)
  • Hi, thanks for ur answer. I understand I would can set the app in docker file, but in this case I want to access from localhost, I think it's possible. BTW: I tried to set ¨localhost¨ and the error keeping. You can see the error on my question. Thanks! – Sommer Dec 26 '22 at 16:58
  • Can you give me your setup so that I can check? Also setup your service in docker – Bhagyesh Patel Dec 28 '22 at 06:05