Installing postgres with brew + basics

Step 1: Install postgres

brew install postgres

Step 2: Start postgres server

brew services start postgresql

Step 3: create a database called book

createdb book

list your databases with: \l

Step 4: opened the database shell instance for “book”

psql book

list your tables with: \dt

Step 5: create a table with fields for “body”, “date” and “code”

create table dids (

body text, date timestamp, code text);

inspect your table schema with: \d <table_name>

Step 6: add some rows

Remember: SINGLE QUOTES ONLY and remember the semi colon

INSERT INTO dids (body, date)

VALUES ('paired with Corey', NOW());

INSERT INTO dids (body, date)

VALUES ('remembered that SQL is SINGLE QUOTES ONLY', NOW());

 

OOPS – we realize here that our table does not have a primary key

Step 7: Add a column for “primary key” that auto increments

ALTER TABLE dids ADD COLUMN id serial primary key;

look at the table schema with: \d <table_name>

Step 8: Query your new database!

select * from dids

 

Installed with:

Homebrew 1.7.1

postgres (PostgreSQL) 10.4