Skip to content

psql Cheatsheet

Jason Lewis edited this page Aug 26, 2024 · 5 revisions

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>

Types

List Types

\dT

Get Details of Composite Type

\d <type>

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;

Clone this wiki locally