pgadmin4 icon indicating copy to clipboard operation
pgadmin4 copied to clipboard

Password not loaded from .pgpass file

Open Brice-D opened this issue 2 years ago • 9 comments

Describe the bug

I setup a pgadmin service in my docker compose file with preloaded servers in servers.json and credentials configured in .pgpass. The servers are loaded at start up but when I open it in the browser I'm getting the following error and I need to enter the password manually

image

To Reproduce

Steps to reproduce the behavior:

servers.json

{
  "Servers": {
    "1": {
      "Name": "Servers",
      "Group": "GrafanaGroup",
      "Host": "pg_data_wh",
      "Port": 5432,
      "MaintenanceDB": "postgres",
      "Username": "grafana_data_user",
      "PassFile": "/var/lib/storage/pgpass/.pgpass",
      "SSLMode": "prefer",
      "ConnectionTimeout": 10
    }
  }
}

.pgpass

#hostname:port:database:username:password
pg_data_wh:5432:grafanaDataDB:grafana_data_user:heremypassword

service in docker compose

  pg_admin:
    hostname: pg_admin
    image: dpage/pgadmin4
    restart: always
    user: root
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: mypgadminpasswordhere
      PGADMIN_LISTEN_ADDRESS: 0.0.0.0
      # more from https://www.pgadmin.org/docs/pgadmin4/8.12/container_deployment.html
      # PGADMIN_SERVER_JSON_FILE:
      PGPASSFILE: /var/lib/storage/pgpass/.pgpass
      PGADMIN_CONFIG_CONSOLE_LOG_LEVEL: 10
    ports:
      - "15432:80"
    volumes:
      - ./config/ZscalerRootCertificate-2048-SHA256.crt:/tmp/ZscalerRootCertificate-2048-SHA256.crt
      #- ./servers.json:/pgadmin4/servers.json
      #- pgAdminData:/var/lib/pgadmin
      - ./.pgpass:/etc/tmp/.pgpass
      - ./servers.json:/etc/tmp/servers.json
    entrypoint: >
      /bin/sh -c "
        mkdir -p /etc/ssl/certs/zscaler && \
        cp /tmp/ZscalerRootCertificate-2048-SHA256.crt /etc/ssl/certs/zscaler/ && \
        cat /etc/ssl/certs/zscaler/ZscalerRootCertificate-2048-SHA256.crt >> /etc/ssl/certs/ca-certificates.crt && \
        mkdir -p /var/lib/storage/pgpass && \
        cp /etc/tmp/.pgpass /var/lib/storage/pgpass/.pgpass && \
        chown pgadmin:root /var/lib/storage/pgpass/.pgpass && \
        chmod 0600 /var/lib/storage/pgpass/.pgpass && \
        cp /etc/tmp/servers.json /pgadmin4/servers.json && \
        chown pgadmin:root /pgadmin4/servers.json && \
        exec /entrypoint.sh
      "

Expected behavior

I'm expected the password to be read from .pgpass and not required when I open the servers in the Browser image

Error message

2024-10-11 07:25:56,994: INFO pgadmin: Connection Request for server#1 2024-10-11T07:25:57.006704649Z 2024-10-11 07:25:57,005: INFO pgadmin: Waiting for a lock. 2024-10-11T07:25:57.007041349Z 2024-10-11 07:25:57,006: INFO pgadmin: Acquired a lock. 2024-10-11T07:25:57.008473931Z 172.18.0.1 - - [11/Oct/2024:07:25:57 +0000] "GET /browser/server/obj/2/1 HTTP/1.1" 200 470 "http://localhost:15432/browser/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36" 2024-10-11T07:25:57.009967057Z 172.18.0.1 - - [11/Oct/2024:07:25:57 +0000] "GET /browser/server_group/nodes/ HTTP/1.1" 200 429 "http://localhost:15432/browser/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36" 2024-10-11T07:25:57.011130538Z 2024-10-11 07:25:57,010: INFO pgadmin: Released a lock. 2024-10-11T07:25:57.011179753Z 2024-10-11 07:25:57,011: INFO pgadmin: Failed to connect to the database server(#1) for connection (DB:postgres) with error message as below:connection failed: connection to server at "172.18.0.4", port 5432 failed: fe_sendauth: no password supplied 2024-10-11T07:25:57.011325101Z 2024-10-11 07:25:57,011: ERROR pgadmin: Could not connect to server(#1) - 'Servers'. 2024-10-11T07:25:57.011346488Z Error: connection failed: connection to server at "172.18.0.4", port 5432 failed: fe_sendauth: no password supplied 2024-10-11T07:25:57.011922796Z 172.18.0.1 - - [11/Oct/2024:07:25:57 +0000] "POST /browser/server/connect/2/1 HTTP/1.1" 401 303 "http://localhost:15432/browser/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"

