Categories
Docker

PostgreSQL and pgAdmin 4 Docker Compose

Database is very import for any web development project. If you are using the php stack for long time you would be aware of Xampp control panel where you automatically activate the MySQL and phpMyAdmin dashboard where you can use the database visually. Things are changed when Docker comes into play. Docker helps us to maintain our local copy with our production server. This is why we need to make docker containers and make it work if we don’t want to take production headache.

Docker Compose File

I got this docker compose file from this Gist on GitHub. Here is the complete docker compose file which you can run later.

version: "3.8"
services:
  db:
    container_name: postgres_container
    image: postgres
    restart: always
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: root
      POSTGRES_DB: test_db
    ports:
      - "5432:5432"
  pgadmin:
    container_name: pgadmin4_container
    image: dpage/pgadmin4
    restart: always
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@admin.com
      PGADMIN_DEFAULT_PASSWORD: admin
    ports:
      - "5050:80"Code language: YAML (yaml)

Run the Docker compose file

Now go to the command prompt where you saved your `docker-compose.yaml` file. Once you are there just run this command to run your container with PostgreSQL and pgAdmin 4 dashboard. pgAdmin would be used as phpMyAdmin alternative for PostgreSQL database. Here is the command which you need to give to run the docker containers .

docker-compose upCode language: Bash (bash)

Now that your docker containers are running your PostgreSQL database and the pgAdmin 4 you can simply list your containers via command prompt with this command.

docker container ls

This will give you list of all of your running containers. You need to copy the id of your PostgreSQL container.

docker container ls

Once you copy the id 25448252af54 in my case, go to the command prompt and enter the inspect command like this.

docker inspect 25448252af54

This will give you the IP Address of your PostgreSQL instance which you need to connect with it in your pgAdmin 4 dashboard. Here is how it looks.

Get IP Address of PostgreSQL instance
Docker Inspect

Login to pgAdmin

Now goto the web browser and navigate to http://localhost:5050. This will take you to the login page of your pgAdmin dashboard. Put the Email id which you provided in the docker compose file. Which in our case is admin@admin.com and the password which we provided in the docker-compose.yaml file which is admin. Once you are logged in, you need to make a new server with the settings provided. This settings will be according to this screenshot below.

Now that you have your pgAdmin connected to your server this will be the landing page of the dashboard.

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.