Introduction

PostgreSQL is an open source object-relational database system. Its psql client runs SQL and meta-commands from a terminal.

Connecting

psql                  # default database, default user
sudo -u postgres psql # as the postgres role

psql commands

CommandDescription
\duList roles
\lList databases
\dtList tables
\d <table>Describe a table
\d+ <table>Describe with sizes
\c <database>Connect to a database
\qQuit

Creating a database

createdb <database>

Dropping a database

dropdb <database>

Running a query

psql -Atc "SELECT * FROM <table>"   # scripting
psql --no-align --tuples-only \
  --command="SELECT * FROM <table>" # same
0 Comments for this cheatsheet. Write yours!