How to Run PostgreSQL and pgAdmin Using Docker | by Mahbub Zaman | Towards Data Science

You can use pgAdmin as an alternate solution if you don’t like managing your database using the command-line interface. It’s a web-based front-end for the PostgreSQL database server.
We will be using Docker for our setup since we don’t want to worry about environment management. By using Docker, we don’t have to worry about the installation of the PostgreSQL or pgAdmin. Moreover, you can use Docker to run this project on macOS, Windows, and Linux distributions.
From this post, you’ll learn how to connect pgAdmin to a PostgreSQL database server using Docker.
First, you will need to install Docker. I’ll use macOS for demonstration purposes.
Method 1
We will use a Docker compose file for our first method, and we need to put the docker-compose.yml inside a folder. In this case, the name of the folder is pgAdmin. Let’s break down the individual ingredients of the docker-compose.yml file.
version: '3.8'services:  db:    container_name: pg_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: root    ports:      - "5050:80"
First, we are using a version tag to define the Compose file format, which is 3.8. There are other file formats — 1, 2, 2.x, and 3.x. You can read more from Docker’s documentation.
Then we have a hash called services. Inside this, we have to define the services we want to use for our application. For our application, we have two services, db, and pgadmin.
We have used the tag container_name for both services to change the default container name to pg_container and pgadmin4_container for our convenience.
The second tag image is used to define the Docker image for the db and pgadmin service. To make our setup process quick and easy, we will use the pre-built official image of PostgreSQL and pgAdmin.
In my previous Docker posts, I’ve talked about the use of restart, environment, and ports tags. Have a look at the following post, where you can learn these tags.
towardsdatascience.com
Now run the following command from the same directory where the docker-compose.yml file is located.
cd pgAdmindocker compose up
The command docker compose up starts and runs the entire app. Congratulations!, you are successfully running a PostgreSQL database and pgadmin4 on your machine using 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 admin@admin.com for the email address and root as the password to log in.
Click Servers > Create > Server to create a new server.
Select the General tab. For the name field, use any name. In this case, I’ll use my_db. Now move to the Connection tab. To get the value for of Host, run the following commands.
docker ps
The command ps will show you brief information about all the running containers.
Please read the Update section first. Now we can grab the container id for the PostgreSQL container.
docker inspect fcc97e066cc8 | grep IPAddress
The command inspect shows detailed information about a running container. Also, we are using the pipeline and grep command to extract IPAddress information.
Finally, we have the value of Host, which in this case is 192.168.80.2. Use the value root for Username and root for Password, also tick the Save password? box if you don’t want to type the password every time you log in to pgadmin4.
Update [16 April 2021]
I received an email this morning from Dosenwerfer, that the person had some issues when running the services again. Because it changed the IP address of the PostgreSQL container, and you have to set up the configuration again. The recommended solution is to use the container name. Because the container name is identical to the hostname, you can read more from here. So our current configuration would be the following.
You can find the PostgreSQL database server’s container name using the docker ps command and grab the name from the NAMES column. In this post, we have explicitly named the container in the docker-compose.yml file, so you can refer to that as well. Many thanks to Dosenwerfer.
For the second method, you can connect a pgAdmin docker container to a running PostgreSQL container. It is helpful when you don’t have a pgAdmin service in your docker-compose.yml file. Have a look at the following post where you can learn this method.
towardsdatascience.com
If you want to import some data for testing purposes, you can use the SQL query I’ve already prepared. Click Servers > my_db > Databases > test_db > Schemas > Tables. Right-click on tables and select Query tool. Copy-paste the SQL query from my GitHub repository to Query Editor and click the play button. This action will create two tables called students and marks with some test data.
Database management via a command-line interface can be nerve-racking. To solve this issue, we can use a tool with an interface. The pgAdmin solves this problem. Moreover, Docker makes the entire process smoother. Also, you can use the test data I’ve provided to play around with PostgreSQL queries. I hope this will help you get started with PostgreSQL, pgAdmin, and Docker. Happy coding!