Guide to Run PostgreSQL dpage pgAdmin4 with Docker Compose

Posted October 10, 2023
Docker - Connect Postgres dpage pgadmin4 With Docker Compose

Do you want to run pgAdmin4 on Docker Compose? This guide teaches you to run dpage pgAdmin4 with Docker Compose. This way, you can run Docker Compose for Postgres and access it with dpage pgadmin4.

What you Will Learn

Along this guide, you will learn:

  • How to run PostgreSQL on Docker.
  • Execute dpage pgAdmin4 with Docker Compose.
  • Connect pgAdmin4 to Postgres using Docker.
  • How to use pgadmin_default_email and access PostgreSQL.

Prerequisites to Running pgAdmin4 Docker Compose

To follow along with this guide:

  • Ensure you have Docker installed and running on your computer

Related: Docker Slim With Minimal Reduce Docker Image Size Strategies

Related: Run Adminer with Docker Compose for and Postgres Container

Why Run dpage pgAdmin4 with Docker Compose Along Side PostgreSQL

Running pgAdmin4 with Docker Compose and PostgreSQL:

  • Create a perfect setup for development, testing, and production environments.
  • You create and manage PostgreSQL on isolation without dependencies conflicts and version mismatches.
  • Easy to scale, especially as a container
  • You get added deployment options.

Creating PostgreSQL Container with Docker Compose

Before running dpage pgAdmin4, you need a working container of your PostgreSQL. You will then use the PostgreSQL container and connect it to dpage pgAdmin4 over Docker Compose.

Let’s dive and create a Docker Compose file that you will use to run Postgres dpage pgAdmin4 With Docker.

In your working directory, create a docker-compose.yml file and run PostgreSQL with Docker Compose as follows:

version: '3.9'
services:
  # PostgreSQL service
  db:
    image: postgres:latest
    environment:
      # PostgreSQL database
      POSTGRES_DB: testdb
      # PostgreSQL username
      POSTGRES_USER: myuser
      # SPostgreSQL password
      POSTGRES_PASSWORD: mypassword
    volumes:
      # Mount a volume to persist PostgreSQL data
      - postgres_data:/var/lib/postgresql/data
    networks:
      - custom_network
    restart: always
# bridge driver for the custom network
networks:
  custom_network:
    driver: bridge 

volumes:
  # volume for PostgreSQL
  postgres_data:

In this code sample, Docker Compose will execute a PostgreSQL container and set all the elements needed to get a PostgreSQL container up and running on Docker. Restart policy is also set to always.

You can edit the restart to fit your requirements, i.e.:

  • always: Always restart the container if it stops, regardless of the exit code.
  • on-failure: Restart the container only if it stops with a non-zero exit code.
  • unless-stopped: Always restart the container unless explicitly stopped by the user.
  • no: Do not automatically restart the container.

Once the PostgreSQL container runs, we can access it On a UI, where dpage pgAdmin4 comes into play. dpage pgAdmin4 will access your PostgreSQL container and maps its data so you can use pgAdmin to manage the database.

Let’s dive in now and get dpage pgAdmin4 on Docker.

Creating dpage pgAdmin4 with PostgreSQL Docker Container

Since you are now adding dpage pgAdmin4 into the picture, your docker-compose.yml file should be updated as follows:

version: '3.9'
services:
  # PostgreSQL service
  db:
    image: postgres:latest
    environment:
      # PostgreSQL database
      POSTGRES_DB: testdb
      # PostgreSQL username
      POSTGRES_USER: myuser
      # SPostgreSQL password
      POSTGRES_PASSWORD: mypassword
    ports:
      - "5432:5432"
    volumes:
      # Mount a volume to persist PostgreSQL data
      - postgres_data:/var/lib/postgresql/data
    networks:
      - custom_network
    restart: always

  # pgAdmin 4 service
  pgadmin:
    image: dpage/pgadmin4:latest
    environment:
      # Default email for pgAdmin 4
      PGADMIN_DEFAULT_EMAIL: [email protected]
      # Password for pgAdmin 4
      PGADMIN_DEFAULT_PASSWORD: adminpassword
    ports:
      # Map host port 5050 to container port 80
      - "5050:80"
    volumes:
      # Mount a volume to persist pgAdmin 4 data
      - pgadmin_data:/var/lib/pgadmin
    depends_on:
      # Ensure that this service starts after 'db'
      - db
    networks:
      - custom_network
    restart: always

# bridge driver for the custom network
networks:
  custom_network:
    driver: bridge 

volumes:
  # volume for PostgreSQL
  postgres_data:
  # Volume for pgAdmin 4
  pgadmin_data:

Here:

  • environment will define the authentication details you need to access pgAdmin.
  • You need PGADMIN_DEFAULT_EMAIL and PGADMIN_DEFAULT_PASSWORD set up based on your liking
  • pgAdmin depends on PostgreSQL. Therefore, you must let Docker Compose know that. This is done using depends_on. So, the pgAdmin service will only start after the PostgreSQL db service is running successfully.
  • To create a pgAdmin container, use the dpage/pgadmin4 image from DockerHub.
  • pgAdmin will be exposed and accessed on port 5050.

Connecting page pgAdmin4 with PostgreSQL

Now that you have everything ready let’s spin the containers and check if they are working as expected.

On the directory containing docker-compose.yml file, run the following command.

docker-compose up -d

Docker - Connect Postgres dpage pgAdmin4 With Docker Compose

Your containers are running, and you can confirm this on your Docker Desktop:

Docker - Connect Postgres dpage pgAdmin4 With Docker Compose

Now open http://localhost:5050/ on the browser. You should be served the following dpage pgAdmin4 UI:

Docker - Connect Postgres dpage pgAdmin4 With Docker Compose

To access PostgreSQL, go ahead and log in using:

  • Username as PGADMIN_DEFAULT_EMAIL value
  • Password as PGADMIN_DEFAULT_PASSWORD value

Docker - Connect Postgres dpage pgAdmin4 With Docker Compose

To access Your PostgreSQL database, you need to create a server as follows:

Navigate to Add New Server:

Docker - Connect Postgres dpage pgAdmin4 With Docker Compose

Provide a name for your Server:

Docker - Connect Postgres dpage pgAdmin4 With Docker Compose

Navigate to the Connection tab and add:

The hostname should be the service name running your PostgreSQL container. In this case, I use db

  • Username should be the value of POSTGRES_USER
  • Password as the value of POSTGRES_PASSWORD

Docker - Connect Postgres dpage pgAdmin4 With Docker Compose

Once you have them ready, Click SAVE. You should now access PostgreSQL. And as you can see, the test database specified on POSTGRES_DB is ready. This means your Postgres dpage pgAdmin4 is correctly working with Docker Compose.

Docker - Connect Postgres dpage pgAdmin4 With Docker Compose

Conclusion

In this guide, you learned:

  • How to run PostgreSQL on Docker.
  • Execute dpage pgAdmin4 with Docker Compose.
  • Connect pgAdmin4 to Postgres using Docker.
  • How to use pgadmin_default_email and access PostgreSQL.

I hope you found this helpful!

Guide to Run PostgreSQL dpage pgAdmin4 with Docker Compose

Written By:

Joseph Chege