Postgres Cheat Sheet Of Common SQL Queries

With Terminal Commands and a Docker Bonus


By Mike Cronin

SELECT sql_cheat_sheetFROM my_brain;12345

Postgres is my SQL flavor of choice, but I’m tired of Googling syntax, so here are all the commands I use with any regularity. I’m not going to explain anything so this will be short and sweet, but I’ve named everything correctly so looking things up should be easy.

Setup with docker is at the end.

Postgres terminal commands


Comments


Create, Drop database, table


I'm going to use an auto incrementing primary key, and so should you.


Delete all data without dropping table


Add, Modify, Delete a column


Insert values


Selections


Order By


Count, In, Limit


Update Values


Delete Values


Foreign Keys and Joins


Here are the 4 types of joins you can do:


Subqueries


Create an Index


Analyze Query Performance


Backup/Restore Database


Docker Compose quickstart

If you just want a little container to play around with make a docker-compose.yml file and then copy this in:


That will create a default db with a user and database of "postgres" and you can start playing with it by running these from the command line:


I like this little container setup when I'm just goofing around and testing stuff (like writing this article). Hope these commands help!

Happy coding everyone,

Mike