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.
Any ideas?