Set up PostgreSQL on WSL2 and Access with pgAdmin on Windows

Chloe Sun
4 min readJan 6, 2021

A super detailed beginner-friendly walk-through

Install PostgreSQL on WSL 2 and Access it through pgAdmin on Windows

Why install PostgreSQL on WSL 2?

Simply because the installation of PostgreSQL on Windows is kind of a pain, and I also use WSL 2 for other development tasks. You can visit my previous post on how to set up WSL 2.

I tried to download from the official website and install PostgreSQL 13.1 for Windows x86–64, and this was the error I couldn’t get rid of — Unable to write inside TEMP environment variable path.

Installation Error Screenshot

After googling, I found a post that might be helpful for others, but it didn’t solve my problem. I was told to download 3rd party distributions and find a standalone version of PostgreSQL, and this installer works for me. This is the workaround if you have experienced the same issue but still want to install it on Windows.

Install PostgreSQL on WSL 2

Here comes the WSL 2, much easier, and you can visit the Windows WSL documentation for more detail.

  1. Open your WSL terminal (ie. Ubuntu 18.04).
  2. Update your Ubuntu packages: sudo apt update
  3. Once the packages have updated, install PostgreSQL (and the -contrib package which has some helpful utilities) with: sudo apt install postgresql postgresql-contrib
  4. Confirm the installation and get the version number: psql --version

There are 3 commands you need to know once PostgreSQL is installed:

  • sudo service postgresql status for checking the status of your database.
  • sudo service postgresql start to start running your database.
  • sudo service postgresql stop to stop running your database.

The default admin user, postgres, needs a password assigned in order to connect to a database. To set a password:

  1. Enter the command: sudo passwd postgres
  2. You will get a prompt to enter your new password.
  3. Close and reopen…
Chloe Sun

<Architect | Software Developer | Metaverse Enthusiast> Discord: Architecting the Metaverse