Follow the instructions in the unit "SQL | Introduction" to install PostgreSQL in your computer (you'll find instructions for each operating system: windows, mac, linux).
Important
Remember the credentials (username and password).
You will need those credentials, not just today but also any time you need to connect to your local db.
Once you have PostgreSQL installed, follow these steps:
- Connect to PostgreSQL, using this command →
psql
(orpsql -U username -d dbname
) - Get a list of users and their roles →
\du
- Get a list of existing databases, using this command →
\l
(or\list
) - Create a new database →
CREATE DATABASE animals_db;
- Get a list of existing databases →
\l
(you should be able to see your new database). - Connect to the db "animals_db" →
\c animals_db
- Get a list of tables in that database →
\dt
(for now, it would be empty, since we haven't created any table)
As a bonus, you can also explore other commands following the cheat sheet below.
For example, you can:
- Create a table (see the section 'Creating Tables')
- Add some data (see the section 'Inserting Data')
- Get all the entries an a table →
SELECT * FROM table_name
Warning
The databases with the names postgres
, template0
and template1
are system databases that are critical to the proper functioning of PostgreSQL. Do not delete or modify them !!
Attribution:
- Cheat sheet by https://learnsql.com/
- Full cheatsheet: https://learnsql.com/blog/postgresql-cheat-sheet/postgresql-cheat-sheet-a4.pdf