1

I'm trying to connect to my local server. I have the following docker-compose.yaml and servers.json files. I think I'm making a mistake with my .pgpass. When the containers are up and running I can login to pgAdmin, and I can see the docker_postgres_group "group", but when I try to login I get the authentication error in the first screenshot below. Further below is how I've got my pgpass set up. I think this is where the problem is, but I could be wrong...

docker-compose.yaml

version: '3.8'
services:
  db:
    container_name: pg_container
    image: postgres
    restart: always
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: root
      POSTGRES_DB: test_db
    ports:
      - "5432:5432"
  pgadmin:
    container_name: pgadmin4_container
    image: dpage/pgadmin4
    restart: always
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: root
   
    ports:
      - "5050:80"
    volumes:
       - ./servers.json:/pgadmin4/servers.json # preconfigured servers/connections
       - ./pgpass:/pgpass # passwords for the connections in this file

servers.json

{
  "Servers": {
    "1": {
      "Name": "docker_postgres",
      "Group": "docker_postgres_group",
      "Host": "host.docker.internal",
      "Port": 15432,
      "MaintenanceDB": "postgres",
      "Username": "postgres",
      "PassFile": "/pgpass",
      "SSLMode": "prefer"
    }
  }
}

pgadmin screenshot

.pgpass This is what's inside my pgpass file: host.docker.internal:15432:postgres:postgres:postgres

Here's its location: enter image description here

I've tried having the .pgpass file in the pgpass folder and outside.
And - I'm using postgres as the password to try to login to pgadmin.

2
  • Do you mean port 5432 (as the answer by ddegasperi suggest)? Commented Sep 23, 2021 at 6:31
  • My question was more about whether I've got the pgpass file in the right place, but @ddegasperi's points are valid. Looking into it. Commented Sep 23, 2021 at 19:32

1 Answer 1

1

When spinning up containers through docker-compose, also a network is created where each service can see the other services of the docker-compose definition. They are also resolvable by there servicename. In your case you can replace host.docker.internal with db and use the internal port 5432.

EDIT: host.docker.internal resolves the internal IP address used by your host machine. If you want connect through the host IP, the you have either to change the port in servers.json from 15432 to 5432 or the port mapping from the db service:

ports:
 - "15432:5432"
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. Should the .pgpass file be in the same directory as the docker-compose file? I changed the .pgpass to have db instead of host.docker.internal and changed the port. Now I'm getting: fe_sendauth: no password supplied when I load up pgadmin, and then FATAL: password authentication failed for user "postgres" when I enter "postgres" into the dialog.
I've seen that you reference for PassFile the pgpass folder in servers.json, but the complete path to the file is expected here. This means when you map the folder with the pgpass file into the container like your example, PassFile should point to /pgpass/.pgpass. The host and port fixes you've applied to the pgpass file have to be fixed also in the servers.json file
That did the trick! Thanks so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.