Connect to postgres running in docker using python

Viewed 26

I am having a problem connecting to the Postgres running inside a docker container using my python script.

docker-compose

version: '3'

services:

  postgres_db:
    image: postgres:12.12
    restart: always
    environment:
      POSTGRES_DB: database
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: pass123

    ports:
      - 5432:5432

    volumes:
      - ./postgres:/docker-entrypoint-initdb.d

Python script

psycopg2.connect(database="database", user='postgres', password='pass123', host='localhost', port= 5432)

When I try to connect to the postgres inside docker I am having getting the following error.

OperationalError: connection to server at "localhost" (::1), port 5432 failed: FATAL:  password authentication failed for user "postgres"
0 Answers
Related