Skip to content

Instantly share code, notes, and snippets.

@michaeltreat
Last active December 29, 2023 21:05
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save michaeltreat/40a2f444d8ff6c89af958733448da093 to your computer and use it in GitHub Desktop.
Save michaeltreat/40a2f444d8ff6c89af958733448da093 to your computer and use it in GitHub Desktop.
Postgres WSL install instructions

Install psql on WSL

Home

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!

Install

  1. Open the powerShell and type wsl, then go to the root of the Ubuntu Subsystem by typing cd ~.
  2. Type sudo nano ../../etc/apt/sources.list. This will open a file on Ubuntu using the Nano editor.
  3. 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- to zesty-.
  1. When that's done, press ctrl + x together to close the file, and press y when prompted to save your changes, and enter to finally close.
  2. 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.

  1. After the update is complete, enter in this line : sudo apt-get install postgresql-10 and press y when prompted.

Postgres User Setup

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.

  1. To set the password for postgres, type sudo passwd postgres.
  2. 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.
  3. Close the window and reopen and PowerShell WSL.

Using psql

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.

  1. To start the service, type sudo service postgresql start.
  2. 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:

  1. Switch users to postgres by typing su - postgres.
  2. Type psql.

When this is successful you will see the command line change to look like this: postgres=#

Tips:

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.

  1. Open a WSL windows and type cd ~, then type sudo nano .profile. This will open your .profile which controls what your terminal does and looks like.
  2. 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 type pgstart to start running the psql service, and runpg 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment