I have have a container running postgresql and I want to ingest there some data that I have prepared in a ETL script. I have created the database, however, When I try to connect in my local machine through a jupyter notebook, it keeps saying it cannot find the host, even though I set it in my .yaml file.
My .yaml file is this one:
version: '3.7'
services:
postgres:
image: library/postgres
container_name: postgres_db
restart: unless-stopped
ports:
- 5432:5432
environment:
- POSTGRES_PASSWORD=password123
- POSTGRES_USER=user123
- POSTGRES_DB=my_db
hostname: postgres
volumes:
- ./pgdata/data:/var/lib/postgresql/data
- ./initdb:/docker-entrypoint-initdb.d
In my jupyter notebook I have this connection test:
import psycopg2
DATABASE_HOST = 'postgres'
DATABASE_PORT = 5432
DATABASE_USER = 'user123'
DATABASE_PASSWORD = 'password123'
DATABASE_NAME = 'my_db'
conn = psycopg2.connect(
host=DATABASE_HOST,
port=DATABASE_PORT,
user=DATABASE_USER,
password=DATABASE_PASSWORD,
dbname=DATABASE_NAME
)
The logs on docker side never come back with any type of connection attempt.
OperationalError: could not translate host name "postgres" to address: Unknown host
Ronnie433 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.