- Docs: https://www.postgresql.org/docs/10/static/index.html
- Curated list of resources: https://github.com/dhamaniasad/awesome-postgres
- pgAdmin 4: https://www.pgadmin.org/download/
Start the psql shell with psql postgres postgres (psql dbname username)`
- Docs: https://www.postgresql.org/docs/current/static/app-psql.html
- Cheat Sheet: https://gist.github.com/Kartones/dd3ff5ec5ea238d4c546
- Show help
\? - List databases
\l - List users
\du - Quit interactive session
\qorCTRL+D
- Create user: https://www.postgresql.org/docs/current/static/sql-createrole.html
- [psql]
CREATE ROLE app_user WITH LOGIN PASSWORD 'app_password'; - Tip: You can setup a .pgpass password file to automate login (i.e., that you do not need to retype the password).
- [psql]
- Change password: https://www.postgresql.org/docs/current/static/sql-alterrole.html
- [psql]
ALTER ROLE app_user WITH PASSWORD 'new_password';
- [psql]
- Create database: https://www.postgresql.org/docs/current/static/sql-createdatabase.html
- [psql]
CREATE DATABASE app_database WITH OWNER app_user ENCODING 'UTF8';
- [psql]
- Drop database: https://www.postgresql.org/docs/current/static/sql-dropdatabase.html
- [psql]
DROP DATABASE app_database;
- [psql]
Two options to execute SQL code:
- [shell] psql −U app_user −f /full/path/to/create_schema.sql app_database
- [psql]
\i /full/path/to/create_schema.sql
Steps to import the MONDIAL database dump:
-
Create a user/role called
mondialwith passwordmondialand a database calledmondial_dbowned by the mondial user# [bash] psql postgres postgres # [psql] CREATE USER mondial WITH PASSWORD 'mondial'; CREATE DATABASE mondial_db WITH OWNER mondial; # Quit interactive session \q -
Download the schema (
create_schema.sql) and input values (insert_inputs.sql) from Canvas -
Change directory where the downloaded files are
# MacOS/Linux uses slashes / for paths cd $HOME/Downloads # Windows uses backslashes \ for paths cd %USERPROFILE%\Downloads
-
Import the schema and values
psql −q −f create_schema.sql mondial_db mondial psql −q −f insert_inputs.sql mondial_db mondial
-
Connect to the mondial database with the terminal client psql
psql mondial_db mondial
-
Verify whether the following query yields
1318SELECT COUNT(iatacode) FROM airport;
Tip: You can use dropdb or sql-dropdatabase DROP DATABASE mondial; to destroy your database and start from scratch.