How to Install PostgreSQL on WSL Ubuntu 22.04

This guide covers how to install PostgreSQL on WSL Ubuntu 22.04, create a database and user, and verify everything works with a Python connection test. The examples are run on WSL2 Ubuntu in Windows, but they work the same on any Ubuntu 22.04 system.

Ubuntu 22.04’s default repositories include PostgreSQL 14. If you need a newer version (16 or 17), I’ll show you how to add the official PostgreSQL repository as well.

Prerequisites

Install PostgreSQL on WSL Ubuntu

Step 1: Update Package Lists

sudo apt update

Step 2: Install PostgreSQL

sudo apt install postgresql postgresql-contrib -y

The postgresql-contrib package includes extra utilities and extensions like pg_stat_statements and uuid-ossp that you’ll probably want later. The installation automatically creates a postgres system user.

Step 3: Start PostgreSQL

sudo service postgresql start

On WSL2, use sudo service instead of systemctl. If your WSL2 distribution has systemd enabled, systemctl will also work — but service is the safe default that works everywhere.

One thing to know: WSL doesn’t auto-start services on boot. Every time you open a new WSL session, you’ll need to run sudo service postgresql start again. You can add it to your ~/.bashrc if you want it to start automatically:

echo 'sudo service postgresql start' >> ~/.bashrc

Step 4: Verify It’s Running

sudo service postgresql status

You should see something like 14/main (port 5432): online.

Create a Database and User

First, switch to the postgres system user and open the PostgreSQL shell:

sudo -i -u postgres
psql

Now create a database, a user with a password, and grant permissions:

CREATE DATABASE mydb;
CREATE USER myuser WITH ENCRYPTED PASSWORD 'your_password_here';
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;

A few settings that are good practice for new roles — especially if you’re working with Django or similar frameworks:

ALTER ROLE myuser SET client_encoding TO 'utf8';
ALTER ROLE myuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE myuser SET timezone TO 'UTC';

Useful psql Commands

Command What It Does
\l List all databases
\du List all users/roles
\dt List tables in current database
\c mydb Connect to a specific database
\q Exit psql

To exit back to your regular user, type \q to leave psql, then exit to leave the postgres shell.

Installing a Newer PostgreSQL Version (Optional)

Ubuntu 22.04 ships with PostgreSQL 14. If you need version 16 or 17, add the official PostgreSQL APT repository:

sudo apt install -y curl ca-certificates
sudo install -d /usr/share/postgresql-common/pgdg
sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc
echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
sudo apt update

Then install the version you want:

# Install PostgreSQL 17
sudo apt install -y postgresql-17

# Or PostgreSQL 16
sudo apt install -y postgresql-16

If you already have PostgreSQL 14 installed, both versions will run side by side on different ports. Check which clusters are running with:

pg_lsclusters

Test the Connection with Python

A quick way to verify your setup is working — connect to the database from a Python script using psycopg2.

Install the required packages. If you manage Python versions with pyenv, see How to Install and manage Python Versions on WSL Ubuntu.

sudo apt-get install -y libpq-dev
pip3 install psycopg2-binary

The libpq-dev package provides the PostgreSQL client library headers that psycopg2 needs.

Create a test script test_pg.py:

import psycopg2
from psycopg2 import OperationalError

def test_connection():
    try:
        conn = psycopg2.connect(
            host="localhost",
            port=5432,
            database="mydb",
            user="myuser",
            password="your_password_here"
        )
        print("Connection successful")
        conn.close()
    except OperationalError as e:
        print(f"Connection failed: {e}")

if __name__ == "__main__":
    test_connection()
python3 test_pg.py

If you see Connection successful, you’re good. If you get an authentication error, double-check the username, password, and database name you created earlier.

Conclusion

PostgreSQL is up and running on your WSL Ubuntu. From here, you can connect to it from your applications, use it as a local dev database, or start learning SQL.

If you want a GUI to manage your databases, check out How to Access PostgreSQL in Ubuntu WSL with pgAdmin 4. And if you’re ready to write queries, How to Use SQL Joins and Conditional Statements in PostgreSQL is a good next step.