We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Postgres

PostgreSQL is a production-ready, open-source database. It's a great choice for many web applications, and as a back-end engineer, it might be the single most important database to be familiar with.

How Does PostgreSQL Work?

Postgres, like most other database technologies, is itself a server. It listens for requests on a port (Postgres' default is :5432), and responds to those requests. To interact with Postgres, first you will install the server and start it. Then, you can connect to it using a client like psql or PGAdmin.

  1. macOS with brew:

    brew install postgresql@15
    

    Linux / WSL (Debian). Here are the docs from Microsoft, but simply:

    sudo apt update
    sudo apt install postgresql postgresql-contrib
    
  2. psql --version
    
  3. sudo passwd postgres
    

    Enter a password, and be sure you won't forget it. You can just use something easy like postgres.

    • Mac: brew services start postgresql@15
    • Linux: sudo service postgresql start
  4. Enter the psql shell:

    • Mac: psql postgres
    • Linux: sudo -u postgres psql

    You should see a new prompt that looks like this:

    postgres=#
    
  5. CREATE DATABASE gator;
    
  6. \c gator
    

    You should see a new prompt that looks like this:

    gator=#
    
  7. ALTER USER postgres PASSWORD 'postgres';
    

    For simplicity, I used postgres as the password. Before, we altered the system user's password, now we're altering the database user's password.

  8. From here you can run SQL queries against the gator database. For example, to see the version of Postgres you're running, you can run:

    SELECT version();
    

    You can type exit to leave the psql shell.

Submit the CLI tests.