Home > Software engineering >  cannot login into postgres Docker database using pgadmin portal
cannot login into postgres Docker database using pgadmin portal

Time:01-05

I am using Docker to create a postgres database and pgadmin-portal.

In my docker-compose.yaml I am setting the POSTGRES_USER to be 'root' with a password of 'password'.

docker-compose.yaml (I have left the frontend and backend in but I don't think they're particularly relevant)

version: "3.9"

services:
  backend:
    container_name: backend
    build:
      context: ./apps/server
      dockerfile: Dockerfile.local
    restart: always
    env_file: .env
    volumes:
      - ./apps/server:/svr/app
      - "./scripts/wait.sh:/wait.sh"
      - /svr/app/node_modules
    networks:
      - gh-network
    ports:
      - "${BACKEND_PORT}:${BACKEND_PORT}"
    depends_on:
      - gh-pg-db
    links:
      - gh-pg-db
  gh-pg-db:
    image: postgres:12-alpine
    restart: always
    container_name: gh-pg-db
    env_file:
      - .env
    environment:
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      PGDATA: /var/lib/postgresql/data
      POSTGRES_USER: ${DB_USER}
      POSTGRES_DB: ${DB_NAME}
    ports:
      - "${DB_PORT}:${DB_PORT}"
    volumes:
      - pgdata:/var/lib/postgresql/data
    networks:
      - gh-network
  pgadmin-portal:
    image: dpage/pgadmin4
    restart: always
    container_name: pgadmin-portal
    env_file:
      - .env
    environment:
      PGADMIN_DEFAULT_PASSWORD: "${PGADMIN_DEFAULT_PASSWORD}"
      PGADMIN_DEFAULT_EMAIL: "${PGADMIN_DEFAULT_EMAIL}"
    volumes:
      - pgadmin:/root/.pgadmin
    ports:
      - "${PGADMIN_PORT}:80"
    depends_on:
      - gh-pg-db
    networks:
      - gh-network
volumes:
  pgdata:
  pgadmin:

networks:
  gh-network:
    driver: bridge

However in PGAdmin when I try to connect to the DB with the following details (below) I get this error (below some more):

Details

    host/name address:   gh-pg-db
    port:                5432
    username:            root
    password:            password

Error

Unable to connect to server:
connection to server at "gh-pg-db" (172.20.0.2), port 5432 failed: FATAL:  password authentication failed for user "root"

When I access the terminal for the postgres server (gh-pg-db) in Docker Desktop and run the \l command I get this list(below) and the Owner is always postgres so I can only assume that the PG_USER line in the doccker-composer.yaml is not working as the user should be root and not postgres.

                                List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
----------- ---------- ---------- ------------ ------------ -----------------------
 gh-pg-db  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres           
           |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres           
           |          |          |            |            | postgres=CTc/postgres

Specifically, my question is, am I setting the PG_USER for the pgadmin-portal correctly?

Daniel.

CodePudding user response:

The POSTGRES_* variables are only used when initializing a database. If you have an existing database in your pgdata volume, then the environment variables have no effect and you need to use the users and passwords that are defined in the database already.

From the docs of the image:

Warning: the Docker specific variables will only have an effect if you start the container with a data directory that is empty; any pre-existing database will be left untouched on container startup.

  • Related