Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: [email protected]
      PGADMIN_DEFAULT_PASSWORD: admin

      ??PGADMIN_CONNECTIONS: dbuser:dbpass@postgres:5432

like image 404
Pavel Schoffer Avatar asked Oct 31 '20 09:10

Pavel Schoffer


People also ask

How do I run PostgreSQL and pgAdmin in docker?

Now let's connect pgadmin4 to our PostgreSQL database server. First, access the pgadmin4 via your favorite web browser by vising the URL http://localhost:5050/. Use the [email protected] for the email address and root as the password to log in. Create a server.

How do I add connections to pgAdmin?

Launch pgAdmin 4. Go to the “Dashboard” tab. In the “Quick Link” section, click “Add New Server” to add a new connection. Select the “Connection” tab in the “Create-Server” window.

How do I connect to PostgreSQL docker?

Setup PostgreSQL Using the Public Image We exposed the 5432 port on the host using the “-p 5432:5432” in the docker run command. To back up the data, we also mounted the /var/lib/postgresql/data directory to the /data directory of the host machine of the postgres container.


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": "[email protected]",
      "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.

like image 190
Jay Achar Avatar answered Oct 16 '22 22:10

Jay Achar


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: [email protected] #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/

like image 10
Lincoln D Avatar answered Oct 16 '22 20:10

Lincoln D