DEV Community

Cover image for Postgres on Debian in WSL
Guy Bowerman
Guy Bowerman

Posted on • Edited on

Postgres on Debian in WSL

This article covers the basics of installing and running Postgres on Debian running in Windows subsystem for Linux (WSL).

WSL makes for a great development environment if you're running Windows and want to use Linux locally. You could of course install Postgres directly on Windows but you may have various reasons for wanting a more authentic Linux environment.

Which Linux distro?

You can choose from many Linux distributions to run on WSL (run wsl -l -o to see a list or import any distro using a TAR file). I like to use Debian as it's a popular, stable, general purpose distro with good support. You can install Debian to WSL directly from the Microsoft Store, and use Windows Terminal to log on to it.

Which Postgres version?

Ignore this step if you want want the default Postgres version that comes with your distro packages.

See the postgresql.org Apt/FAQ for guidance on how to install the latest PostgreSQL version (17 at time of writing). E.g., edit /etc/apt/sources.list.d/pgdg.list to point to version 17:

deb http://apt.postgresql.org/pub/repos/apt/ bookworm-pgdg main 17 
Enter fullscreen mode Exit fullscreen mode

Install Postgres

Once you're connected to the Debian command line, make sure you're up to date with the latest patches:

sudo apt update
sudo apt upgrade
sudo apt autoremove # to free up space if needed
Enter fullscreen mode Exit fullscreen mode

Then install with:

sudo apt install postgresql postgresql-contrib
Enter fullscreen mode Exit fullscreen mode

See Install PostgreSQL in the WSL docs for more details.

Set a password for the postgres user with sudo passwd postgres and then start the service:

sudo passwd postgres
sudo service postgresql start
sudo systemctl enable postgresql # to start service automatically
Enter fullscreen mode Exit fullscreen mode

You can then enter the PostgreSQL shell with:

sudo -u postgres psql
Enter fullscreen mode Exit fullscreen mode

..and from there run SQL commands, e.g. create PostgreSQL users with CREATE USER

Or run SQL commands directly, e.g.

sudo -i -u postgres psql < create_db.sql
sudo -i -u postgre psql -d mydb < create_schema.sql
# list databases
sudo -i postgres psql --list
Enter fullscreen mode Exit fullscreen mode

Connecting with pgAdmin

You can install pgAdmin on Windows and connect the GUI Admin/developer tool directly to your Postgres server running in WSL.

In pgAdmin, create a new server with the name of your choice, Host name/address: 127.0.0.1 and a valid PostgreSQL user/password.

A screen shot of a pgAdmin new server definition panel

A screen shot of pgAdmin server view
Once I've done my local development it's very straightforward to export a database from WSL to a Postgres cloud service or other production destination.

Top comments (0)