skip to Main Content

I’m trying to add a postgres server to pgadmin4, through bash commands in a docker compose file.
Here’s my docker compose file:

version: '3.9'
services:
  podcast_db:
    image: postgres:15.3-alpine
    container_name: podcast_db
    ports:
      - 5432:5432
    volumes:
      - podcast_db_data:/usr/share/podcast_db/data
    env_file:
      - ./database/.env.local.db
    # instead, we could have created a `environment` section, with
    # with the env vars in .env.local.db file.
  podcast_pgadmin:
    image: dpage/pgadmin4
    container_name: pgadmin4_container
    ports:
      - 5050:80
    env_file:
      - ./database/.env.local.db
    command: >
      sh -c " sudo apt-get install jq
      | TOKEN=$(curl -s -X POST -d "username=${PGADMIN_DEFAULT_EMAIL}&password=${PGADMIN_DEFAULT_PASSWORD}" ${PGADMIN_URL}/api/v1/auth/login | jq -r .access_token)
      | curl -X POST -H "Authorization: Bearer ${TOKEN}" -H "Content-Type: application/json" -d '{
        "name": "'${POSTGRES_NAME}'",
        "host": "'${POSTGRES_HOST}'",
        "port": '${POSTGRES_PORT}',
        "ssl_mode": "prefer",
        "maintenance_database": "'${POSTGRES_DB}'",
        "username": "'${POSTGRES_USER}'",
        "password": "'${POSTGRES_PASSWORD}'"
      }' ${PGADMIN_URL}/api/v1/servers"


volumes:
  podcast_db_data:

The part above, in the command section, was done with the help of chatGPT… yeah. 😀

I can add the server, using the pgadmin GUI, without any problem…

However, I want to add it from bash.

When I do docker compose up, I don’t get any error messages in the terminal, and when I check through the GUI, no server has been added…

Here is the log of the pgadming container:

NOTE: Configuring authentication for SERVER mode.

pgAdmin 4 - Application Initialisation
======================================

postfix/postlog: starting the Postfix mail system
[2023-10-03 19:47:43 +0000] [1] [INFO] Starting gunicorn 20.1.0
[2023-10-03 19:47:43 +0000] [1] [INFO] Listening at: http://[::]:80 (1)
[2023-10-03 19:47:43 +0000] [1] [INFO] Using worker: gthread
[2023-10-03 19:47:43 +0000] [91] [INFO] Booting worker with pid: 91

All help would be appreciated.

2

Answers


  1. You could easily do that by mapping a servers.json(/pgadmin4/servers.json) file. Checkout https://www.pgadmin.org/docs/pgadmin4/7.7/container_deployment.html#examples

    Then servers.json file will be automatically imported on start.

    Login or Signup to reply.
  2. This is a detailed solution step by step. I have spent a little bit of time to solve it, I followed this post from the early days, but I have a problem with the port, I configed wrong port. Now I test sucessfully.

    current folder:

    • postgres_data
    • docker-compose.yaml
    • pgpass
    • servers.json
    1. servers.json:
      with name is hostname in pgdatabase service
    {
      "Servers": {
        "1": {
          "Name": "pgdatabase",
          "Group": "Servers",
          "Host": "host.docker.internal",
          "Port": 5431,
          "MaintenanceDB": "postgres",
          "Username": "root",
          "PassFile": "/pgpass",
          "SSLMode": "prefer"
        }
      }
    }
    
    1. pgpass:
    host.docker.internal:5431:postgres:root:root
    
    1. docker-compose.yaml
    services:
      pgdatabase:
        image: postgres:13
        hostname: pgdatabase
        environment:
          - POSTGRES_USER=root
          - POSTGRES_PASSWORD=root
          - POSTGRES_DB=postgres
        volumes:
          - postgres_data:/var/lib/postgresql/data:rw
        ports:
          - "5431:5432"
        networks:
          - pg-network-week4
    
      pgadmin:
        restart: always
        image: dpage/pgadmin4
        environment:
          - [email protected]
          - PGADMIN_DEFAULT_PASSWORD=root
        ports:
          - "8080:80"
        networks:
          - pg-network-week4
        volumes:
          - ./servers.json:/pgadmin4/servers.json
          - ./pgpass:/pgpass
        depends_on:
          - pgdatabase
    networks:
      pg-network-week4:
        name: pg-network-week4
    volumes:
      postgres_data:
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search