Brice-D avatar Oct 11 '24 07:10 Brice-D

@Brice-D Here is an example of docker-compose for passing pgpass file for connecting to the server. Please make sure the source file mapped to pgpass file has permission 600 or less. pgpass file should be present in user home directory.

version: "3.8"

services:
  pgadmin:
    container_name: pgadmin
    image: dpage/pgadmin4:8.12
    environment:
      - [email protected]
      - PGADMIN_DEFAULT_PASSWORD=adminedb
      - PGADMIN_CONFIG_CONSOLE_LOG_LEVEL=10
    ports:
      - "5050:80"
    volumes:
      - ./config/servers2.json:/pgadmin4/servers.json
      - ./config/pgpass:/tmp/.pgpass
      - pgadmin-data:/var/lib/pgadmin
    entrypoint: >
        /bin/sh -c "
        mkdir -p /var/lib/pgadmin/storage/user_edb.com/;
        cp /tmp/.pgpass /var/lib/pgadmin/storage/user_edb.com/.pgpass;
        chown pgadmin:root /var/lib/pgadmin/storage/user_edb.com/.pgpass;
        chmod 600 /var/lib/pgadmin/storage/user_edb.com/.pgpass;
        /entrypoint.sh
        "

volumes:
  pgadmin-data:
    name: 'pgadmin-data_with_userlevle_pgpass'

server.json -

{
    "Servers": {
        "1": {
            "Name": "pg16-pgpass",
            "Group": "AWS",
            "Host": "ip",
            "Port": port,
            "MaintenanceDB": "postgres",
            "Username": "postgres",
            "UseSSHTunnel": 0,
            "TunnelPort": "22",
            "TunnelAuthentication": 0,
            "KerberosAuthentication": false,
            "ConnectionParameters": {
                "sslmode": "prefer",
                "connect_timeout": 10,
                "sslcompression": 0,
                "passfile": "/.pgpass"
            }
        },
    }
}

yogeshmahajan-1903 avatar Oct 14 '24 11:10 yogeshmahajan-1903

@yogeshmahajan-1903 configured that way, the servers informations are no more loaded at startup and I'm getting an exception (bad parsing) -> see attached logs ErrorLogs.log

I need to mention that the container is start with the root use as you can see in my container definition. pgadmin use can't create a new directory, I was getting permission denied with pgadmin

pg_admin:
  hostname: pg_admin
  image: dpage/pgadmin4
  restart: always
  user: root
  environment:
    PGADMIN_DEFAULT_EMAIL: [email protected]
    PGADMIN_DEFAULT_PASSWORD: mypassword
    PGADMIN_LISTEN_ADDRESS: 0.0.0.0
    # more from https://www.pgadmin.org/docs/pgadmin4/8.12/container_deployment.html
    # PGADMIN_SERVER_JSON_FILE:
    #POSTGRES_PASSFILE_STORE: /var/lib/storage/pgpass/.pgpass
    #PGADMIN_CONFIG_CONSOLE_LOG_LEVEL: 10
  ports:
    - "15432:80"
  volumes:
    - ./config/ZscalerRootCertificate-2048-SHA256.crt:/tmp/ZscalerRootCertificate-2048-SHA256.crt
    #- ./servers.json:/pgadmin4/servers.json
    #- pgAdminData:/var/lib/pgadmin
    - pgadmin-data:/var/lib/pgadmin
    - ./.pgpass:/tmp/.pgpass
    - ./servers.json:/pgadmin4/servers.json
  entrypoint: >
    /bin/sh -c "
      mkdir -p /etc/ssl/certs/zscaler && \
      cp /tmp/ZscalerRootCertificate-2048-SHA256.crt /etc/ssl/certs/zscaler/ && \
      cat /etc/ssl/certs/zscaler/ZscalerRootCertificate-2048-SHA256.crt >> /etc/ssl/certs/ca-certificates.crt && \
      mkdir -p /var/lib/pgadmin/storage/user_edb.com/;
      cp /tmp/.pgpass /var/lib/pgadmin/storage/user_edb.com/.pgpass;
      chown pgadmin:root /var/lib/pgadmin/storage/user_edb.com/.pgpass;
      chmod 600 /var/lib/pgadmin/storage/user_edb.com/.pgpass;
      /entrypoint.sh
    "
{
  "Servers": {
    "1": {
      "Name": "Servers",
      "Group": "GrafanaGroup",
      "Host": "pg_data_wh",
      "Port": 5432,
      "MaintenanceDB": "postgres",
      "Username": "grafana_data_user",
      "ConnectionParameters": {
        "sslmode": "prefer",
        "connect_timeout": 10,
        "sslcompression": 0,
        "passfile": "/.pgpass"
      }
    }
  }
}

