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"
    image: postgres
    container_name: local_pgdb
    restart: always
      - "54320:5432"
      POSTGRES_USER: user
      - local_pgdata:/var/lib/postgresql/data
    image: dpage/pgadmin4
    container_name: pgadmin4_container
    restart: always
      - "5050:80"
      - pgadmin-data:/var/lib/pgadmin

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

5 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`

    Liked by 1 person

  2. POSTGRES_USER: user

    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.

    Liked by 1 person

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

    you can look this up in docker network details.

    step 1:

    docker ps

    92264ade8064 postgres “docker-entrypoint.s…” About an hour ago Up About an hour>5432/tcp, :::54320->5432/tcp local_pgdb
    96c52b28c60d dpage/pgadmin4 “/” About an hour ago Up About an hour 443/tcp,>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”: [
    “NetworkID”: “8f4e2e7ad541f28115868813e9e36c5942754e239704a6d614c56dfce0493627”,
    “EndpointID”: “d0dfcfc0c6e2104474ca0b564ba32402d78189d21008811e34c44bb63bab2164”,
    “Gateway”: “”,
    “IPAddress”: “”,
    “IPPrefixLen”: 16,
    “IPv6Gateway”: “”,
    “GlobalIPv6Address”: “”,
    “GlobalIPv6PrefixLen”: 0,
    “MacAddress”: “02:42:ac:15:00:02”,
    “DriverOpts”: null

    Liked by 1 person

Leave a Reply to Stephen Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s