Sample from class:
try:
conn = psycopg2.connect(
database='ytbstatsdb',
host='127.0.0.1',
user='admin',
password='admin123',
port='5432'
)
except psycopg2.Error as e:
print(e)
cursor = conn.cursor()
engine = sqlalchemy.create_engine('postgresql://admin:admin123@localhost:5432/ytbstatsdb')
df_videos_details.to_sql('testtable', engine, if_exists='replace', index=False)
Docker file (docker-compose.yaml):
services:
db:
image: postgres
ports:
- 5432:5432
restart: always
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: admin123
POSTGRES_DB: ytbstatsdb
adminer:
image: adminer
restart: always
depends_on:
- db
ports:
- 8080:8080
My error:
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?
The db in docker is created:
docker-image
Hello!
I’m trying to connect to a database using docker. Accessing http://localhost:8080 is working, I can log in to Adminer and work with my database on the Adminer interface, but when I try to connect using the code from the class with psycopg2 I receive the specified error.
Do you have any clue on why I get it?
I restarted the pc to clear all the services and reset it to the essential ones, changed the port to 7432 and so on. Nothign works…
2
Answers
you should NOT use ‘0.0.0.0’ as source address, this address only used by sercice binding.
First check what you have set in pg_hba.conf!
If Lokalhost doesn’t ask for a password, don’t use it.
If a password is required:
the password must be entered in the appropriate place. Where you find it depends on the operating system.
Since it does not expect a password in the first case, it is a significant source of security-danger.