PostgreSQL

Terms defined: client-server database, local database, privilege, role

Contributed by Konstantinos Kitsios.

Why PostgreSQL?

The Client-Server Model

Setup on MacOS: Direct Installer

  1. Download the latest version of PostgreSQL for macOS.
  2. Double-click the downloaded file to run the installer
    1. Click “Next”. You might see a warning message because it was downloaded from the internet. If so, click “Open” to continue.
    2. Leave the default installation folder and click “Next”.
    3. Leave the default components and click “Next”.
    4. Leave the default location (the database files will be stored there).
    5. You will be prompted to create a password; make sure to remember it. Click “Next”.
    6. Leave the default options (port and locale) and click “Next”.
    7. Click “Next”; if requested, provide your Mac OS password (the one you use for unlocking your laptop).
    8. Click “Finish”.
  3. Open PgAdmin from the Applications folder.
  4. In the top-left corner of PgAdmin, expand the “Servers” option by clicking on it.
  5. Click “PostgreSQL”; if prompted, use the password you created in step 2.4.
  6. You are now connected to PostgreSQL.

Setup on MacOS: With Options

  1. Download the latest version of Postgress.app.
  2. Open the downloaded .dmg file.
  3. Drag Postgres to the Applications folder.
  4. Open the Applications folder, then open Postgres. The first time you open the app, you might see a warning message because it was downloaded from the internet. If so, click “Open” to continue.
  5. Once the app is open, click “Initialize” to start your PostgreSQL session.
  6. You can see the existing databases (they have been created by default). Double click one and the terminal for running queries to that database will open
  7. You are now connected to PostgreSQL.

Setup on MacOS: Command-Line Tools

  1. Open a terminal window.
  2. Run open ~/.zshrc (or ~/.bashrc depending on your shell).
  3. Add the following line to the bottom of the file: export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin
  4. Run source ~/.zshrc.
  5. Verify installation by running psql.

Running Queries Using PgAdmin

select * from penguins limit 10;
select count(*) from penguins;

Running Queries in the Terminal

psql -d penguins

Privileges and Roles

Creating a Role and Granting Privileges

create role penguin_reader_writer
with login password 'reader_writer';
grant select, update on penguins
to penguin_reader_writer;

Verifying Privileges in PgAdmin

select * from penguins limit 10;
update penguins
set island = 'Antarctica'
where sex = 'MALE' and island = 'Torgersen';
delete from penguins
where island='Antarctica' and sex='MALE';
psql:delete_penguins.sql:2: ERROR:  permission denied for table penguins

Revoking Privileges

revoke update on penguins
from penguin_reader_writer;
update penguins
set island = 'Atlantis'
where sex = 'MALE' and island = 'Antarctica';
psql:update_penguins_again.sql:3: ERROR:  permission denied for table penguins