Install PostgreSQL on WSL

Raymond Raymond event 2021-02-01 visibility 7,844
more_vert

Prerequisites

Before installing PostgreSQL on WSL, please ensure you have WSL enabled on your Windows 10 system. 

Follow Install Windows Subsystem for Linux on a Non-System Drive to install WSL on a non-C drive. 

This tutorial provides steps to install PostgreSQL on WSL Debian distro. You can follow similar steps to install it in other Linux distros. 

Step by step guide

  1. Open Debian distro through WSL command:
    wsl -d Debian
  2. Update Debian packages:
    sudo apt update

    You need to type user password.

  3. Install PostgreSQL and -contrib package using the following command:
    sudo apt install postgresql postgresql-contrib

    Type Y to continue when asked.

  4. Wait until the installation is completed. 
  5. Verify PostgreSQL version:
    $ psql --version
    psql (PostgreSQL) 9.6.20
  6. The default admin user is postgres. We need to setup a password for it. Use the following command to setup the password:
    sudo passwd postgres

    Enter the password. I'm setting the password as 'password'.

    $ sudo passwd postgres
    Enter new UNIX password:
    Retype new UNIX password:
    passwd: password updated successfully

Test PostgreSQL

  1. Start PostgreSQL service using the following commands:
    $ sudo service postgresql start
    # Or
    sudo /etc/init.d/postgresql start
  2. Check the status of the service:
    $ sudo service postgresql status
    # Or
    sudo /etc/init.d/postgresql status
    The output looks like the following:
    9.6/main (port 5432): online
  3. Try PostgresSQL prompt using the following command:
    sudo -u postgres psql

    The above command sudo the default user postgres and then run psql command:

    $ sudo -u postgres psql
    
    psql (9.6.20)
    
    Type "help" for help.
    
    
    
    postgres=#
  4. Check databases by typing \l in psql command window:
    \l

    The output looks like the following screenshot:

    20210201115212-image.png

  5. Type \q to exit the CLI.
  6. Stop the service using the following command:
    $ sudo service postgresql stop
    # Or
    sudo /etc/init.d/postgresql stop
More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts