22

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

Pavel Schoffer
  • 492
  • 1
  • 3
  • 11
  • 1
    https://github.com/thaJeztah/pgadmin4-docker/issues/26#issuecomment-354291987 might help you. – Radix Oct 31 '20 at 13:51

2 Answers2

28

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 servers.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.

You can find an example structure for the servers.json file 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.

Pexers
  • 953
  • 1
  • 7
  • 20
Jay Achar
  • 1,156
  • 9
  • 16
  • 9
    Its not that well documented on the pgadmin docker file because it doesn't contain the information in your answer which is actually required to get it going, like the structure of the json file. For others reading this Jay's second link on the passfile also contains a full example of all properties available for the server.json file. Thx Jay. – Dave Pile Jun 18 '21 at 23:40
  • 2
    This seems to apply only for the default pgadmin user. These servers do not show up for other users. So, i tried using 'Shared' flag and its visible for all users but its not pre-populating the username and password. Each user must manually provide the Username and the PassFile again – Hemanth S. Vaddi Jan 14 '22 at 12:54
15

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/

Lincoln D
  • 351
  • 4
  • 7
  • 2
    I get `fe_sendauth: no password supplied` when trying to open the server in pgadmin interface. From what I read it could be because of permissions of the pgpass file but as I am on Windows, I don't know how to fix this – ymoreau Nov 18 '21 at 11:03
  • 1
    Please take a look at my answer here: https://stackoverflow.com/questions/66578506/where-is-the-pgpass-file-in-pgadmin4-docker-container-when-this-file-is-mounted/69258794#69258794 – Lincoln D Nov 19 '21 at 12:52
  • @LincolnD Your answer is very helpful! Only thing I needed to [change](https://stackoverflow.com/a/76823892/4695280) here is volume for `.pgpass`. Also I changed database name in this file from `postgres` to `*` to be able to affect other databases too. – Boolean_Type Aug 02 '23 at 22:39