Docker

Setup Postgres Using Docker Image on Windows

Docker is a frequently used open-source application development platform. While developing the project or application, the developers think about project management. For this purpose, they typically utilize the DBMS to store and manage applications. PostgreSQL is well established and maintained RDBMS that usually uses SQL queries to save and manage project data.

This blog will illustrate the method to set up Postgres using Docker in Windows.

How to Install and Connect Postgres Using Docker on Windows?

The Docker platform supports numerous tools for project development, and PostgreSql is one of them. To install and connect PostgreSQL, first, pull the “postgres” Docker official image, and create and run the container through the pulled image. After that, install “pgAdmin4” and connect the Postgres Docker container to pgAdmin4.

For the proper guideline, utilize the listed steps.

Step 1: Pull “postgres” Docker Official Image

First, pull the “postgres” official Postgres image from the Docker Hub registry:

> docker pull postgres

Step 2: Create and Run Container

Next, execute the pulled “postgres” image to create and start the Docker container:

> docker run --name postgresql -e POSTGRES_USER=root -e POSTGRES_PASSWORD=root -p 5432:5432 -v /data:/var/lib/postgresql/data -d postgres

In the above command:

  • –name” is utilized to define the container’s name.
  • -e” is used to specify the environment variables, such as username and password.
  • -p” allocated the exposing port for the container.
  • -v” defines or allocates the volume or file system for the container.
  • -d” runs the container as backend services or in detached mode.
  • postgres” is a Docker image used to install Postgres:

Step 3: Install “pgAdmin4” Using Docker Image

In the next step, pull the Docker image to install pgAdmin4. The “pgAdmin4” is a GUI version of PostgreSQL. For doing so, we have pulled the “dpage/pgadmin4:latest” image:

> docker pull dpage/pgadmin4:latest

Step 4: Access PgAdmin4

Now, run the pulled image to create and execute the container to access pgAdmin4. To run the “dpage/pgadmin4” image in the container, set the email and password as specified in the below command:

> docker run --name my-pgadmin -p 82:80 -e '[email protected]' -e 'PGADMIN_DEFAULT_PASSWORD=root'-d dpage/pgadmin4

Step 5: Provide User Credentials for PgAdmin4

Next, access the pgAdmin4 on the specified port of localhost. For instance, we visited “localhost:82”. Provide the login credential that you have set in the previous step:

It can be observed that we have successfully installed and accessed the pgAdmin4 using Docker:

Step 6: Inspect the Postgres Container

Inspect the Postgres container through the “docker inspect <container-name>” command. In our scenario, we have inspected the “postgresql” container that was created by the “postgres” image:

> docker inspect postgresql

Note the environment variable (username/password) you have set while creating the container and the host or ip address of the container are shown below:

Step 7: Connect pgAdmin4 to Docker Postgres Instance

Now, connect the pgAdmin4 to the Docker Postgres container “postgresql”. For this purpose, first, add a new server for Postgres by right-clicking on the “Server”, then click on “Register” to register the new server:

Set the name for the server. For instance, we have utilized “PostgreSQL”:

After that, move to the “Connection” menu, and place the Postgres container ip address, port, username, and password. Then, hit the “Save” button:

Here, you can see we have connected the postgres container instance with pgAdmin4 to access and use PostgreSQL:

That’s all! We have explained how to set up Postgres using Docker image on Windows.

Conclusion

To install the PostgreSQL database, pull the Docker image “postgres” from the official Docker Hub registry. Then, execute the image to containerize and install PostgreSQL. After that, install the pgAdmin4 through the “dpage/pgadmin4:latest” image and connect the Postgres container instance to pgAdmin4. This write-up has illustrated how to set up Postgres using a Docker image on Windows.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.