Skip to content

Instantly share code, notes, and snippets.

@rgarner
Last active August 29, 2015 14:02
Show Gist options
  • Save rgarner/ab302a2c87f2587d6cd0 to your computer and use it in GitHub Desktop.
Save rgarner/ab302a2c87f2587d6cd0 to your computer and use it in GitHub Desktop.
Developer's new-to-PSQL cheat sheet

PSQL cheat sheet

Starting it (examples)

Command (shell) Does this
psql Starts psql connected to your own user's database
psql -d <database> Starts psql connected to the given <database>
cat some.sql | psql -d <database> Runs the SQL script in some.sql on <database>

Getting out

An arcane way of quitting is the hallmark of a wonderful tool

Command Does this
\q Quit

Basic help

Your cheatsheet, it is not complete

Command Does this
\? Show PSQL commands
\h Show SQL commands
\h <command> Get help for a SQL command

Common things

Why are the first things a dev wants to do so hard to find?

Command Does this
\l List all databases
\connect <database> Change to another database (shortcut \c)
\dt Show all tables in the current database
\d <table> Describe a table's columns
\di Show all indices in the current database

Editing queries

Set your EDITOR environmental variable. Then use \e to edit the last query you entered, or start a new one if you haven't run a query yet. When you save and quit your query will run if it parses. If not, your inadequacies will be pointed out to you by a machine.

Awesome things that should be your default

Command Does this
\x auto Displays query results so as not to overflow the screen when it can (will use a dictionary style for a single row)
\timing Toggle query timings on (off by default)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment