The PostgreSQL Book

Notes on how to use the database

How to install and connect to postgres using docker

// (1) Download the latest version of PostgresSQL from the Docker hub
debugme@desktop ~> docker pull postgres:latest

// (2) Start up a container based on the downloaded PostgreSQL image
debugme@desktop ~> sudo mkdir -p /var/lib/postgresql/data
debugme@desktop ~> docker run --name mypostgresbox -e POSTGRES_PASSWORD=password -p 5432:5432 -v pgdata:/var/lib/postgresql/data -d postgres

// (3) Check container logs to see if there were any issues
debugme@desktop ~> docker logs mypostgresbox 2>&1

// (4) Log into PostgreSQL on the container
debugme@desktop ~> docker exec -it mypostgresbox psql -U postgres

How to install and connect to postgres using docker-compose

// (1) define your docker-compose.yaml file
version: '3'
services:
  database:
    image: postgres
    environment:
      - POSTGRES_PASSWORD=password
      
// (2) start-up your containers
docker-compose up --build -d       
      
// (3) connect to psql shell inside container
docker exec -it postgres_database_1 psql -h database -U postgres

How to connect to a database directly

How to connect to a database indirectly

How to list all databases

How to list all tables in active database

How to toggle between expanded display

How to select all columns from a table

How to select specific columns from a table

How to find out what columns are in a table (Does not work if in expanded display mode)

How to get a list of unique values for a column

How to get a count of the number of rows in a table

How to get a count of the number of distinct values in a column

How to get rows that satisfy a query

How to order rows by one column and then within that, another column

How to limit the number of rows

How to find all rows within an inclusive-inclusive range

How to find all rows whose value is in a list of values you are interested in

How to find all rows that match a pattern (_ for a single characters, % for zero or more characters)

How to use some common aggregate functions (aggregate means multiple inputs, single output)

How to use group by (Note: group by can only be used with non-aggregate values)

How to use having (Note: having can only be used with aggregate values)

How to rename columns (Note: aliases can only be referenced in the select clause)

How to join columns between tables that have values in common (inner join)

How to join all columns between tables unconditionally (full outer join)

How to join columns between tables without values in common (full outer join minus inner join)

How to join columns in left table whose values are also in right table (left outer join)

How to join columns in left table whose values are not in right table (left outer join minus inner join)

How to get various time and date related information

How to extract and format different parts of a timestamp

How to perform a subquery

How to perform self-join

How to create a table

How to insert rows into a table

How to update values in a table

How to delete rows from a table

How to alter a table

How to delete a column from a table

How to add check constraints

How to use case statements

How to use coalesce function

How to use cast function and cast operator

How to create a view

Last updated