psql is a terminal-based front-end to PostgreSQL. It enables you to type in queries interactively, issue them to PostgreSQL, and see the query results.
Once you are ssh-ed into a lab machine, you can check the version of psql installed with:
psql --version
On a Hunter lab machine, this outputs:
psql (PostgreSQL) 10.18 (Ubuntu 10.18-0ubuntu0.18.04.1)
The docs for psql 10 can be found here or via command line by typing:
man psql
Here are some commonly used psql commands, once you have connected to the PostgreSQL database:
\du
: list all the users (e.g. student) and their roles and permissions.
\list
or \l
: list all databases in PostgreSQL.
\c databasename
: connect to a specific database. (e.g. \c employees
)
\dt
: once connected to a specific database, this will list all tables in the current database.
\d tablename
: to describe the table and print the table schema as well as column level data type.
\e
: to run commands from text editor instead of typing in-line on the command line. This will come in handy as our SQL queries get long and complex. The lab machines have emacs, nano, and vim installed. By default, \e
will open vim. If you are new to vim, here is a tutorial to get started.
\?
: list all available psql commands. (to exit out of the list, type q
+ ENTER
)
\quit
or \q
: exit (quit) out of PostgreSQL and back to the lab machine.