This install is pretty different from previous versions of psql install instructions for windows. This install uses the WSL and Ubuntu shell.
We are installing this through the Ubuntu command line which is different from the other Ubuntu install instructions because those instructions use Ubuntu's GUI, which we don't have access to here.
NOTE: Since this is a service that is running on Ubuntu, you should be in the the Ubuntu file system when running these commands!
- Open the powerShell and type wsl, 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/ xenial-pgdg main
- If the version of Ubuntu is zesty, then change the last part of the line above from
xenial-
tozesty-
.
- 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 3 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 lastest 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.
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. Note that it will not show when you are typing, but it is still registering your key-strokes.
- Close the window and reopen and PowerShell WSL.
After your first install, and each time you restart your machince 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 you 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 typeing out sudo service postgres start
and sudo -u postgrest psql
all the time can be tedious, I would recommend you set a couple aliases for this.
- Open a WSL windows 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 type just typepgstart
to start running the psql service, andrunpg
to quickly log into the psql prompt.
Note that you can change pgstart
and runpg
to what ever you want, but just be careful you don't overwrite something that postgres might use!