Adding postgress connections to pgadmin in docker file

Viewed 5385

Is there a way to pre-configure pgadmin (maybe via env variables) with some server connections?

Say you have this docker-compose.yml, something like PGADMIN_CONNECTIONS env variable in this example? (PGADMIN_CONNECTIONS is probably not a valid ENV variable, it's just an ilustration)

version: '3'
services:
  postgres:
    image: postgres
    hostname: postgres
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: dbuser
      POSTGRES_PASSWORD: dbpass
      POSTGRES_DB: TEST_SM

  pgadmin:
    image: dpage/pgadmin4
    ports:
      - "80:80"
    environment:
      PGADMIN_DEFAULT_EMAIL: pgadmin4@pgadmin.org
      PGADMIN_DEFAULT_PASSWORD: admin

      ??PGADMIN_CONNECTIONS: dbuser:dbpass@postgres:5432

2 Answers

You need to define a servers.json file to configure connections. It's quite well documented here.

The default path inside the container is /pgadmin4/servers.json. You could either COPY your own version of the server.json file into a newly built image, or bind mount the file into the container when it is run.

The password cannot be passed to the pgadmin through JSON, but a file containing the password is referenced in the example below. See below for further information.

An example structure for the server.json file is below:

{
  "Servers": {
    "1": {
      "Name": "pgadmin4@pgadmin.org",
      "Group": "Servers",
      "Host": "magic_db",
      "Port": 5432,
      "MaintenanceDB": "postgres",
      "Username": "postgres",
      "PassFile": "/pgpass",
      "SSLMode": "prefer"
    }
  }
}

The /pgpass requires the following structure:

hostname:port:database:username:password

So for the example above it would be:

magic_db:5432:postgres:postgres:secretpassword

Note: Password was changed to PassFile in newer versions of pgadmin as seen here.

This config works for me:

  • postgres_db_data/
  • pgpass
  • servers.json
  • docker-compose.yaml
  1. create an empty dir call postgres_db_data

  2. create "pgpass" with the following contents

host.docker.internal:15432:postgres:postgres:postgres

  1. create "servers.json" with the following contents
{
  "Servers": {
    "1": {
      "Name": "docker_postgres",
      "Group": "docker_postgres_group",
      "Host": "host.docker.internal",
      "Port": 15432,
      "MaintenanceDB": "postgres",
      "Username": "postgres",
      "PassFile": "/pgpass",
      "SSLMode": "prefer"
    }
  }
}
  1. create "docker-compose.yaml" with the following contents
version: "3.9"
services:
  db:
    image: postgres
    volumes:
      - ./postgres_db_data:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - "15432:5432"
  pgadmin:
     image: dpage/pgadmin4
     restart: always
     environment:
       PGADMIN_DEFAULT_EMAIL: pgadmin@pgadmin.com #the username to login to pgadmin
       PGADMIN_DEFAULT_PASSWORD: pgadmin # the password to login to pgadmin
     ports:
       - "5050:80"
     volumes:
       - ./servers.json:/pgadmin4/servers.json # preconfigured servers/connections
       - ./pgpass:/pgpass # passwords for the connections in this file
     depends_on:
       - "db"
  1. run docker compose up

  2. open http://localhost:5050/browser/

Related