skip to Main Content

I’m continuously hitting an error when trying to psql into a docker composed postgres image that has its ports forwarded. (this issue seems to persist also when attempting to access the DB programatically via node application).

Running docker-compose up -d on the following docker compose file:

services:
  postgres:
    container_name: cnc-matches
    image: postgres:12.1-alpine
    ports:
      - '5432:5432'
    environment:
      POSTGRES_USER: dbuser
      POSTGRES_PASSWORD: pass
      POSTGRES_DB: cnc-matches

When running psql to attempt to access it I hit the following error continuously:

C:UsersdanieDesktopdevcnc-dbdb-setup>psql -h "localhost" -p "5432" -U dbuser
Password for user dbuser: pass
psql: error: connection to server at "localhost" (::1), port 5432 failed: FATAL:  password authentication failed for user "dbuser"

When running docker exec I’m able to access the table and info fine:

C:UsersdanieDesktopdevcnc-dbdb-setup>docker exec -it cnc-matches psql -U dbuser cnc-matches
psql (12.1)
Type "help" for help.

cnc-matches=# du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 dbuser    | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

I’ve tried creating a new user as well as altering the dbuser profiles passwords in here with ALTER PASSWORD dbuser WITH PASSWORD ‘pass’ and I still cannot access the db with default psql command locally.

cnc-matches=# CREATE USER tester WITH PASSWORD 'tester';
CREATE ROLE
cnc-matches=# du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 dbuser    | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 tester    |                                                            | {}

C:UsersdanieDesktopdevcnc-dbdb-setup>psql -h "localhost" -p "5432" -U tester
Password for user tester: tester
psql: error: connection to server at "localhost" (::1), port 5432 failed: FATAL:  password authentication failed for user "tester"

Not sure what it is I’m misisng here, if relevant running via windows 11 cmd.
Any help/suggestions appreciated.

3

Answers


  1. Looks like you also need to include the database name when connecting to the database running in the Docker container, ex

    psql -h "0.0.0.0" -p "5432" -U dbuser cnc-matches
    
    Login or Signup to reply.
  2. First time I am posting, sorry if there is wrong formatting.
    I updated your docker-compose file with a network bridge as still isolated from the network with the host machine.

    Steps:

    "db-net" can be any name you want but both must coincide with your docker compose file

    1. Create a docker network with: docker network create db-net
    2. Run: docker-compose up -d
      version: "3.9"
      services:
        postgresCNC:
          container_name: cnc-matches
          image: postgres:15.0-alpine
          ports:
            - '5432:5432'
          environment:
            POSTGRES_USER: dbuser
            POSTGRES_PASSWORD: pass
            POSTGRES_DB: cnc-matches
          networks:
            db-net:
      
      networks:
        db-net:
          driver: bridge

    Network bridge by default uses the hardware network device to communicate between containers and host machine as with containers with others containers.
    More info here:
    https://docs.docker.com/network/bridge/

    Login or Signup to reply.
  3. If the container was successfully started, try running:

    psql -U dbuser -d cnc-matches -h 0.0.0.0 --port 5432

    For the port use whatever you mapped yours in the container to; in your case 5432.

    You must specify all that explicitly or else you will get an error that the database cannot accept TCP/IP connections.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search