dockerpgadminpgadmin-4

Adding postgress connections to pgadmin in docker file


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


Solution

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