.pgpass has the correct permission in the container image

Brice-D avatar Oct 14 '24 13:10 Brice-D

@Brice-D I guess server.json has incorrect key 'SSLmode'. It should be "sslmode". Servers from server.json is loaded only first time database creation. If persistent volume is used, then servers will not be loaded again. If you you want to add new servers to existing container with persistent volume, then you can use UI utility Tools > Import/Export Server. For existing servers, you need to add parameter form server configuration dialogue.

yogeshmahajan-1903 avatar Oct 15 '24 07:10 yogeshmahajan-1903

No response from the author, hence closing it.

akshay-joshi avatar Nov 06 '24 07:11 akshay-joshi

On my server.json file it's clearly sslmode.

Brice-D avatar Jun 09 '25 18:06 Brice-D

@Brice-D did you manage to fix this? I am having the same issue.

EDIT: It appears as though the folder name /var/lib/pgadmin/storage/user_edb.com/ perhaps needs to correspond to the user email? e.g. for the user [email protected], the folder name is /var/lib/pgadmin/storage/pgadmin4_pgadmin.org/

james-advatek avatar Dec 07 '25 22:12 james-advatek

@james-advatek For Docker Image dpage/pgadmin:9.4.0

You need to find a way to mount the .pgpass to the path /tmp/pgpass/.pgpass (I had to read the code in github to find it - documentation is not updated somehow). I created bash script to do it and also make sure that in the container it has permission 600

set the env variable PGPASS_FILE: "/tmp/pgpass/.pgpass" in the container definition and make sure in the servers.json file that you have the following definition (see my previous comment)

  "ConnectionParameters": {
    "sslmode": "prefer",
    "connect_timeout": 10,
    "sslcompression": 0,
    "passfile": "/.pgpass"
  }

Let me know if it helped you. If not I can share my entire code here

Brice-D avatar Dec 08 '25 02:12 Brice-D

I found a cleaner approach (similar to what Brice-D mentioned) to provide pgAdmin/libpq credentials without baking anything into the image or doing a runtime “cp” step.

The idea is to mount a .pgpass file via Docker Compose secrets, then point libpq at it using passfile in the server definition (or PassFile if you prefer pgAdmin’s field). This keeps the secret in the Compose secret store and makes the config fairly natural.

Docker Compose

secrets:
  pgadmin_pgpass:
    file: ./secrets/.pgpass

services:
  pgadmin:
    # ...
    environment:
      PGPASSFILE: /run/secrets/.pgpass
    secrets:
      - source: pgadmin_pgpass
        target: .pgpass  # -> /run/secrets/.pgpass

servers.json (libpq parameter form)

"ConnectionParameters": {
  "connect_timeout": 10,
  "sslmode": "prefer",
  "passfile": "/run/secrets/.pgpass"
}

Notes / gotchas

  • Ensure the runtime user can read the file. In the pgAdmin container this is typically uid/gid 5050:

    sudo chown 5050:5050 ./secrets/.pgpass
    sudo chmod 600 ./secrets/.pgpass
    

itzonevn avatar Feb 27 '26 01:02 itzonevn

Hi @Brice-D

I am really interested to know how you managed to make it work ! I've been struggling for 2days digging into old docs / forums to provide passwords for a servers.json file but couldn't make it work.

Could you eventually show me how your complete compose.yml and servers.json look like ?

Thanks in advance !

XayneCast avatar Apr 23 '26 16:04 XayneCast