Home > Mobile >  cannot access postgres db running docker container from local machine
cannot access postgres db running docker container from local machine

Time:09-14

I have been spending 3-4 hours on this and still have not found a solution.

I can successfully run the docker container and use psql from the container bash, however, when I try to call the db from my local machine I continue to get this error message:

error role "postgres" does not exist

I have already tried editing "listen_addresses" in the postgresql.conf file from the container bash

My setup: I am using a macbook - Monterey 12.4

my docker compose file: version: '3.4'

services:
 postgres:
    image: postgres:latest
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_DB=postgres_db
      - POSTGRES_USER=testUser
      - POSTGRES_PASSWORD=testPW
    volumes:
      - postgres-data:/var/lib/postgresql/db

but this issue occurs if I do it through the standard CLI command as well, i.e:

docker run -d -p 5432:5432 --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword postgres

I tried to follow this tutorial but it didnt work: [https://betterprogramming.pub/connect-from-local-machine-to-postgresql-docker-container-f785f00461a7][1]

when I try this command: psql -h localhost -p 5432 -U postgres -W

it doesnt work: psql: error: connection to server at "localhost" (::1), port 5432 failed: FATAL: role "postgres" does not exist

Also for reference, the user "postgres" does exist in postgres - as a superuser

CodePudding user response:

Replace POSTGRES_USER=testUser with POSTGRES_USER=postgres in the compose configuration. Also use the password defined in POSTGRES_PASSWORD. Delete the old container and create a new one.

CodePudding user response:

Thank you all for your help on this.

It turns out the issue was that I was running postgres on my local machine as well.

so once I turn that off I was able to connect.

I appreciate your time!

  • Related