PostgreSQL cheat sheet

\l (lowercase L)list all databases
\cuse database
\dtshow tables
\dTlist datatypes
\dflist functions
\dilist indexes
\dvlist views
\dulist users
\d+show all tables with descriptions
\d+ tableshow table definition or view definition with column descriptions

Altering a table

Renaming a table

> alter table coward rename to gallant;

Add a column to a table

> alter table gallant add column victories varchar(50);

Rename an existing column

> alter table gallant rename column victories to successes;

Backup and Restore

To backup

> pg_dump -U username -W database_name > db.sql

To restore

> psql -U username -W database < db.sql