通过docker compose文件中的bash命令将postgres服务器添加到pgadmin4

3z6pesqy  于 2023-10-16  发布在  Docker
关注(0)|答案(2)|浏览(96)

我正在尝试通过docker compose文件中的bash命令向pgadmin4添加postgres服务器。这是我的docker compose文件:

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:

上面的部分,在命令部分,是在chatGPT的帮助下完成的。yeah.:D
我可以添加服务器,使用pgadmin GUI,没有任何问题.
但是,我想从bash添加它。
当我执行docker compose up时,我在终端中没有收到任何错误消息,当我通过GUI检查时,没有添加服务器。
下面是pgadding容器的日志:

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

所有的帮助将不胜感激。

yx2lnoni

yx2lnoni1#

您可以通过Mapservers.json(/pgadmin4/servers.json)文件轻松做到这一点。查看https://www.pgadmin.org/docs/pgadmin4/7.7/container_deployment.html#examples
然后servers.json文件将在启动时自动导入。

oknwwptz

oknwwptz2#

这是一步一步的详细解决方案。我花了一点时间来解决它,我从早期就关注了这个帖子,但是我的端口有问题,我安装了错误的端口。现在我测试成功了。
当前文件夹:

  • postgres_data
  • docker-compose.yaml
  • pgpass
  • servers.json
  1. servers.json:使用name时,pgdatabase服务中的hostname
{
  "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:

相关问题