C
C
CosmoV2019-10-31 23:47:24
PostgreSQL
CosmoV, 2019-10-31 23:47:24

How to connect using psql to a subd located inside a docker container?

Good afternoon, I have PostgresSQL running inside a docker container and I want to connect to it using the psql utility:

sudo psql -h localhost -p 5432 -U postgres -d master

but something goes wrong and when passing authentication, I permanently get an error:
FATAL: password authentication failed for user "postgres"

I'm running the whole thing on Ubuntu 19.10 via docker-compose:
version: "3.4"

networks:
  default_network:
    ipam:
      config:
        - subnet: 18.129.0.0/16

services: 
  postgres:
    image: postgres:11
    networks:
      - default_network
    ports:
      - ${DB_EXTERNAL_PORT}:${DB_INTERNAL_PORT}
    environment:
      POSTGRES_DB: ${DB_NAME}
      POSTGRES_USER: ${USER}
      POSTGRES_PASSWORD: ${PASSWORD}
    volumes:
    - ./db_data:/var/lib/postgresql/data

and accordingly environment variables:
DB_NAME=master
USER=postgres
PASSWORD=postgres
DB_EXTERNAL_PORT=5432
DB_INTERNAL_PORT=5432

It would be funny if it really was a password error, but I couldn't make it wrong 15 times in a row, including copy-paste. Thanks in advance to those who will not pass by, I suspect the error is that when connecting to the container, you need to log in under a certain linux user, but I'm not sure.
UPD:
I forgot to look at the container logs
DETAIL: Role "postgres" does not exist.
but anyway, how can a role not exist if it is set in POSTGRES_USER or is there a role under the role here?
UPD 1:
The user exists and the python code easily connects to the database when it is in an adjacent container on the same subnet. Apparently, I have not configured the permissions configuration located in the pg_hba.conf file. documentation
It remains to understand which address space the localhost belongs to for the contents of the container.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question