Home > OS >  Cannot psql into docker postgres image port forwarded locally
Cannot psql into docker postgres image port forwarded locally

Time:11-02

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:\Users\danie\Desktop\dev\cnc-db\db-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:\Users\danie\Desktop\dev\cnc-db\db-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:\Users\danie\Desktop\dev\cnc-db\db-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.

CodePudding user response:

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

CodePudding user response:

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/

  • Related