Run PostgreSQL and pgAdmin in docker for local development using docker compose

  • create a directory

    mkdir postgres-docker && cd postgres-docker

  • create a file and name it as docker-compose.yml

  • Add the following content in the docker-compose,yml file
version: "3.8"
services:
  db:
    image: postgres
    container_name: local_pgdb
    restart: always
    ports:
      - "54320:5432"
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: admin
    volumes:
      - local_pgdata:/var/lib/postgresql/data
  pgadmin:
    image: dpage/pgadmin4
    container_name: pgadmin4_container
    restart: always
    ports:
      - "5050:80"
    environment:
      PGADMIN_DEFAULT_EMAIL: raj@nola.com
      PGADMIN_DEFAULT_PASSWORD: admin
    volumes:
      - pgadmin-data:/var/lib/pgadmin

volumes:
  local_pgdata:
  pgadmin-data:
  • First line defines the version of the compose file which is 3.8. You can read more from Docker’s documentation.
  • Next we have  services section. Inside this, we have to define 2 services postgreSQLand pgAdmin
  • container_name is used to define container names for postgreSQL & pgAdmin, the default values are overridden by local_pgdb and pgadmin4_container values
  • restart always will restart the container when either the Docker daemon restarts or the container itself is manually restarted

  • ports is used to define both host and container ports. It maps port 54320 on the host to port 5432 on the container for postgreSQL and 5050 on the host to port 80 on container for pgAdmin
  • environment defines a set of environment variables , for both services we have set the user id and password.
  • volumes tag is used to mount a folder from the host machine to the container. Its the path in the host where the database data is stored. The above config will create volume name local_pgdata and mount this volume to container’s path.
  • Inorder for pgAdmin to persist the connection details even after the container is shutdown , we need to provide volume and that is provided under volumes section. It has to be /var/lib/pgadmin. I was not able to make docker persist the connect details with any other path
  • Docker volumes are the recommended way to persist data. When you stop or down the docker container, the database and connection details will still be there.
  • Now run the below command to run postgreSQL and pgAdmin4 in a detached mode
  • docker compose up -d
  • If all goes well, then below message will be displayed
  • To view the logs , use command docker logs -f local_pgdb
  • To configure pgadmin – open a browser and go to – http://localhost:5050/ . In the connection details for hostname give the container name of postgreSQL

  • This will allow pgAdmin to connect to the postgreSQL database .

    Thank you !! Please share your queries and feedback in the comments section.

10 thoughts on “Run PostgreSQL and pgAdmin in docker for local development using docker compose

  1. Hi from Argentina! I was just looking for a way to run pgadmin4 on Ubuntu 20.04 LTS.
    He also wanted to comment that the command to run docker compose is wrong, it is missing the middle dash: `docker-compose up -d`
    Thanks!!!

    1. There is also docker compose and docker-compose. I think the latter depends on the former. But docker compose works.

  2. POSTGRES_USER: user
    POSTGRES_PASSWORD: admin

    since you already haves user and pwd for postgres, why not installing it them right into the pgadmin container, so pgadmin configuration not set manually as in your screenshits, but comes right out-of-the-box?

    i believe it’s done somehow like that:

    RUN cat mybase:5432:user:admim:mybase >pgpass
    RUN cat ‘{ “Servers….’ >servers.json

    and something like, that. .. somehow.

  3. the connection IP in the pgadmin gui is dependent on the docker network.

    172.21.0.2 for me.

    you can look this up in docker network details.

    step 1:

    docker ps

    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    92264ade8064 postgres “docker-entrypoint.s…” About an hour ago Up About an hour 0.0.0.0:54320->5432/tcp, :::54320->5432/tcp local_pgdb
    96c52b28c60d dpage/pgadmin4 “/entrypoint.sh” About an hour ago Up About an hour 443/tcp, 0.0.0.0:5050->80/tcp, :::5050->80/tcp pgadmin4_container

    step 2:

    docker inspect 92264ade8064

    at the very bottom you find the IPAddress as:

    Networks”: {
    “pgadmin_default”: {
    “IPAMConfig”: null,
    “Links”: null,
    “Aliases”: [
    “local_pgdb”,
    “db”,
    “92264ade8064”
    ],
    “NetworkID”: “8f4e2e7ad541f28115868813e9e36c5942754e239704a6d614c56dfce0493627”,
    “EndpointID”: “d0dfcfc0c6e2104474ca0b564ba32402d78189d21008811e34c44bb63bab2164”,
    “Gateway”: “172.21.0.1”,
    “IPAddress”: “172.21.0.2”,
    “IPPrefixLen”: 16,
    “IPv6Gateway”: “”,
    “GlobalIPv6Address”: “”,
    “GlobalIPv6PrefixLen”: 0,
    “MacAddress”: “02:42:ac:15:00:02”,
    “DriverOpts”: null
    }
    }
    }

  4. Hi! thanks for you tutorial…. In the log pgadmin4_container show this:

    [2022-09-12 00:23:02 +0000] [99] [INFO] Worker exiting (pid: 99)
    ERROR : Failed to create the directory /var/lib/pgadmin/sessions:
    [Errno 13] Permission denied: ‘/var/lib/pgadmin/sessions’
    HINT : Create the directory /var/lib/pgadmin/sessions, ensure it is writeable by
    ‘pgadmin’, and try again, or, create a config_local.py file
    and override the SESSION_DB_PATH setting per
    https://www.pgadmin.org/docs/pgadmin4/6.13/config_py.html
    [2022-09-12 00:23:02 +0000] [100] [INFO] Booting worker with pid: 100
    ERROR : Failed to create the directory /var/lib/pgadmin/sessions:
    [Errno 13] Permission denied: ‘/var/lib/pgadmin/sessions’
    [2022-09-12 00:23:05 +0000] [100] [INFO] Worker exiting (pid: 100)
    HINT : Create the directory /var/lib/pgadmin/sessions, ensure it is writeable by
    ‘pgadmin’, and try again, or, create a config_local.py file
    and override the SESSION_DB_PATH setting per
    https://www.pgadmin.org/docs/pgadmin4/6.13/config_py.html

Leave a Reply to joeCancel reply