2

Hey im trying to create a postgresql db container, im running it using the command:

docker-compose up

on the following compose file:

version: '3.1'
services:

  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_USERNAME: admin
      POSTGRES_PASSWORD: admin
      POSTGRES_DB: default_db
    ports:
      - 54320:5432

However when I try to connect to it using the follwoing python code:

import sqlalchemy
engine = sqlalchemy.create_engine('postgres://admin:admin@localhost:54320/default_db')
engine.connect()

I get the following error:

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL:  password authentication failed for user "admin"

Anyone knows why this happens?

2
  • How is your postgres authentication configured? It appears that the client library is able to access Postgres, but not to authenticate. Try starting the containers, then doingdocker exec -it <postgres-container-id> /bin/sh, and try to connect to Postgres from within the container. Commented Sep 20, 2019 at 18:31
  • 3
    Try POSTGRES_USER instead of POSTGRES_USERNAME Commented Sep 20, 2019 at 18:49

2 Answers 2

2

Using POSTGRES_USER instead of POSTGRES_USERNAME solved this for me.

Sign up to request clarification or add additional context in comments.

Comments

1

You should use POSTGRES_USER instead of POSTGRES_USERNAME.

Here is my postgres docker-compose configuration for your reference.

version: '3'
services:
    postgres:
        image: 'mdillon/postgis:latest'
        environment:
            - TZ=Asia/Shanghai
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWOR=postgres
        ports:
            - '15432:5432'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.