PostgreSQL cheatsheet

macOS (homebrew)

Install

brew install postgresql

Migrate existing data from a previous major version of PostgreSQL

brew postgresql-upgrade-database

Restart postgresql after an upgrade

brew services restart postgresql

Direct, without a background service

pg_ctl -D /usr/local/var/postgres start

Console

$ psql #logs in to default database & default user
$ sudo -u <rolename:postgres> psql #logs in with a particular user

Commands

DescriptionCommand
Show roles\du
Show tables\dt
Show databases\l
Connect to a database\c <database>
Show columns of a table\d <table>\d+ <table>
Quit\q

Creating database

 $ createdb databasename