How to Create Postgres Docker Container in Windows [Step by Step Guide]

How to Create Postgres Docker Container in Windows [Step by Step Guide]

In this tutorial, we will learn about how to create Postgres Docker container in Windows using step by step guide. Postgres is an open-sources powerful relational database which is a popular choice for various applications. By containerizing it with Docker, you gain the advantages of isolation, scalability and ease of management. Whether you are building a local development environment or preparing your application for deployment, understanding how to create a Postgres docker container on Windows will streamline your workflow and enhance the efficiency of your development process.

 

Postgres Overview

Postgres (also known as PostgreSQL) is a powerful open source relational database management system which is known for its robust feature, extensibility and adherence to SQL standards. Postgres provides reliability, scalability and advanced capabilities which makes it a popular choice for a wide range of applications. Postgres is used in many fields like Web applications, business intelligence, geospatial applications and so on.  There are different ways to configure Postgres in any operating system for using it with any applications. For example, install Postgres using the installer or setup and configure he Postgres as Docker container. This tutorial will focus on setting up the Postgres as docker container.

 

How to Create Postgres Docker Container in Windows [Step by Step Guide]

Also read: How to setup Windows Subsystem for Linux [WSL] in Windows

Prerequisite

  • Docker desktop installed
  • Basic Understanding of Windows Command Prompt

 

Step-1: Check Status of Docker Desktop

In this step, check if docker desktop process is in running state. It is mandatory that the docker process is running in else the container creation will fail. There are multiple ways to check this. Use any of the below command to validate the running state of docker desktop.

Execute docker info command

C:\Users\linuxnasa.com> docker info
Client:
Version: 24.0.7
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.12.0-desktop.2
.........................................................

..........................................................
scout: Docker Scout (Docker Inc.)
Version: v1.2.0
Path: C:\Program Files\Docker\cli-plugins\docker-scout.exe

Server:
Containers: 1
Running: 1
Paused: 0
Stopped: 0
Images: 1
Server Version: 24.0.7
................................................

..................................................
Insecure Registries:
hubproxy.docker.internal:5555
127.0.0.0/8
Live Restore Enabled: false

 

You can also check the status of Docker using wsl command as shown below. 

C:\Users\linuxnasa.com> wsl -l -v
   NAME               STATE   VERSION
* docker-desktop      Running  2
  docker-desktop-data Running  2

 

Alternatively, you can also check the running status of Docker using ps command as shown below.

C:\Users\linuxnasa.com> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

 

NOTE:

I have used Windows Command Prompt (cmd) to execute all the commands given in this tutorial. You can also use Powershell but the syntax of commands may vary.

 

Step-2: Create Postgres Container

In this step, we will create the Postgres container using docker run command a shown below.

C:\Users\linuxnasa.com> docker run -d --rm --name local-pg -e POSTGRES_PASSWORD=postgres -p 5432:5432 postgres
Unable to find image 'postgres:latest' locally
latest: Pulling from library/postgres
af107e978371: Pull complete
85f7bca87921: Pull complete
.................................................

.................................................
08c5357f23b5: Pull complete
Digest: sha256:e2135391c55eb2ecabaaaeef4a9538bb8915c1980953fb6ce41a2d6d3e4b5695
Status: Downloaded newer image for postgres:latest
b723419051bb0c043aa69b9e11f3858acb09516dcac36c1138f0f9c965ff2a1d

Let us understand each part of the above command.

docker run: This initiates the command to run a Docker container.

-d: It runs the container in the background (detached mode), allowing you to use the terminal for other commands.

--rm: This flag removes the container automatically once it’s stopped, helping to keep the system clean by cleaning up resources after the container exits.

--name local-pg: It assigns the name “local-pg” to the container for easier identification.

-e POSTGRES_PASSWORD=postgres: This sets the environment variable POSTGRES_PASSWORD inside the container with the value “postgres.” It is the password for the default PostgreSQL user (postgres).

-p 5432:5432: This maps port 5432 on the host machine to port 5432 inside the container. PostgreSQL typically uses port 5432 for connections.

postgres: This specifies the Docker image to use, in this case, the official PostgreSQL image from the Docker Hub.

The overall purpose of this command is to start a PostgreSQL container with the specified configurations, including a defined password, and make it accessible on the host machine’s port 5432.

 

Step-3: List the Docker images

In this step, you can verify the Postgres docker image that is pulled and used to create the docker container using below command.

C:\Users\linuxnasa.com> docker images
REPOSITORY TAG    IMAGE ID      CREATED      SIZE
postgres   latest 398d34d3cc5e   2 weeks ago 425MB

 

Step-4: List the running Postgres Docker container

In this step, verify if the Postgres container “local-pg” is created successfully by using below command,

C:\Users\linuxnasa.com> docker ps
CONTAINER ID  IMAGE      COMMAND                CREATED           STATUS         PORTS                   NAMES
b723419051bb  postgres   "docker-entrypoint.s…  " 10 seconds ago  Up 8 seconds   0.0.0.0:5432->5432/tcp  local-pg

 

Step-5: Exec into the Postgres Docker Container

In this step, get inside the running container “local-pg” using exec command as shown below.

C:\Users\linuxnasa.com> docker exec -it local-pg bash
root@b723419051bb:/#

Next, once you successfully get inside the container, login as “postgres” user using below command.

root@b723419051bb:/# psql -U postgres
psql (16.1 (Debian 16.1-1.pgdg120+1))
Type "help" for help.
postgres=#

 

Step-6: List the default databases

In this step, you can verify the working of Postgres container by execution some of the command. For example, to list all the default Postgres databases, execute the command \l as shown below.

postgres=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | ICU Locale | ICU Rules | Access privileges

-----------+----------+----------+-----------------+------------+------------+------------+-----------+----------------------
-
postgres | postgres | UTF8 | libc | en_US.utf8 | en_US.utf8 | | |
template0 | postgres | UTF8 | libc | en_US.utf8 | en_US.utf8 | | | =c/postgres
+
| | | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | libc | en_US.utf8 | en_US.utf8 | | | =c/postgres
+
| | | | | | | | postgres=CTc/postgres

 

Step-7: Exit from Postgres Docker container

In this step, once all done you can exit from the container using exit command. It will bring you back to the Windows terminal  as shown below.

//Exit as postgres user
postgres=# exit
root@b723419051bb:/#

//Exit from container
root@b723419051bb:/# exit
exit
C:\Users\linuxnasa.com>

 

How to Delete Running Docker Container?

So far we know how to create a Docker container and login into the running container using exec command. It is equally important to stop and delete the container when it is no more required as part of clean up activity. To do so follow below steps.

List the running container. As you see there is currently one container running.

C:\Users\linuxnasa.com> docker ps
CONTAINER ID  IMAGE     COMMAND                 CREATED        STATUS       PORTS                  NAMES
b723419051bb  postgres  "docker-entrypoint.s…"  27 hours ago   Up 27 hours  0.0.0.0:5432->5432/tcp local-pg

Next, stop the running container. Prior to delete any docker container, it is required that you first stop the container. You can either use container name or container id with the docker stop command. I have used the container id as shown below.

C:\Users\linuxnasa.com> docker stop b723419051b
b723419051b

Next, once the container is in stopped state, you can now delete the container using docker rm command as shown below.

C:\Users\linuxnasa.com> docker rm b723419051b
b723419051b

 

Summary

We have learnt about creating Docker containers. We also learnt about how Docker images are pulled prior to creating the container. You can always refer the docker cheat sheet for referring to some basic docker commands while working with Docker.

Leave a Comment