This doc explains how to install PostgreSQL 10 for Windows WSL
We are installing this through the Ubuntu command line since we want this software to run in the Linux environment. You can check out the PostgreSQL Linux install docs here.
- Open a terminal (the Ubuntu app) and then go to the root of the Ubuntu Subsystem by typing
cd ~
. - Type
sudo nano ../../etc/apt/sources.list
. This will open a file on Ubuntu using the Nano editor. - At the bottom of this file, paste in this line
deb http://apt.postgresql.org/pub/repos/apt/ <YOUR DISTRO VERSION>-pgdg main
- Change the last part of the line above from
<YOUR DISTRO VERSION>
to whichever version of Ubuntu you are running. - 18.04.X:
bionic-
16.04.X:xenial-
14.04.X:trusty-
- When that's done, press
Ctrl + X
together to close the file, and pressy
when prompted to save your changes, andenter
to finally close. - Next, copy these 2 lines and paste them into your terminal:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
This will add postgresql-10 to your repositories so you can install the latest version of PostgreSQL. Press enter
when the last line pops up.
- After the update is complete, enter in this line
sudo apt-get install postgresql-10
and pressy
when prompted. If the process aborts automatically, you may have to restart your terminal.
postgresql-10 runs under the user postgres
. We need to give this user a password so that postgres can allow this user to connect to the database.
- To set the password for postgres, type
sudo passwd postgres
. - You will get a prompt to enter in your password. It will not show when you are typing, but it is still registering your key-strokes.
- Close and reopen the terminal.
After your first install, and each time you restart your machine you will have to also restart the postgres service, or else you will get a Is the server running?
error.
- To start the service, type
sudo service postgresql start
. - To conntect to postgres, type
sudo -u postgres psql
.
You should get a prompt asking for your password. If this doesn't work, then you can try the second option listed below.
- Switch users to postgres by typing
su - postgres
. - Type
psql
.
When this is successful you will see the command line change to look like this postgres=#
Since typing out sudo service postgres start
and sudo -u postgrest psql
all the time can be tedious, I would recommend you set up a couple aliases for this.
- Open a terminal and type
cd ~
, then typesudo nano .profile
. This will open your.profile
which controls what your terminal does and looks like. - Add these two lines next to any other aliases that you have:
alias pgstart='sudo service postgresql start'
alias runpg='sudo -u postgres psql'
This will allow you to typepgstart
to start running the psql service, andrunpg
to quickly log into the psql prompt. This is an example of a Quality of Life enhancement, something that makes your life easier and faster as a developer.
You can change pgstart
and runpg
to what ever you want, but just be careful you don't overwrite something that postgres might use.