psql Cheatsheet - gecko-8/devwiki GitHub Wiki

Up

Getting Help

Outside psql

psql --help

Inside psql

help General Help
\h SQL Command Help
\? psql Command Help

General

Turn On/Off Expanded Display

\x

List Available Functions

\df

Connection

psql -h <localhost or IP> -p <port> -U <username> -d <database name>
NOTE: Most parameters are optional
NOTE: Case is important!

Databases

List

\l

Connect

\c <database name>

Tables

List Tables and Other Items

\d

List Just Tables

\dt

List Table Columns

\d <table name>

Export

Export to CSV

\copy (<query>) TO '<path to file>' DELIMITER ',' CSV HEADER;
e.g. \copy (SELECT * FROM person) TO '/home/jasonl/Desktop/sql/results.csv' DELIMITER ',' CSV HEADER;

⚠️ **GitHub.com Fallback** ⚠️