Running PostgreSQL in Docker Container on Windows 10

J Karelins
4 min readDec 16, 2020

--

PostgreSQL database in Docker Container on WSL2 (Windows machine)
PostgreSQL database in Docker Container on WSL2 (Windows machine)

I was using Windows & Ubuntu, switching between them for some time. It was happening just because, I was working for different companies, with various cyber security policies. But some time ago, I have jumped from Ubuntu 20.04 to Windows 10, and used Ubuntu in WSL (Windows Subsystem for Linux). I have found problems to install docker on WSL and use it to run for example Postgres DB for my projects. So I was using Docker for Windows 10 for some time. But I really do not like it, and using docker on Ubuntu Bash is faster and easier for me. So I decided to find way to install and use it!

Motivation

  1. Install docker on WSL on Windows machine
  2. Make it possible to use Windows app, for example HeidiSQL (I am using it, right now) to connect to DB running in WSL docker container

Install WSL2 to run Docker

Please note: We should use PowerShell in Administrator mode on Windows machine. And let’s assume you have Ubuntu installed on you Windows machine in WSL.

  1. Open PowerShell in admin mode. Open Start menu, type PowerShell, click with right mouse button and select Run as Administrator
  2. Let’s enable virtualization, it is going to be used by WSL2:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

3. Ok. Let’s convert your current Linux Distribution to WSL version 2. Please be patient. It will take some time!

wsl --set-version Ubuntu 2

4. Optional, to make WSL2 default:

wsl --set-default-version 2

Install Docker to WSL2

  1. Open your Ubuntu WSL2 Bash

2. Update your Linux repository

sudo apt-get update

3. Download Docker dependencies

sudo apt-get install apt-transport-https ca-certificates curl software-properties-common

4. Add Docker GPG key

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

5. Install Docker repository

sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable”

6. Update your Linux repository once again

sudo apt-get update

7. And install last Docker version

sudo apt-get install docker-ce

8. Let’s check Docker version, when it is installed on your WSL

docker --version

You should be able to see docker version:

Docker version installed: 20.10

Run Docker service and run PostgreSQL DB

  1. Let’s check Docker status
service docker status

or

sudo service docker status

You should see that docker service is not running:

Docker service is not running
Docker is not running

2. Let’s run Docker service!

sudo service docker start
service docker status

You should see that it runs now

Docker service is running now

3. Let’s start new PostgreSQL database running in Docker container.

sudo docker run -d — name=NewContainerName -p 5432:5432 -e POSTGRES_PASSWORD=complicatedPassword postgres

Note: We should make some changes first

NewContainerName — any container name you like

complicatedPassword — any password to access your database

4. To check that everything works fine:

sudo docker ps

It should show you new container in a list.

New PostgreSQL instance running in Docker container

Connect to this DB from your Windows machine

I am using HeidiSQL installed on my Windows 10 to access different databases. And I want to use same program to connect to this running PostgreSQL database running in Docker container in WSL2.

Note: Docker container is running, while your Ubuntu Bash window is opened. If you close Ubuntu, Docker service is going to be stopped.

  1. Open PowerShell
  2. Let’s get WSL2 IP address
wsl hostname -I

You should see 2 IP addresses

WSL IP address

We are interested in IP address highlighted in green color. This IP address is our WSL2 address.
3. Let’s test if we can connect to port 5432 of WSL. This is default port used by PostgreSQL DB.

Test-NetConnection -ComputerName YourIpAdress -Port 5432

You should change YourIpAdress, to IP you just got in second point.

It will take some time, but you should be able to see:

If TcpTestSucceeded is True — everything works fine.

If it shows False, you should fix a connection.

4. Now we can open HeidiSQL and connect to DB:

HeidiSQL connect to WSL2 PostgreSQL database

Select TCP/IP, version of library for PostgreSQL.

Input IP received in second point (WSL2 IP address).

User and Database name: postgres

Password you have used to create a Docker container with Postgres DB.

Conclusion

Now you should have running Docker container with PostgreSQL DB in it. And you can easily connect to it, using HeidiSQL graphic interface and make all actions you need, to change your database.

--

--

J Karelins
J Karelins

Written by J Karelins

Full Stack Web Developer now: WEB|JS|React|SPA. In the past: Hardware engineer. Know more: https://jkarelins.herokuapp.com/

Responses (3)