Skip to content

Instantly share code, notes, and snippets.

@luisjunco
Last active December 2, 2024 09:06
Show Gist options
  • Save luisjunco/26ffe513a9a40e28c4862234ce274b42 to your computer and use it in GitHub Desktop.
Save luisjunco/26ffe513a9a40e28c4862234ce274b42 to your computer and use it in GitHub Desktop.

Practice: PostgreSQL setup & first steps

Iteration 1 - Setup:

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.


Iteration 2 - Basic commands:

Once you have PostgreSQL installed, follow these steps:

  • Connect to PostgreSQL, using this command → psql (or psql -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)

Bonus - Explore some SQL Statements:

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

postgresql cheat-sheet


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:

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