Ubuntu and WSL Python Installfest
Python 3
To install Python,
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.11
You can test the installation by running python3 --version
.
PostgreSQL
PostgreSQL is a popular and robust Relational Database Management System (RDBMS).
Check if PostgreSQL is already installed by running this command:
psql
If you entered PostgreSQL's Interactive Shell, you already have PostgreSQL installed. Enter \q to exit the shell.
If you already have PostgreSQL installed, note the version and inform an instructor if it's not at least version 10.
Otherwise, let's install it:
Install PostgreSQL
First, make sure your system packages are up to date. In the terminal:
sudo apt update
Next, run the following command to install PostgreSQL:
sudo apt install postgresql postgresql-contrib
You may be asked if you would like to continue. Type "Y" and hit the "Enter" key to continue installing.
Important!! You'll have to run this command on each restart to start up the PostgreSQL service:
sudo service postgresql start
The first time you start this service
You must assign a password to the default admin user to connect to a database. Do so with this command:
sudo passwd postgres
Enter a password when prompted. It is EXTREMELY IMPORTANT that you do not forget the password. Keep it stored in your password manager. Note that as you type, no characters will appear.
If the new password you have chosen is valid you will see this returned in the console:
passwd: password updated successfully
Upon seeing this, stop the PostgreSQL service with this command:
sudo service postgresql stop
Close all running terminal instances including any instances of VS Code that are currently open.
Open a new terminal instance. Start the PostgreSQL service again with:
sudo service postgresql start
We now need to create a database role that matches the name of your operating system user. Use this command (do not replace $USER
, this will automatically create a user that matches your Ubuntu OS username):
sudo -u postgres createuser $USER
And then follow that up by creating a database of the same name (again, do not replace $USER, this will automatically create a user that matches your Ubuntu OS username):
sudo -u postgres createdb $USER
Now run this command to switch to the postgres user:
sudo su - postgres
Finally, enter:
psql
You have now successfully entered the PostgreSQLshell as the postgres user!
Assigning Roles
We want the user we just made to be able to create databases so we need to assign it that role with this command in the postgres shell. YOU MUST replace <user>
with your Ubuntu username:
ALTER ROLE <user> WITH CREATEDB;
Don't forget the semicolon ^
You may want to also give this user super user permissions. This will keep you from having to ever use the postgres user again, but will strip a layer of protection from your postgres databases. This choice is ultimately yours, but if you do want to give your user account these permissions run this command. YOU MUST replace <user>
with your Ubuntu username:
ALTER ROLE <user> WITH SUPERUSER;
Again, don't forget the semicolon ^
Close the terminal session, and start a new one.
You should now be able to run this command and start the Postgres shell successfully:
psql
Let's test your ability to create databases. Run the following command in the postgres shell:
CREATE DATABASE apples;
As always, don't leave off the semicolon!
Next, enter the \l
command to see a list of all databases. If you can see the apples
database you just created, then success!
Let's go ahead and delete that database with the following command:
DROP DATABASE apples;
Enter \l
again to confirm the database was dropped.